Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(498)

Side by Side Diff: webrtc/base/buffer.h

Issue 1749693002: rtc::Buffer: Use RTC_DCHECK instead of assert (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/base/buffer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_BASE_BUFFER_H_ 11 #ifndef WEBRTC_BASE_BUFFER_H_
12 #define WEBRTC_BASE_BUFFER_H_ 12 #define WEBRTC_BASE_BUFFER_H_
13 13
14 #include <cassert>
15 #include <cstring> 14 #include <cstring>
16 #include <memory> 15 #include <memory>
17 #include <utility> 16 #include <utility>
18 17
19 #include "webrtc/base/array_view.h" 18 #include "webrtc/base/array_view.h"
20 #include "webrtc/base/checks.h" 19 #include "webrtc/base/checks.h"
21 #include "webrtc/base/constructormagic.h" 20 #include "webrtc/base/constructormagic.h"
22 #include "webrtc/base/deprecation.h" 21 #include "webrtc/base/deprecation.h"
23 22
24 namespace rtc { 23 namespace rtc {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> 73 template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
75 Buffer(const T(&array)[N]) 74 Buffer(const T(&array)[N])
76 : Buffer(array, N) {} 75 : Buffer(array, N) {}
77 76
78 ~Buffer(); 77 ~Buffer();
79 78
80 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*, 79 // Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
81 // but you may also use .data<int8_t>() and .data<char>(). 80 // but you may also use .data<int8_t>() and .data<char>().
82 template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> 81 template <typename T = uint8_t, typename internal::ByteType<T>::t = 0>
83 const T* data() const { 82 const T* data() const {
84 assert(IsConsistent()); 83 RTC_DCHECK(IsConsistent());
85 return reinterpret_cast<T*>(data_.get()); 84 return reinterpret_cast<T*>(data_.get());
86 } 85 }
87 86
88 template <typename T = uint8_t, typename internal::ByteType<T>::t = 0> 87 template <typename T = uint8_t, typename internal::ByteType<T>::t = 0>
89 T* data() { 88 T* data() {
90 assert(IsConsistent()); 89 RTC_DCHECK(IsConsistent());
91 return reinterpret_cast<T*>(data_.get()); 90 return reinterpret_cast<T*>(data_.get());
92 } 91 }
93 92
94 size_t size() const { 93 size_t size() const {
95 assert(IsConsistent()); 94 RTC_DCHECK(IsConsistent());
96 return size_; 95 return size_;
97 } 96 }
98 97
99 size_t capacity() const { 98 size_t capacity() const {
100 assert(IsConsistent()); 99 RTC_DCHECK(IsConsistent());
101 return capacity_; 100 return capacity_;
102 } 101 }
103 102
104 Buffer& operator=(const Buffer& buf) { 103 Buffer& operator=(const Buffer& buf) {
105 if (&buf != this) 104 if (&buf != this)
106 SetData(buf.data(), buf.size()); 105 SetData(buf.data(), buf.size());
107 return *this; 106 return *this;
108 } 107 }
109 108
110 Buffer& operator=(Buffer&& buf) { 109 Buffer& operator=(Buffer&& buf) {
111 assert(IsConsistent()); 110 RTC_DCHECK(IsConsistent());
112 assert(buf.IsConsistent()); 111 RTC_DCHECK(buf.IsConsistent());
113 size_ = buf.size_; 112 size_ = buf.size_;
114 capacity_ = buf.capacity_; 113 capacity_ = buf.capacity_;
115 data_ = std::move(buf.data_); 114 data_ = std::move(buf.data_);
116 buf.OnMovedFrom(); 115 buf.OnMovedFrom();
117 return *this; 116 return *this;
118 } 117 }
119 118
120 bool operator==(const Buffer& buf) const { 119 bool operator==(const Buffer& buf) const {
121 assert(IsConsistent()); 120 RTC_DCHECK(IsConsistent());
122 return size_ == buf.size() && memcmp(data_.get(), buf.data(), size_) == 0; 121 return size_ == buf.size() && memcmp(data_.get(), buf.data(), size_) == 0;
123 } 122 }
124 123
125 bool operator!=(const Buffer& buf) const { return !(*this == buf); } 124 bool operator!=(const Buffer& buf) const { return !(*this == buf); }
126 125
127 // The SetData functions replace the contents of the buffer. They accept the 126 // The SetData functions replace the contents of the buffer. They accept the
128 // same input types as the constructors. 127 // same input types as the constructors.
129 template <typename T, typename internal::ByteType<T>::t = 0> 128 template <typename T, typename internal::ByteType<T>::t = 0>
130 void SetData(const T* data, size_t size) { 129 void SetData(const T* data, size_t size) {
131 assert(IsConsistent()); 130 RTC_DCHECK(IsConsistent());
132 size_ = 0; 131 size_ = 0;
133 AppendData(data, size); 132 AppendData(data, size);
134 } 133 }
135 134
136 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> 135 template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
137 void SetData(const T(&array)[N]) { 136 void SetData(const T(&array)[N]) {
138 SetData(array, N); 137 SetData(array, N);
139 } 138 }
140 139
141 void SetData(const Buffer& buf) { SetData(buf.data(), buf.size()); } 140 void SetData(const Buffer& buf) { SetData(buf.data(), buf.size()); }
(...skipping 10 matching lines...) Expand all
152 size_t SetData(size_t max_bytes, F&& setter) { 151 size_t SetData(size_t max_bytes, F&& setter) {
153 RTC_DCHECK(IsConsistent()); 152 RTC_DCHECK(IsConsistent());
154 size_ = 0; 153 size_ = 0;
155 return AppendData<T>(max_bytes, std::forward<F>(setter)); 154 return AppendData<T>(max_bytes, std::forward<F>(setter));
156 } 155 }
157 156
158 // The AppendData functions adds data to the end of the buffer. They accept 157 // The AppendData functions adds data to the end of the buffer. They accept
159 // the same input types as the constructors. 158 // the same input types as the constructors.
160 template <typename T, typename internal::ByteType<T>::t = 0> 159 template <typename T, typename internal::ByteType<T>::t = 0>
161 void AppendData(const T* data, size_t size) { 160 void AppendData(const T* data, size_t size) {
162 assert(IsConsistent()); 161 RTC_DCHECK(IsConsistent());
163 const size_t new_size = size_ + size; 162 const size_t new_size = size_ + size;
164 EnsureCapacity(new_size); 163 EnsureCapacity(new_size);
165 std::memcpy(data_.get() + size_, data, size); 164 std::memcpy(data_.get() + size_, data, size);
166 size_ = new_size; 165 size_ = new_size;
167 assert(IsConsistent()); 166 RTC_DCHECK(IsConsistent());
168 } 167 }
169 168
170 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> 169 template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
171 void AppendData(const T(&array)[N]) { 170 void AppendData(const T(&array)[N]) {
172 AppendData(array, N); 171 AppendData(array, N);
173 } 172 }
174 173
175 void AppendData(const Buffer& buf) { AppendData(buf.data(), buf.size()); } 174 void AppendData(const Buffer& buf) { AppendData(buf.data(), buf.size()); }
176 175
177 // Append at most |max_bytes| of data to the end of the buffer, using the 176 // Append at most |max_bytes| of data to the end of the buffer, using the
(...skipping 25 matching lines...) Expand all
203 // uninitialized. 202 // uninitialized.
204 void SetSize(size_t size) { 203 void SetSize(size_t size) {
205 EnsureCapacity(size); 204 EnsureCapacity(size);
206 size_ = size; 205 size_ = size;
207 } 206 }
208 207
209 // Ensure that the buffer size can be increased to at least capacity without 208 // Ensure that the buffer size can be increased to at least capacity without
210 // further reallocation. (Of course, this operation might need to reallocate 209 // further reallocation. (Of course, this operation might need to reallocate
211 // the buffer.) 210 // the buffer.)
212 void EnsureCapacity(size_t capacity) { 211 void EnsureCapacity(size_t capacity) {
213 assert(IsConsistent()); 212 RTC_DCHECK(IsConsistent());
214 if (capacity <= capacity_) 213 if (capacity <= capacity_)
215 return; 214 return;
216 std::unique_ptr<uint8_t[]> new_data(new uint8_t[capacity]); 215 std::unique_ptr<uint8_t[]> new_data(new uint8_t[capacity]);
217 std::memcpy(new_data.get(), data_.get(), size_); 216 std::memcpy(new_data.get(), data_.get(), size_);
218 data_ = std::move(new_data); 217 data_ = std::move(new_data);
219 capacity_ = capacity; 218 capacity_ = capacity;
220 assert(IsConsistent()); 219 RTC_DCHECK(IsConsistent());
221 } 220 }
222 221
223 // b.Pass() does the same thing as std::move(b). 222 // b.Pass() does the same thing as std::move(b).
224 // Deprecated; remove in March 2016 (bug 5373). 223 // Deprecated; remove in March 2016 (bug 5373).
225 RTC_DEPRECATED Buffer&& Pass() { return DEPRECATED_Pass(); } 224 RTC_DEPRECATED Buffer&& Pass() { return DEPRECATED_Pass(); }
226 225
227 Buffer&& DEPRECATED_Pass() { 226 Buffer&& DEPRECATED_Pass() {
228 assert(IsConsistent()); 227 RTC_DCHECK(IsConsistent());
229 return std::move(*this); 228 return std::move(*this);
230 } 229 }
231 230
232 // Resets the buffer to zero size without altering capacity. Works even if the 231 // Resets the buffer to zero size without altering capacity. Works even if the
233 // buffer has been moved from. 232 // buffer has been moved from.
234 void Clear() { 233 void Clear() {
235 size_ = 0; 234 size_ = 0;
236 assert(IsConsistent()); 235 RTC_DCHECK(IsConsistent());
237 } 236 }
238 237
239 // Swaps two buffers. Also works for buffers that have been moved from. 238 // Swaps two buffers. Also works for buffers that have been moved from.
240 friend void swap(Buffer& a, Buffer& b) { 239 friend void swap(Buffer& a, Buffer& b) {
241 using std::swap; 240 using std::swap;
242 swap(a.size_, b.size_); 241 swap(a.size_, b.size_);
243 swap(a.capacity_, b.capacity_); 242 swap(a.capacity_, b.capacity_);
244 swap(a.data_, b.data_); 243 swap(a.data_, b.data_);
245 } 244 }
246 245
(...skipping 22 matching lines...) Expand all
269 } 268 }
270 269
271 size_t size_; 270 size_t size_;
272 size_t capacity_; 271 size_t capacity_;
273 std::unique_ptr<uint8_t[]> data_; 272 std::unique_ptr<uint8_t[]> data_;
274 }; 273 };
275 274
276 } // namespace rtc 275 } // namespace rtc
277 276
278 #endif // WEBRTC_BASE_BUFFER_H_ 277 #endif // WEBRTC_BASE_BUFFER_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698