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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 // <vector>); it is handled by the general case in C++11, since std::vector | 54 // <vector>); it is handled by the general case in C++11, since std::vector |
55 // has a data() method there. | 55 // has a data() method there. |
56 template <typename U> | 56 template <typename U> |
57 ArrayView(std::vector<U>& u) | 57 ArrayView(std::vector<U>& u) |
58 : ArrayView(u.empty() ? nullptr : &u[0], u.size()) {} | 58 : ArrayView(u.empty() ? nullptr : &u[0], u.size()) {} |
59 | 59 |
60 // Indexing, size, and iteration. These allow mutation even if the ArrayView | 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 | 61 // is const, because the ArrayView doesn't own the array. (To prevent |
62 // mutation, use ArrayView<const T>.) | 62 // mutation, use ArrayView<const T>.) |
63 size_t size() const { return size_; } | 63 size_t size() const { return size_; } |
| 64 bool empty() const { return size_ == 0; } |
64 T* data() const { return data_; } | 65 T* data() const { return data_; } |
65 T& operator[](size_t idx) const { | 66 T& operator[](size_t idx) const { |
66 RTC_DCHECK_LT(idx, size_); | 67 RTC_DCHECK_LT(idx, size_); |
67 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant. | 68 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant. |
68 return data_[idx]; | 69 return data_[idx]; |
69 } | 70 } |
70 T* begin() const { return data_; } | 71 T* begin() const { return data_; } |
71 T* end() const { return data_ + size_; } | 72 T* end() const { return data_ + size_; } |
72 const T* cbegin() const { return data_; } | 73 const T* cbegin() const { return data_; } |
73 const T* cend() const { return data_ + size_; } | 74 const T* cend() const { return data_ + size_; } |
74 | 75 |
| 76 // 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 |
75 private: | 85 private: |
76 // Invariant: !data_ iff size_ == 0. | 86 // Invariant: !data_ iff size_ == 0. |
77 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); } | 87 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); } |
78 T* data_; | 88 T* data_; |
79 size_t size_; | 89 size_t size_; |
80 }; | 90 }; |
81 | 91 |
82 } // namespace rtc | 92 } // namespace rtc |
83 | 93 |
84 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ | 94 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ |
OLD | NEW |