Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_ARRAY_VIEW_H_ |
| 12 | #define WEBRTC_BASE_ARRAY_VIEW_H_ |
| 13 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 14 | #include "webrtc/base/checks.h" |
kwiberg | d313403 | 2016-09-05 07:46:20 -0700 | [diff] [blame] | 15 | #include "webrtc/base/type_traits.h" |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 16 | |
| 17 | namespace rtc { |
| 18 | |
kwiberg | c3ddb3e | 2015-11-24 08:59:31 -0800 | [diff] [blame] | 19 | // Many functions read from or write to arrays. The obvious way to do this is |
| 20 | // to use two arguments, a pointer to the first element and an element count: |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 21 | // |
kwiberg | c3ddb3e | 2015-11-24 08:59:31 -0800 | [diff] [blame] | 22 | // bool Contains17(const int* arr, size_t size) { |
| 23 | // for (size_t i = 0; i < size; ++i) { |
| 24 | // if (arr[i] == 17) |
| 25 | // return true; |
| 26 | // } |
| 27 | // return false; |
| 28 | // } |
| 29 | // |
| 30 | // This is flexible, since it doesn't matter how the array is stored (C array, |
| 31 | // std::vector, rtc::Buffer, ...), but it's error-prone because the caller has |
| 32 | // to correctly specify the array length: |
| 33 | // |
| 34 | // Contains17(arr, arraysize(arr)); // C array |
| 35 | // Contains17(&arr[0], arr.size()); // std::vector |
| 36 | // Contains17(arr, size); // pointer + size |
| 37 | // ... |
| 38 | // |
| 39 | // It's also kind of messy to have two separate arguments for what is |
| 40 | // conceptually a single thing. |
| 41 | // |
| 42 | // Enter rtc::ArrayView<T>. It contains a T pointer (to an array it doesn't |
| 43 | // own) and a count, and supports the basic things you'd expect, such as |
| 44 | // indexing and iteration. It allows us to write our function like this: |
| 45 | // |
| 46 | // bool Contains17(rtc::ArrayView<const int> arr) { |
| 47 | // for (auto e : arr) { |
| 48 | // if (e == 17) |
| 49 | // return true; |
| 50 | // } |
| 51 | // return false; |
| 52 | // } |
| 53 | // |
| 54 | // And even better, because a bunch of things will implicitly convert to |
| 55 | // ArrayView, we can call it like this: |
| 56 | // |
| 57 | // Contains17(arr); // C array |
| 58 | // Contains17(arr); // std::vector |
| 59 | // Contains17(rtc::ArrayView<int>(arr, size)); // pointer + size |
kwiberg | 3f81fcd | 2016-06-23 03:58:36 -0700 | [diff] [blame] | 60 | // Contains17(nullptr); // nullptr -> empty ArrayView |
kwiberg | c3ddb3e | 2015-11-24 08:59:31 -0800 | [diff] [blame] | 61 | // ... |
| 62 | // |
| 63 | // One important point is that ArrayView<T> and ArrayView<const T> are |
| 64 | // different types, which allow and don't allow mutation of the array elements, |
| 65 | // respectively. The implicit conversions work just like you'd hope, so that |
| 66 | // e.g. vector<int> will convert to either ArrayView<int> or ArrayView<const |
| 67 | // int>, but const vector<int> will convert only to ArrayView<const int>. |
| 68 | // (ArrayView itself can be the source type in such conversions, so |
| 69 | // ArrayView<int> will convert to ArrayView<const int>.) |
| 70 | // |
| 71 | // Note: ArrayView is tiny (just a pointer and a count) and trivially copyable, |
| 72 | // so it's probably cheaper to pass it by value than by const reference. |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 73 | template <typename T> |
| 74 | class ArrayView final { |
| 75 | public: |
| 76 | // Construct an empty ArrayView. |
| 77 | ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {} |
kwiberg | 3f81fcd | 2016-06-23 03:58:36 -0700 | [diff] [blame] | 78 | ArrayView(std::nullptr_t) : ArrayView() {} |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 79 | |
| 80 | // Construct an ArrayView for a (pointer,size) pair. |
| 81 | template <typename U> |
| 82 | ArrayView(U* data, size_t size) |
| 83 | : data_(size == 0 ? nullptr : data), size_(size) { |
| 84 | CheckInvariant(); |
| 85 | } |
| 86 | |
| 87 | // Construct an ArrayView for an array. |
| 88 | template <typename U, size_t N> |
| 89 | ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {} |
| 90 | |
| 91 | // Construct an ArrayView for any type U that has a size() method whose |
| 92 | // return value converts implicitly to size_t, and a data() method whose |
| 93 | // return value converts implicitly to T*. In particular, this means we allow |
| 94 | // conversion from ArrayView<T> to ArrayView<const T>, but not the other way |
| 95 | // around. Other allowed conversions include std::vector<T> to ArrayView<T> |
| 96 | // or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and |
| 97 | // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as |
| 98 | // std::vector). |
kwiberg | d313403 | 2016-09-05 07:46:20 -0700 | [diff] [blame] | 99 | template < |
| 100 | typename U, |
| 101 | typename std::enable_if<HasDataAndSize<U, T>::value>::type* = nullptr> |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 102 | ArrayView(U& u) : ArrayView(u.data(), u.size()) {} |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 103 | |
| 104 | // Indexing, size, and iteration. These allow mutation even if the ArrayView |
| 105 | // is const, because the ArrayView doesn't own the array. (To prevent |
| 106 | // mutation, use ArrayView<const T>.) |
| 107 | size_t size() const { return size_; } |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 108 | bool empty() const { return size_ == 0; } |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 109 | T* data() const { return data_; } |
| 110 | T& operator[](size_t idx) const { |
| 111 | RTC_DCHECK_LT(idx, size_); |
| 112 | RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant. |
| 113 | return data_[idx]; |
| 114 | } |
| 115 | T* begin() const { return data_; } |
| 116 | T* end() const { return data_ + size_; } |
| 117 | const T* cbegin() const { return data_; } |
| 118 | const T* cend() const { return data_ + size_; } |
| 119 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 120 | // Comparing two ArrayViews compares their (pointer,size) pairs; it does |
| 121 | // *not* dereference the pointers. |
| 122 | friend bool operator==(const ArrayView& a, const ArrayView& b) { |
| 123 | return a.data_ == b.data_ && a.size_ == b.size_; |
| 124 | } |
| 125 | friend bool operator!=(const ArrayView& a, const ArrayView& b) { |
| 126 | return !(a == b); |
| 127 | } |
| 128 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 129 | private: |
| 130 | // Invariant: !data_ iff size_ == 0. |
| 131 | void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); } |
| 132 | T* data_; |
| 133 | size_t size_; |
| 134 | }; |
| 135 | |
kwiberg | ac554ee | 2016-09-02 00:39:33 -0700 | [diff] [blame] | 136 | template <typename T> |
| 137 | inline ArrayView<T> MakeArrayView(T* data, size_t size) { |
| 138 | return ArrayView<T>(data, size); |
| 139 | } |
| 140 | |
Karl Wiberg | e2a83ee | 2015-10-26 19:51:29 +0100 | [diff] [blame] | 141 | } // namespace rtc |
| 142 | |
| 143 | #endif // WEBRTC_BASE_ARRAY_VIEW_H_ |