OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 | |
18 namespace 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. | |
26 template <typename T> | |
27 class 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 CheckInvariants(); | |
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 return data_[idx]; | |
68 } | |
69 T* begin() const { return data_; } | |
70 T* end() const { return data_ + size_; } | |
71 const T* cbegin() const { return data_; } | |
72 const T* cend() const { return data_ + size_; } | |
73 | |
74 private: | |
75 // Invariant: !data_ iff size_ == 0. | |
76 void CheckInvariants() const { RTC_DCHECK_EQ(!data_, size_ == 0); } | |
the sun
2015/10/22 14:31:30
nit: lose the plural "s"
kwiberg-webrtc
2015/10/22 20:58:01
But then I leak the implementation detail that the
| |
77 T* data_; | |
78 size_t size_; | |
79 }; | |
80 | |
81 } // namespace rtc | |
82 | |
83 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ | |
OLD | NEW |