blob: c77a6e16b4ab3f998827f382182cfe0fa63b7190 [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
18// Keeps track of an array (a pointer and a size) that it doesn't own.
19// ArrayView objects are immutable except for assignment, and small enough to
20// be cheaply passed by value.
21//
22// Note that ArrayView<T> and ArrayView<const T> are distinct types; this is
23// how you would represent mutable and unmutable views of an array.
24template <typename T>
25class ArrayView final {
26 public:
27 // Construct an empty ArrayView.
28 ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {}
29
30 // Construct an ArrayView for a (pointer,size) pair.
31 template <typename U>
32 ArrayView(U* data, size_t size)
33 : data_(size == 0 ? nullptr : data), size_(size) {
34 CheckInvariant();
35 }
36
37 // Construct an ArrayView for an array.
38 template <typename U, size_t N>
39 ArrayView(U (&array)[N]) : ArrayView(&array[0], N) {}
40
41 // Construct an ArrayView for any type U that has a size() method whose
42 // return value converts implicitly to size_t, and a data() method whose
43 // return value converts implicitly to T*. In particular, this means we allow
44 // conversion from ArrayView<T> to ArrayView<const T>, but not the other way
45 // around. Other allowed conversions include std::vector<T> to ArrayView<T>
46 // or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and
47 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as
48 // std::vector).
49 template <typename U>
50 ArrayView(U& u) : ArrayView(u.data(), u.size()) {}
Karl Wiberge2a83ee2015-10-26 19:51:29 +010051
52 // Indexing, size, and iteration. These allow mutation even if the ArrayView
53 // is const, because the ArrayView doesn't own the array. (To prevent
54 // mutation, use ArrayView<const T>.)
55 size_t size() const { return size_; }
kwiberg288886b2015-11-06 01:21:35 -080056 bool empty() const { return size_ == 0; }
Karl Wiberge2a83ee2015-10-26 19:51:29 +010057 T* data() const { return data_; }
58 T& operator[](size_t idx) const {
59 RTC_DCHECK_LT(idx, size_);
60 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant.
61 return data_[idx];
62 }
63 T* begin() const { return data_; }
64 T* end() const { return data_ + size_; }
65 const T* cbegin() const { return data_; }
66 const T* cend() const { return data_ + size_; }
67
kwiberg288886b2015-11-06 01:21:35 -080068 // Comparing two ArrayViews compares their (pointer,size) pairs; it does
69 // *not* dereference the pointers.
70 friend bool operator==(const ArrayView& a, const ArrayView& b) {
71 return a.data_ == b.data_ && a.size_ == b.size_;
72 }
73 friend bool operator!=(const ArrayView& a, const ArrayView& b) {
74 return !(a == b);
75 }
76
Karl Wiberge2a83ee2015-10-26 19:51:29 +010077 private:
78 // Invariant: !data_ iff size_ == 0.
79 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); }
80 T* data_;
81 size_t size_;
82};
83
84} // namespace rtc
85
86#endif // WEBRTC_BASE_ARRAY_VIEW_H_