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 |
14 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
15 | 17 |
16 namespace rtc { | 18 namespace rtc { |
17 | 19 |
| 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 |
18 // Many functions read from or write to arrays. The obvious way to do this is | 45 // Many functions read from or write to arrays. The obvious way to do this is |
19 // to use two arguments, a pointer to the first element and an element count: | 46 // to use two arguments, a pointer to the first element and an element count: |
20 // | 47 // |
21 // bool Contains17(const int* arr, size_t size) { | 48 // bool Contains17(const int* arr, size_t size) { |
22 // for (size_t i = 0; i < size; ++i) { | 49 // for (size_t i = 0; i < size; ++i) { |
23 // if (arr[i] == 17) | 50 // if (arr[i] == 17) |
24 // return true; | 51 // return true; |
25 // } | 52 // } |
26 // return false; | 53 // return false; |
27 // } | 54 // } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {} | 115 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {} |
89 | 116 |
90 // Construct an ArrayView for any type U that has a size() method whose | 117 // Construct an ArrayView for any type U that has a size() method whose |
91 // return value converts implicitly to size_t, and a data() method whose | 118 // return value converts implicitly to size_t, and a data() method whose |
92 // return value converts implicitly to T*. In particular, this means we allow | 119 // return value converts implicitly to T*. In particular, this means we allow |
93 // conversion from ArrayView<T> to ArrayView<const T>, but not the other way | 120 // conversion from ArrayView<T> to ArrayView<const T>, but not the other way |
94 // around. Other allowed conversions include std::vector<T> to ArrayView<T> | 121 // around. Other allowed conversions include std::vector<T> to ArrayView<T> |
95 // or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and | 122 // or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and |
96 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as | 123 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as |
97 // std::vector). | 124 // std::vector). |
98 template <typename U> | 125 template <typename U, |
| 126 typename std::enable_if< |
| 127 internal::HasDataAndSize<U, T>::value>::type* = nullptr> |
99 ArrayView(U& u) : ArrayView(u.data(), u.size()) {} | 128 ArrayView(U& u) : ArrayView(u.data(), u.size()) {} |
100 | 129 |
101 // Indexing, size, and iteration. These allow mutation even if the ArrayView | 130 // Indexing, size, and iteration. These allow mutation even if the ArrayView |
102 // is const, because the ArrayView doesn't own the array. (To prevent | 131 // is const, because the ArrayView doesn't own the array. (To prevent |
103 // mutation, use ArrayView<const T>.) | 132 // mutation, use ArrayView<const T>.) |
104 size_t size() const { return size_; } | 133 size_t size() const { return size_; } |
105 bool empty() const { return size_ == 0; } | 134 bool empty() const { return size_ == 0; } |
106 T* data() const { return data_; } | 135 T* data() const { return data_; } |
107 T& operator[](size_t idx) const { | 136 T& operator[](size_t idx) const { |
108 RTC_DCHECK_LT(idx, size_); | 137 RTC_DCHECK_LT(idx, size_); |
(...skipping 22 matching lines...) Expand all Loading... |
131 }; | 160 }; |
132 | 161 |
133 template <typename T> | 162 template <typename T> |
134 inline ArrayView<T> MakeArrayView(T* data, size_t size) { | 163 inline ArrayView<T> MakeArrayView(T* data, size_t size) { |
135 return ArrayView<T>(data, size); | 164 return ArrayView<T>(data, size); |
136 } | 165 } |
137 | 166 |
138 } // namespace rtc | 167 } // namespace rtc |
139 | 168 |
140 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ | 169 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ |
OLD | NEW |