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 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 // } | 49 // } |
50 // return false; | 50 // return false; |
51 // } | 51 // } |
52 // | 52 // |
53 // And even better, because a bunch of things will implicitly convert to | 53 // And even better, because a bunch of things will implicitly convert to |
54 // ArrayView, we can call it like this: | 54 // ArrayView, we can call it like this: |
55 // | 55 // |
56 // Contains17(arr); // C array | 56 // Contains17(arr); // C array |
57 // Contains17(arr); // std::vector | 57 // Contains17(arr); // std::vector |
58 // Contains17(rtc::ArrayView<int>(arr, size)); // pointer + size | 58 // Contains17(rtc::ArrayView<int>(arr, size)); // pointer + size |
| 59 // Contains17(nullptr); // nullptr -> empty ArrayView |
59 // ... | 60 // ... |
60 // | 61 // |
61 // One important point is that ArrayView<T> and ArrayView<const T> are | 62 // 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 // 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 // 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 // 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 // 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 itself can be the source type in such conversions, so |
67 // ArrayView<int> will convert to ArrayView<const int>.) | 68 // ArrayView<int> will convert to ArrayView<const int>.) |
68 // | 69 // |
69 // Note: ArrayView is tiny (just a pointer and a count) and trivially copyable, | 70 // 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. | 71 // so it's probably cheaper to pass it by value than by const reference. |
71 template <typename T> | 72 template <typename T> |
72 class ArrayView final { | 73 class ArrayView final { |
73 public: | 74 public: |
74 // Construct an empty ArrayView. | 75 // Construct an empty ArrayView. |
75 ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {} | 76 ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {} |
| 77 ArrayView(std::nullptr_t) : ArrayView() {} |
76 | 78 |
77 // Construct an ArrayView for a (pointer,size) pair. | 79 // Construct an ArrayView for a (pointer,size) pair. |
78 template <typename U> | 80 template <typename U> |
79 ArrayView(U* data, size_t size) | 81 ArrayView(U* data, size_t size) |
80 : data_(size == 0 ? nullptr : data), size_(size) { | 82 : data_(size == 0 ? nullptr : data), size_(size) { |
81 CheckInvariant(); | 83 CheckInvariant(); |
82 } | 84 } |
83 | 85 |
84 // Construct an ArrayView for an array. | 86 // Construct an ArrayView for an array. |
85 template <typename U, size_t N> | 87 template <typename U, size_t N> |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 private: | 126 private: |
125 // Invariant: !data_ iff size_ == 0. | 127 // Invariant: !data_ iff size_ == 0. |
126 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); } | 128 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); } |
127 T* data_; | 129 T* data_; |
128 size_t size_; | 130 size_t size_; |
129 }; | 131 }; |
130 | 132 |
131 } // namespace rtc | 133 } // namespace rtc |
132 | 134 |
133 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ | 135 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ |
OLD | NEW |