henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_BUFFER_H_ |
| 12 | #define WEBRTC_BASE_BUFFER_H_ |
| 13 | |
| 14 | #include <string.h> |
| 15 | |
kwiberg@webrtc.org | 11426dc | 2015-02-11 14:30:34 +0000 | [diff] [blame] | 16 | #include "webrtc/base/common.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | #include "webrtc/base/scoped_ptr.h" |
| 18 | |
| 19 | namespace rtc { |
| 20 | |
| 21 | // Basic buffer class, can be grown and shrunk dynamically. |
| 22 | // Unlike std::string/vector, does not initialize data when expanding capacity. |
| 23 | class Buffer { |
| 24 | public: |
| 25 | Buffer() { |
| 26 | Construct(NULL, 0, 0); |
| 27 | } |
| 28 | Buffer(const void* data, size_t length) { |
| 29 | Construct(data, length, length); |
| 30 | } |
| 31 | Buffer(const void* data, size_t length, size_t capacity) { |
| 32 | Construct(data, length, capacity); |
| 33 | } |
| 34 | Buffer(const Buffer& buf) { |
| 35 | Construct(buf.data(), buf.length(), buf.length()); |
| 36 | } |
| 37 | |
| 38 | const char* data() const { return data_.get(); } |
| 39 | char* data() { return data_.get(); } |
| 40 | // TODO: should this be size(), like STL? |
| 41 | size_t length() const { return length_; } |
| 42 | size_t capacity() const { return capacity_; } |
| 43 | |
| 44 | Buffer& operator=(const Buffer& buf) { |
| 45 | if (&buf != this) { |
| 46 | Construct(buf.data(), buf.length(), buf.length()); |
| 47 | } |
| 48 | return *this; |
| 49 | } |
| 50 | bool operator==(const Buffer& buf) const { |
| 51 | return (length_ == buf.length() && |
| 52 | memcmp(data_.get(), buf.data(), length_) == 0); |
| 53 | } |
| 54 | bool operator!=(const Buffer& buf) const { |
| 55 | return !operator==(buf); |
| 56 | } |
| 57 | |
| 58 | void SetData(const void* data, size_t length) { |
| 59 | ASSERT(data != NULL || length == 0); |
| 60 | SetLength(length); |
| 61 | memcpy(data_.get(), data, length); |
| 62 | } |
| 63 | void AppendData(const void* data, size_t length) { |
| 64 | ASSERT(data != NULL || length == 0); |
| 65 | size_t old_length = length_; |
| 66 | SetLength(length_ + length); |
| 67 | memcpy(data_.get() + old_length, data, length); |
| 68 | } |
| 69 | void SetLength(size_t length) { |
| 70 | SetCapacity(length); |
| 71 | length_ = length; |
| 72 | } |
| 73 | void SetCapacity(size_t capacity) { |
| 74 | if (capacity > capacity_) { |
| 75 | rtc::scoped_ptr<char[]> data(new char[capacity]); |
| 76 | memcpy(data.get(), data_.get(), length_); |
| 77 | data_.swap(data); |
| 78 | capacity_ = capacity; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void TransferTo(Buffer* buf) { |
| 83 | ASSERT(buf != NULL); |
| 84 | buf->data_.reset(data_.release()); |
| 85 | buf->length_ = length_; |
| 86 | buf->capacity_ = capacity_; |
| 87 | Construct(NULL, 0, 0); |
| 88 | } |
| 89 | |
| 90 | protected: |
| 91 | void Construct(const void* data, size_t length, size_t capacity) { |
| 92 | data_.reset(new char[capacity_ = capacity]); |
| 93 | SetData(data, length); |
| 94 | } |
| 95 | |
| 96 | scoped_ptr<char[]> data_; |
| 97 | size_t length_; |
| 98 | size_t capacity_; |
| 99 | }; |
| 100 | |
| 101 | } // namespace rtc |
| 102 | |
| 103 | #endif // WEBRTC_BASE_BUFFER_H_ |