OLD | NEW |
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 <algorithm> | 14 #include <algorithm> |
15 #include <cstring> | 15 #include <cstring> |
16 #include <memory> | 16 #include <memory> |
17 #include <type_traits> | 17 #include <type_traits> |
18 #include <utility> | 18 #include <utility> |
19 | 19 |
20 #include "webrtc/base/array_view.h" | 20 #include "webrtc/base/array_view.h" |
21 #include "webrtc/base/checks.h" | 21 #include "webrtc/base/checks.h" |
| 22 #include "webrtc/base/type_traits.h" |
22 | 23 |
23 namespace rtc { | 24 namespace rtc { |
24 | 25 |
25 namespace internal { | 26 namespace internal { |
26 | 27 |
27 // (Internal; please don't use outside this file.) Determines if elements of | 28 // (Internal; please don't use outside this file.) Determines if elements of |
28 // type U are compatible with a BufferT<T>. For most types, we just ignore | 29 // type U are compatible with a BufferT<T>. For most types, we just ignore |
29 // top-level const and forbid top-level volatile and require T and U to be | 30 // top-level const and forbid top-level volatile and require T and U to be |
30 // otherwise equal, but all byte-sized integers (notably char, int8_t, and | 31 // otherwise equal, but all byte-sized integers (notably char, int8_t, and |
31 // uint8_t) are compatible with each other. (Note: We aim to get rid of this | 32 // uint8_t) are compatible with each other. (Note: We aim to get rid of this |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 } | 191 } |
191 | 192 |
192 template <typename U, | 193 template <typename U, |
193 size_t N, | 194 size_t N, |
194 typename std::enable_if< | 195 typename std::enable_if< |
195 internal::BufferCompat<T, U>::value>::type* = nullptr> | 196 internal::BufferCompat<T, U>::value>::type* = nullptr> |
196 void SetData(const U (&array)[N]) { | 197 void SetData(const U (&array)[N]) { |
197 SetData(array, N); | 198 SetData(array, N); |
198 } | 199 } |
199 | 200 |
200 void SetData(const BufferT& buf) { SetData(buf.data(), buf.size()); } | 201 template <typename W, |
| 202 typename std::enable_if< |
| 203 HasDataAndSize<const W, const T>::value>::type* = nullptr> |
| 204 void SetData(const W& w) { |
| 205 SetData(w.data(), w.size()); |
| 206 } |
201 | 207 |
202 // Replace the data in the buffer with at most |max_elements| of data, using | 208 // Replace the data in the buffer with at most |max_elements| of data, using |
203 // the function |setter|, which should have the following signature: | 209 // the function |setter|, which should have the following signature: |
204 // size_t setter(ArrayView<U> view) | 210 // size_t setter(ArrayView<U> view) |
205 // |setter| is given an appropriately typed ArrayView of the area in which to | 211 // |setter| is given an appropriately typed ArrayView of the area in which to |
206 // write the data (i.e. starting at the beginning of the buffer) and should | 212 // write the data (i.e. starting at the beginning of the buffer) and should |
207 // return the number of elements actually written. This number must be <= | 213 // return the number of elements actually written. This number must be <= |
208 // |max_elements|. | 214 // |max_elements|. |
209 template <typename U = T, | 215 template <typename U = T, |
210 typename F, | 216 typename F, |
(...skipping 21 matching lines...) Expand all Loading... |
232 } | 238 } |
233 | 239 |
234 template <typename U, | 240 template <typename U, |
235 size_t N, | 241 size_t N, |
236 typename std::enable_if< | 242 typename std::enable_if< |
237 internal::BufferCompat<T, U>::value>::type* = nullptr> | 243 internal::BufferCompat<T, U>::value>::type* = nullptr> |
238 void AppendData(const U (&array)[N]) { | 244 void AppendData(const U (&array)[N]) { |
239 AppendData(array, N); | 245 AppendData(array, N); |
240 } | 246 } |
241 | 247 |
242 void AppendData(const BufferT& buf) { AppendData(buf.data(), buf.size()); } | 248 template <typename W, |
| 249 typename std::enable_if< |
| 250 HasDataAndSize<const W, const T>::value>::type* = nullptr> |
| 251 void AppendData(const W& w) { |
| 252 AppendData(w.data(), w.size()); |
| 253 } |
243 | 254 |
244 template <typename U, | 255 template <typename U, |
245 typename std::enable_if< | 256 typename std::enable_if< |
246 internal::BufferCompat<T, U>::value>::type* = nullptr> | 257 internal::BufferCompat<T, U>::value>::type* = nullptr> |
247 void AppendData(const U& item) { | 258 void AppendData(const U& item) { |
248 AppendData(&item, 1); | 259 AppendData(&item, 1); |
249 } | 260 } |
250 | 261 |
251 // Append at most |max_elements| to the end of the buffer, using the function | 262 // Append at most |max_elements| to the end of the buffer, using the function |
252 // |setter|, which should have the following signature: | 263 // |setter|, which should have the following signature: |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 size_t capacity_; | 365 size_t capacity_; |
355 std::unique_ptr<T[]> data_; | 366 std::unique_ptr<T[]> data_; |
356 }; | 367 }; |
357 | 368 |
358 // By far the most common sort of buffer. | 369 // By far the most common sort of buffer. |
359 using Buffer = BufferT<uint8_t>; | 370 using Buffer = BufferT<uint8_t>; |
360 | 371 |
361 } // namespace rtc | 372 } // namespace rtc |
362 | 373 |
363 #endif // WEBRTC_BASE_BUFFER_H_ | 374 #endif // WEBRTC_BASE_BUFFER_H_ |
OLD | NEW |