blob: 868009631f83e69d22bb25a989866126ef144978 [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"
15
16namespace rtc {
17
kwibergc3ddb3e2015-11-24 08:59:31 -080018// 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:
Karl Wiberge2a83ee2015-10-26 19:51:29 +010020//
kwibergc3ddb3e2015-11-24 08:59:31 -080021// bool Contains17(const int* arr, size_t size) {
22// for (size_t i = 0; i < size; ++i) {
23// if (arr[i] == 17)
24// return true;
25// }
26// return false;
27// }
28//
29// This is flexible, since it doesn't matter how the array is stored (C array,
30// std::vector, rtc::Buffer, ...), but it's error-prone because the caller has
31// to correctly specify the array length:
32//
33// Contains17(arr, arraysize(arr)); // C array
34// Contains17(&arr[0], arr.size()); // std::vector
35// Contains17(arr, size); // pointer + size
36// ...
37//
38// It's also kind of messy to have two separate arguments for what is
39// conceptually a single thing.
40//
41// Enter rtc::ArrayView<T>. It contains a T pointer (to an array it doesn't
42// own) and a count, and supports the basic things you'd expect, such as
43// indexing and iteration. It allows us to write our function like this:
44//
45// bool Contains17(rtc::ArrayView<const int> arr) {
46// for (auto e : arr) {
47// if (e == 17)
48// return true;
49// }
50// return false;
51// }
52//
53// And even better, because a bunch of things will implicitly convert to
54// ArrayView, we can call it like this:
55//
56// Contains17(arr); // C array
57// Contains17(arr); // std::vector
58// Contains17(rtc::ArrayView<int>(arr, size)); // pointer + size
kwiberg3f81fcd2016-06-23 03:58:36 -070059// Contains17(nullptr); // nullptr -> empty ArrayView
kwibergc3ddb3e2015-11-24 08:59:31 -080060// ...
61//
62// One important point is that ArrayView<T> and ArrayView<const T> are
63// different types, which allow and don't allow mutation of the array elements,
64// respectively. The implicit conversions work just like you'd hope, so that
65// e.g. vector<int> will convert to either ArrayView<int> or ArrayView<const
66// int>, but const vector<int> will convert only to ArrayView<const int>.
67// (ArrayView itself can be the source type in such conversions, so
68// ArrayView<int> will convert to ArrayView<const int>.)
69//
70// Note: ArrayView is tiny (just a pointer and a count) and trivially copyable,
71// so it's probably cheaper to pass it by value than by const reference.
Karl Wiberge2a83ee2015-10-26 19:51:29 +010072template <typename T>
73class ArrayView final {
74 public:
75 // Construct an empty ArrayView.
76 ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {}
kwiberg3f81fcd2016-06-23 03:58:36 -070077 ArrayView(std::nullptr_t) : ArrayView() {}
Karl Wiberge2a83ee2015-10-26 19:51:29 +010078
79 // Construct an ArrayView for a (pointer,size) pair.
80 template <typename U>
81 ArrayView(U* data, size_t size)
82 : data_(size == 0 ? nullptr : data), size_(size) {
83 CheckInvariant();
84 }
85
86 // Construct an ArrayView for an array.
87 template <typename U, size_t N>
88 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {}
89
90 // 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
92 // 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
94 // 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
96 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as
97 // std::vector).
98 template <typename U>
99 ArrayView(U& u) : ArrayView(u.data(), u.size()) {}
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100100
101 // Indexing, size, and iteration. These allow mutation even if the ArrayView
102 // is const, because the ArrayView doesn't own the array. (To prevent
103 // mutation, use ArrayView<const T>.)
104 size_t size() const { return size_; }
kwiberg288886b2015-11-06 01:21:35 -0800105 bool empty() const { return size_ == 0; }
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100106 T* data() const { return data_; }
107 T& operator[](size_t idx) const {
108 RTC_DCHECK_LT(idx, size_);
109 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant.
110 return data_[idx];
111 }
112 T* begin() const { return data_; }
113 T* end() const { return data_ + size_; }
114 const T* cbegin() const { return data_; }
115 const T* cend() const { return data_ + size_; }
116
kwiberg288886b2015-11-06 01:21:35 -0800117 // Comparing two ArrayViews compares their (pointer,size) pairs; it does
118 // *not* dereference the pointers.
119 friend bool operator==(const ArrayView& a, const ArrayView& b) {
120 return a.data_ == b.data_ && a.size_ == b.size_;
121 }
122 friend bool operator!=(const ArrayView& a, const ArrayView& b) {
123 return !(a == b);
124 }
125
Karl Wiberge2a83ee2015-10-26 19:51:29 +0100126 private:
127 // Invariant: !data_ iff size_ == 0.
128 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); }
129 T* data_;
130 size_t size_;
131};
132
133} // namespace rtc
134
135#endif // WEBRTC_BASE_ARRAY_VIEW_H_