You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004-2010, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_BUFFER_H_
  28. #define TALK_BASE_BUFFER_H_
  29. #include <cstring>
  30. #include "talk/base/scoped_ptr.h"
  31. namespace talk_base {
  32. // Basic buffer class, can be grown and shrunk dynamically.
  33. // Unlike std::string/vector, does not initialize data when expanding capacity.
  34. class Buffer {
  35. public:
  36. Buffer() {
  37. Construct(NULL, 0, 0);
  38. }
  39. Buffer(const void* data, size_t length) {
  40. Construct(data, length, length);
  41. }
  42. Buffer(const void* data, size_t length, size_t capacity) {
  43. Construct(data, length, capacity);
  44. }
  45. Buffer(const Buffer& buf) {
  46. Construct(buf.data(), buf.length(), buf.length());
  47. }
  48. const char* data() const { return data_.get(); }
  49. char* data() { return data_.get(); }
  50. // TODO: should this be size(), like STL?
  51. size_t length() const { return length_; }
  52. size_t capacity() const { return capacity_; }
  53. Buffer& operator=(const Buffer& buf) {
  54. if (&buf != this) {
  55. Construct(buf.data(), buf.length(), buf.length());
  56. }
  57. return *this;
  58. }
  59. bool operator==(const Buffer& buf) const {
  60. return (length_ == buf.length() &&
  61. memcmp(data_.get(), buf.data(), length_) == 0);
  62. }
  63. bool operator!=(const Buffer& buf) const {
  64. return !operator==(buf);
  65. }
  66. void SetData(const void* data, size_t length) {
  67. ASSERT(data != NULL || length == 0);
  68. SetLength(length);
  69. memcpy(data_.get(), data, length);
  70. }
  71. void AppendData(const void* data, size_t length) {
  72. ASSERT(data != NULL || length == 0);
  73. size_t old_length = length_;
  74. SetLength(length_ + length);
  75. memcpy(data_.get() + old_length, data, length);
  76. }
  77. void SetLength(size_t length) {
  78. SetCapacity(length);
  79. length_ = length;
  80. }
  81. void SetCapacity(size_t capacity) {
  82. if (capacity > capacity_) {
  83. talk_base::scoped_array<char> data(new char[capacity]);
  84. memcpy(data.get(), data_.get(), length_);
  85. data_.swap(data);
  86. capacity_ = capacity;
  87. }
  88. }
  89. void TransferTo(Buffer* buf) {
  90. ASSERT(buf != NULL);
  91. buf->data_.reset(data_.release());
  92. buf->length_ = length_;
  93. buf->capacity_ = capacity_;
  94. Construct(NULL, 0, 0);
  95. }
  96. protected:
  97. void Construct(const void* data, size_t length, size_t capacity) {
  98. data_.reset(new char[capacity_ = capacity]);
  99. SetData(data, length);
  100. }
  101. scoped_array<char> data_;
  102. size_t length_;
  103. size_t capacity_;
  104. };
  105. } // namespace talk_base
  106. #endif // TALK_BASE_BUFFER_H_