blob: 019bd8b6c6b8f37f348f3d8a4d87f742f9602eba [file] [log] [blame]
Karl Wiberg882e2212015-10-26 18:51:291/*
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_; }
64 T* data() const { return data_; }
65 T& operator[](size_t idx) const {
66 RTC_DCHECK_LT(idx, size_);
67 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant.
68 return data_[idx];
69 }
70 T* begin() const { return data_; }
71 T* end() const { return data_ + size_; }
72 const T* cbegin() const { return data_; }
73 const T* cend() const { return data_ + size_; }
74
75 private:
76 // Invariant: !data_ iff size_ == 0.
77 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); }
78 T* data_;
79 size_t size_;
80};
81
82} // namespace rtc
83
84#endif // WEBRTC_BASE_ARRAY_VIEW_H_