OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 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_ARRAY_VIEW_H_ | 11 #ifndef WEBRTC_BASE_ARRAY_VIEW_H_ |
12 #define WEBRTC_BASE_ARRAY_VIEW_H_ | 12 #define WEBRTC_BASE_ARRAY_VIEW_H_ |
13 | 13 |
14 #include <type_traits> | |
15 | |
16 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/type_traits.h" |
17 | 16 |
18 namespace rtc { | 17 namespace rtc { |
19 | 18 |
20 namespace internal { | |
21 | |
22 // (Internal; please don't use outside this file.) Determines if the given | |
23 // class has zero-argument .data() and .size() methods whose return values are | |
24 // convertible to T* and size_t, respectively. | |
25 template <typename DS, typename T> | |
26 class HasDataAndSize { | |
27 private: | |
28 template < | |
29 typename C, | |
30 typename std::enable_if< | |
31 std::is_convertible<decltype(std::declval<C>().data()), T*>::value && | |
32 std::is_convertible<decltype(std::declval<C>().size()), | |
33 size_t>::value>::type* = nullptr> | |
34 static int Test(int); | |
35 | |
36 template <typename> | |
37 static char Test(...); | |
38 | |
39 public: | |
40 static constexpr bool value = std::is_same<decltype(Test<DS>(0)), int>::value; | |
41 }; | |
42 | |
43 } // namespace internal | |
44 | |
45 // Many functions read from or write to arrays. The obvious way to do this is | 19 // Many functions read from or write to arrays. The obvious way to do this is |
46 // to use two arguments, a pointer to the first element and an element count: | 20 // to use two arguments, a pointer to the first element and an element count: |
47 // | 21 // |
48 // bool Contains17(const int* arr, size_t size) { | 22 // bool Contains17(const int* arr, size_t size) { |
49 // for (size_t i = 0; i < size; ++i) { | 23 // for (size_t i = 0; i < size; ++i) { |
50 // if (arr[i] == 17) | 24 // if (arr[i] == 17) |
51 // return true; | 25 // return true; |
52 // } | 26 // } |
53 // return false; | 27 // return false; |
54 // } | 28 // } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {} | 89 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {} |
116 | 90 |
117 // Construct an ArrayView for any type U that has a size() method whose | 91 // Construct an ArrayView for any type U that has a size() method whose |
118 // return value converts implicitly to size_t, and a data() method whose | 92 // return value converts implicitly to size_t, and a data() method whose |
119 // return value converts implicitly to T*. In particular, this means we allow | 93 // return value converts implicitly to T*. In particular, this means we allow |
120 // conversion from ArrayView<T> to ArrayView<const T>, but not the other way | 94 // conversion from ArrayView<T> to ArrayView<const T>, but not the other way |
121 // around. Other allowed conversions include std::vector<T> to ArrayView<T> | 95 // around. Other allowed conversions include std::vector<T> to ArrayView<T> |
122 // or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and | 96 // or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and |
123 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as | 97 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as |
124 // std::vector). | 98 // std::vector). |
125 template <typename U, | 99 template < |
126 typename std::enable_if< | 100 typename U, |
127 internal::HasDataAndSize<U, T>::value>::type* = nullptr> | 101 typename std::enable_if<HasDataAndSize<U, T>::value>::type* = nullptr> |
128 ArrayView(U& u) : ArrayView(u.data(), u.size()) {} | 102 ArrayView(U& u) : ArrayView(u.data(), u.size()) {} |
129 | 103 |
130 // Indexing, size, and iteration. These allow mutation even if the ArrayView | 104 // Indexing, size, and iteration. These allow mutation even if the ArrayView |
131 // is const, because the ArrayView doesn't own the array. (To prevent | 105 // is const, because the ArrayView doesn't own the array. (To prevent |
132 // mutation, use ArrayView<const T>.) | 106 // mutation, use ArrayView<const T>.) |
133 size_t size() const { return size_; } | 107 size_t size() const { return size_; } |
134 bool empty() const { return size_ == 0; } | 108 bool empty() const { return size_ == 0; } |
135 T* data() const { return data_; } | 109 T* data() const { return data_; } |
136 T& operator[](size_t idx) const { | 110 T& operator[](size_t idx) const { |
137 RTC_DCHECK_LT(idx, size_); | 111 RTC_DCHECK_LT(idx, size_); |
(...skipping 22 matching lines...) Expand all Loading... |
160 }; | 134 }; |
161 | 135 |
162 template <typename T> | 136 template <typename T> |
163 inline ArrayView<T> MakeArrayView(T* data, size_t size) { | 137 inline ArrayView<T> MakeArrayView(T* data, size_t size) { |
164 return ArrayView<T>(data, size); | 138 return ArrayView<T>(data, size); |
165 } | 139 } |
166 | 140 |
167 } // namespace rtc | 141 } // namespace rtc |
168 | 142 |
169 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ | 143 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ |
OLD | NEW |