OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_ARRAY_VIEW_H_ | 11 #ifndef WEBRTC_BASE_ARRAY_VIEW_H_ |
12 #define WEBRTC_BASE_ARRAY_VIEW_H_ | 12 #define WEBRTC_BASE_ARRAY_VIEW_H_ |
13 | 13 |
14 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
15 | 15 |
16 namespace rtc { | 16 namespace rtc { |
17 | 17 |
18 // Keeps track of an array (a pointer and a size) that it doesn't own. | 18 // Many functions read from or write to arrays. The obvious way to do this is |
19 // ArrayView objects are immutable except for assignment, and small enough to | 19 // to use two arguments, a pointer to the first element and an element count: |
20 // be cheaply passed by value. | |
21 // | 20 // |
22 // Note that ArrayView<T> and ArrayView<const T> are distinct types; this is | 21 // bool Contains17(const int* arr, size_t size) { |
23 // how you would represent mutable and unmutable views of an array. | 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 |
| 59 // ... |
| 60 // |
| 61 // One important point is that ArrayView<T> and ArrayView<const T> are |
| 62 // different types, which allow and don't allow mutation of the array elements, |
| 63 // respectively. The implicit conversions work just like you'd hope, so that |
| 64 // e.g. vector<int> will convert to either ArrayView<int> or ArrayView<const |
| 65 // int>, but const vector<int> will convert only to ArrayView<const int>. |
| 66 // (ArrayView itself can be the source type in such conversions, so |
| 67 // ArrayView<int> will convert to ArrayView<const int>.) |
| 68 // |
| 69 // Note: ArrayView is tiny (just a pointer and a count) and trivially copyable, |
| 70 // so it's probably cheaper to pass it by value than by const reference. |
24 template <typename T> | 71 template <typename T> |
25 class ArrayView final { | 72 class ArrayView final { |
26 public: | 73 public: |
27 // Construct an empty ArrayView. | 74 // Construct an empty ArrayView. |
28 ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {} | 75 ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {} |
29 | 76 |
30 // Construct an ArrayView for a (pointer,size) pair. | 77 // Construct an ArrayView for a (pointer,size) pair. |
31 template <typename U> | 78 template <typename U> |
32 ArrayView(U* data, size_t size) | 79 ArrayView(U* data, size_t size) |
33 : data_(size == 0 ? nullptr : data), size_(size) { | 80 : data_(size == 0 ? nullptr : data), size_(size) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 private: | 124 private: |
78 // Invariant: !data_ iff size_ == 0. | 125 // Invariant: !data_ iff size_ == 0. |
79 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); } | 126 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); } |
80 T* data_; | 127 T* data_; |
81 size_t size_; | 128 size_t size_; |
82 }; | 129 }; |
83 | 130 |
84 } // namespace rtc | 131 } // namespace rtc |
85 | 132 |
86 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ | 133 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ |
OLD | NEW |