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