blob: aa73ef72f604074b251e64f580c9fb3e30ae4bfb [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
Karl Wiberge2a83ee2015-10-26 19:51:29 +010014#include "webrtc/base/checks.h"
kwibergd3134032016-09-05 07:46:20 -070015#include "webrtc/base/type_traits.h"
Karl Wiberge2a83ee2015-10-26 19:51:29 +010016
17namespace rtc {
18
kwibergc3ddb3e2015-11-24 08:59:31 -080019// 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 Wiberge2a83ee2015-10-26 19:51:29 +010021//
kwibergc3ddb3e2015-11-24 08:59:31 -080022// 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
kwiberg3f81fcd2016-06-23 03:58:36 -070060// Contains17(nullptr); // nullptr -> empty ArrayView
kwibergc3ddb3e2015-11-24 08:59:31 -080061// ...
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 Wiberge2a83ee2015-10-26 19:51:29 +010073template <typename T>
74class ArrayView final {
75 public:
minyuea6f495c2016-10-24 09:19:14 -070076 using value_type = T;
77 using const_iterator = const T*;
78
Karl Wiberge2a83ee2015-10-26 19:51:29 +010079 // Construct an empty ArrayView.
80 ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {}
kwiberg3f81fcd2016-06-23 03:58:36 -070081 ArrayView(std::nullptr_t) : ArrayView() {}
Karl Wiberge2a83ee2015-10-26 19:51:29 +010082
83 // Construct an ArrayView for a (pointer,size) pair.
84 template <typename U>
85 ArrayView(U* data, size_t size)
86 : data_(size == 0 ? nullptr : data), size_(size) {
87 CheckInvariant();
88 }
89
90 // Construct an ArrayView for an array.
91 template <typename U, size_t N>
92 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {}
93
94 // Construct an ArrayView for any type U that has a size() method whose
95 // return value converts implicitly to size_t, and a data() method whose
96 // return value converts implicitly to T*. In particular, this means we allow
97 // conversion from ArrayView<T> to ArrayView<const T>, but not the other way
98 // around. Other allowed conversions include std::vector<T> to ArrayView<T>
99 // or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and
100 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as
101 // std::vector).
kwibergd3134032016-09-05 07:46:20 -0700102 template <
103 typename U,
104 typename std::enable_if<HasDataAndSize<U, T>::value>::type* = nullptr>
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100105 ArrayView(U& u) : ArrayView(u.data(), u.size()) {}
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100106
107 // Indexing, size, and iteration. These allow mutation even if the ArrayView
108 // is const, because the ArrayView doesn't own the array. (To prevent
109 // mutation, use ArrayView<const T>.)
110 size_t size() const { return size_; }
kwiberg288886b2015-11-06 01:21:35 -0800111 bool empty() const { return size_ == 0; }
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100112 T* data() const { return data_; }
113 T& operator[](size_t idx) const {
114 RTC_DCHECK_LT(idx, size_);
115 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant.
116 return data_[idx];
117 }
118 T* begin() const { return data_; }
119 T* end() const { return data_ + size_; }
120 const T* cbegin() const { return data_; }
121 const T* cend() const { return data_ + size_; }
122
danilchapa28780e2016-11-18 01:46:23 -0800123 ArrayView subview(size_t offset, size_t size) const {
124 if (offset >= size_)
125 return ArrayView();
126 return ArrayView(data_ + offset, std::min(size, size_ - offset));
127 }
128 ArrayView subview(size_t offset) const { return subview(offset, size_); }
129
kwiberg288886b2015-11-06 01:21:35 -0800130 // Comparing two ArrayViews compares their (pointer,size) pairs; it does
131 // *not* dereference the pointers.
132 friend bool operator==(const ArrayView& a, const ArrayView& b) {
133 return a.data_ == b.data_ && a.size_ == b.size_;
134 }
135 friend bool operator!=(const ArrayView& a, const ArrayView& b) {
136 return !(a == b);
137 }
138
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100139 private:
140 // Invariant: !data_ iff size_ == 0.
141 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); }
142 T* data_;
143 size_t size_;
144};
145
kwibergac554ee2016-09-02 00:39:33 -0700146template <typename T>
147inline ArrayView<T> MakeArrayView(T* data, size_t size) {
148 return ArrayView<T>(data, size);
149}
150
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100151} // namespace rtc
152
153#endif // WEBRTC_BASE_ARRAY_VIEW_H_