blob: 725a2d79d6f6fcc055e787dc7627889b30f74569 [file] [log] [blame]
Karl Wiberge2a83ee2015-10-26 19:51:29 +01001/*
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
kwibergbd431722016-09-05 04:20:54 -070014#include <type_traits>
15
Karl Wiberge2a83ee2015-10-26 19:51:29 +010016#include "webrtc/base/checks.h"
17
18namespace rtc {
19
kwibergbd431722016-09-05 04:20:54 -070020namespace 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.
25template <typename DS, typename T>
26class 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
kwibergc3ddb3e2015-11-24 08:59:31 -080045// 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:
Karl Wiberge2a83ee2015-10-26 19:51:29 +010047//
kwibergc3ddb3e2015-11-24 08:59:31 -080048// bool Contains17(const int* arr, size_t size) {
49// for (size_t i = 0; i < size; ++i) {
50// if (arr[i] == 17)
51// return true;
52// }
53// return false;
54// }
55//
56// This is flexible, since it doesn't matter how the array is stored (C array,
57// std::vector, rtc::Buffer, ...), but it's error-prone because the caller has
58// to correctly specify the array length:
59//
60// Contains17(arr, arraysize(arr)); // C array
61// Contains17(&arr[0], arr.size()); // std::vector
62// Contains17(arr, size); // pointer + size
63// ...
64//
65// It's also kind of messy to have two separate arguments for what is
66// conceptually a single thing.
67//
68// Enter rtc::ArrayView<T>. It contains a T pointer (to an array it doesn't
69// own) and a count, and supports the basic things you'd expect, such as
70// indexing and iteration. It allows us to write our function like this:
71//
72// bool Contains17(rtc::ArrayView<const int> arr) {
73// for (auto e : arr) {
74// if (e == 17)
75// return true;
76// }
77// return false;
78// }
79//
80// And even better, because a bunch of things will implicitly convert to
81// ArrayView, we can call it like this:
82//
83// Contains17(arr); // C array
84// Contains17(arr); // std::vector
85// Contains17(rtc::ArrayView<int>(arr, size)); // pointer + size
kwiberg3f81fcd2016-06-23 03:58:36 -070086// Contains17(nullptr); // nullptr -> empty ArrayView
kwibergc3ddb3e2015-11-24 08:59:31 -080087// ...
88//
89// One important point is that ArrayView<T> and ArrayView<const T> are
90// different types, which allow and don't allow mutation of the array elements,
91// respectively. The implicit conversions work just like you'd hope, so that
92// e.g. vector<int> will convert to either ArrayView<int> or ArrayView<const
93// int>, but const vector<int> will convert only to ArrayView<const int>.
94// (ArrayView itself can be the source type in such conversions, so
95// ArrayView<int> will convert to ArrayView<const int>.)
96//
97// Note: ArrayView is tiny (just a pointer and a count) and trivially copyable,
98// so it's probably cheaper to pass it by value than by const reference.
Karl Wiberge2a83ee2015-10-26 19:51:29 +010099template <typename T>
100class ArrayView final {
101 public:
102 // Construct an empty ArrayView.
103 ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {}
kwiberg3f81fcd2016-06-23 03:58:36 -0700104 ArrayView(std::nullptr_t) : ArrayView() {}
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100105
106 // Construct an ArrayView for a (pointer,size) pair.
107 template <typename U>
108 ArrayView(U* data, size_t size)
109 : data_(size == 0 ? nullptr : data), size_(size) {
110 CheckInvariant();
111 }
112
113 // Construct an ArrayView for an array.
114 template <typename U, size_t N>
115 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {}
116
117 // 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
119 // 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
121 // 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
123 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as
124 // std::vector).
kwibergbd431722016-09-05 04:20:54 -0700125 template <typename U,
126 typename std::enable_if<
127 internal::HasDataAndSize<U, T>::value>::type* = nullptr>
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100128 ArrayView(U& u) : ArrayView(u.data(), u.size()) {}
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100129
130 // Indexing, size, and iteration. These allow mutation even if the ArrayView
131 // is const, because the ArrayView doesn't own the array. (To prevent
132 // mutation, use ArrayView<const T>.)
133 size_t size() const { return size_; }
kwiberg288886b2015-11-06 01:21:35 -0800134 bool empty() const { return size_ == 0; }
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100135 T* data() const { return data_; }
136 T& operator[](size_t idx) const {
137 RTC_DCHECK_LT(idx, size_);
138 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant.
139 return data_[idx];
140 }
141 T* begin() const { return data_; }
142 T* end() const { return data_ + size_; }
143 const T* cbegin() const { return data_; }
144 const T* cend() const { return data_ + size_; }
145
kwiberg288886b2015-11-06 01:21:35 -0800146 // Comparing two ArrayViews compares their (pointer,size) pairs; it does
147 // *not* dereference the pointers.
148 friend bool operator==(const ArrayView& a, const ArrayView& b) {
149 return a.data_ == b.data_ && a.size_ == b.size_;
150 }
151 friend bool operator!=(const ArrayView& a, const ArrayView& b) {
152 return !(a == b);
153 }
154
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100155 private:
156 // Invariant: !data_ iff size_ == 0.
157 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); }
158 T* data_;
159 size_t size_;
160};
161
kwibergac554ee2016-09-02 00:39:33 -0700162template <typename T>
163inline ArrayView<T> MakeArrayView(T* data, size_t size) {
164 return ArrayView<T>(data, size);
165}
166
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100167} // namespace rtc
168
169#endif // WEBRTC_BASE_ARRAY_VIEW_H_