blob: a7bc5705167811e1e5cc3300ec405cdf37a369d0 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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.org11426dc2015-02-11 14:30:34 +000016#include "webrtc/base/common.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/scoped_ptr.h"
18
19namespace rtc {
20
21// Basic buffer class, can be grown and shrunk dynamically.
22// Unlike std::string/vector, does not initialize data when expanding capacity.
23class Buffer {
24 public:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000025 Buffer();
26 Buffer(const void* data, size_t length);
27 Buffer(const void* data, size_t length, size_t capacity);
28 Buffer(const Buffer& buf);
29 ~Buffer();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030
31 const char* data() const { return data_.get(); }
32 char* data() { return data_.get(); }
33 // TODO: should this be size(), like STL?
34 size_t length() const { return length_; }
35 size_t capacity() const { return capacity_; }
36
37 Buffer& operator=(const Buffer& buf) {
38 if (&buf != this) {
39 Construct(buf.data(), buf.length(), buf.length());
40 }
41 return *this;
42 }
43 bool operator==(const Buffer& buf) const {
44 return (length_ == buf.length() &&
45 memcmp(data_.get(), buf.data(), length_) == 0);
46 }
47 bool operator!=(const Buffer& buf) const {
48 return !operator==(buf);
49 }
50
51 void SetData(const void* data, size_t length) {
52 ASSERT(data != NULL || length == 0);
53 SetLength(length);
54 memcpy(data_.get(), data, length);
55 }
56 void AppendData(const void* data, size_t length) {
57 ASSERT(data != NULL || length == 0);
58 size_t old_length = length_;
59 SetLength(length_ + length);
60 memcpy(data_.get() + old_length, data, length);
61 }
62 void SetLength(size_t length) {
63 SetCapacity(length);
64 length_ = length;
65 }
66 void SetCapacity(size_t capacity) {
67 if (capacity > capacity_) {
68 rtc::scoped_ptr<char[]> data(new char[capacity]);
69 memcpy(data.get(), data_.get(), length_);
70 data_.swap(data);
71 capacity_ = capacity;
72 }
73 }
74
75 void TransferTo(Buffer* buf) {
76 ASSERT(buf != NULL);
77 buf->data_.reset(data_.release());
78 buf->length_ = length_;
79 buf->capacity_ = capacity_;
80 Construct(NULL, 0, 0);
81 }
82
83 protected:
84 void Construct(const void* data, size_t length, size_t capacity) {
85 data_.reset(new char[capacity_ = capacity]);
86 SetData(data, length);
87 }
88
89 scoped_ptr<char[]> data_;
90 size_t length_;
91 size_t capacity_;
92};
93
94} // namespace rtc
95
96#endif // WEBRTC_BASE_BUFFER_H_