Chromium Code Reviews| 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 using SizeOne = char[1]; | |
| 29 using SizeTwo = char[2]; | |
| 30 | |
| 31 template < | |
| 32 typename C, | |
| 33 typename std::enable_if< | |
| 34 std::is_convertible<decltype(std::declval<C>().data()), T*>::value && | |
| 35 std::is_convertible<decltype(std::declval<C>().size()), | |
| 36 size_t>::value>::type* = nullptr> | |
| 37 static SizeOne& Test(int); | |
| 38 | |
| 39 template <typename> | |
| 40 static SizeTwo& Test(...); | |
| 41 | |
| 42 public: | |
| 43 static constexpr bool value = (sizeof(Test<DS>(0)) == 1); | |
|
tommi
2016/09/03 14:56:47
nit: == sizeof(SizeOne) to help readers connect th
kwiberg-webrtc
2016/09/03 15:56:43
I shortened it some more, with the help of std::is
| |
| 44 }; | |
| 45 | |
| 46 } // namespace internal | |
| 47 | |
| 18 // Many functions read from or write to arrays. The obvious way to do this is | 48 // 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: | 49 // to use two arguments, a pointer to the first element and an element count: |
| 20 // | 50 // |
| 21 // bool Contains17(const int* arr, size_t size) { | 51 // bool Contains17(const int* arr, size_t size) { |
| 22 // for (size_t i = 0; i < size; ++i) { | 52 // for (size_t i = 0; i < size; ++i) { |
| 23 // if (arr[i] == 17) | 53 // if (arr[i] == 17) |
| 24 // return true; | 54 // return true; |
| 25 // } | 55 // } |
| 26 // return false; | 56 // return false; |
| 27 // } | 57 // } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {} | 118 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {} |
| 89 | 119 |
| 90 // Construct an ArrayView for any type U that has a size() method whose | 120 // 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 | 121 // 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 | 122 // 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 | 123 // 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> | 124 // 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 | 125 // 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 | 126 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as |
| 97 // std::vector). | 127 // std::vector). |
| 98 template <typename U> | 128 template <typename U, |
| 129 typename std::enable_if< | |
| 130 internal::HasDataAndSize<U, T>::value>::type* = nullptr> | |
| 99 ArrayView(U& u) : ArrayView(u.data(), u.size()) {} | 131 ArrayView(U& u) : ArrayView(u.data(), u.size()) {} |
| 100 | 132 |
| 101 // Indexing, size, and iteration. These allow mutation even if the ArrayView | 133 // Indexing, size, and iteration. These allow mutation even if the ArrayView |
| 102 // is const, because the ArrayView doesn't own the array. (To prevent | 134 // is const, because the ArrayView doesn't own the array. (To prevent |
| 103 // mutation, use ArrayView<const T>.) | 135 // mutation, use ArrayView<const T>.) |
| 104 size_t size() const { return size_; } | 136 size_t size() const { return size_; } |
| 105 bool empty() const { return size_ == 0; } | 137 bool empty() const { return size_ == 0; } |
| 106 T* data() const { return data_; } | 138 T* data() const { return data_; } |
| 107 T& operator[](size_t idx) const { | 139 T& operator[](size_t idx) const { |
| 108 RTC_DCHECK_LT(idx, size_); | 140 RTC_DCHECK_LT(idx, size_); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 131 }; | 163 }; |
| 132 | 164 |
| 133 template <typename T> | 165 template <typename T> |
| 134 inline ArrayView<T> MakeArrayView(T* data, size_t size) { | 166 inline ArrayView<T> MakeArrayView(T* data, size_t size) { |
| 135 return ArrayView<T>(data, size); | 167 return ArrayView<T>(data, size); |
| 136 } | 168 } |
| 137 | 169 |
| 138 } // namespace rtc | 170 } // namespace rtc |
| 139 | 171 |
| 140 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ | 172 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ |
| OLD | NEW |