Chromium Code Reviews| Index: webrtc/base/array_view.h |
| diff --git a/webrtc/base/array_view.h b/webrtc/base/array_view.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..94a241038c8583cae0dea1ce0d0644570b135055 |
| --- /dev/null |
| +++ b/webrtc/base/array_view.h |
| @@ -0,0 +1,80 @@ |
| +/* |
| + * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#ifndef WEBRTC_BASE_ARRAY_VIEW_H_ |
| +#define WEBRTC_BASE_ARRAY_VIEW_H_ |
| + |
| +#include <vector> |
| + |
| +#include "webrtc/base/checks.h" |
| + |
| +namespace rtc { |
| + |
| +// Keeps track of an array (a pointer and a size) that it doesn't own. |
| +// ArrayView objects are immutable except for assignment, and small enough to |
| +// be cheaply passed by value. |
| +// |
| +// Note that ArrayView<T> and ArrayView<const T> are distinct types; this is |
| +// how you would represent mutable and unmutable views of an array. |
| +template <typename T> |
| +class ArrayView final { |
| + public: |
| + // Construct an empty ArrayView. |
| + ArrayView() : ArrayView(static_cast<T*>(nullptr), 0) {} |
|
the sun
2015/10/21 14:06:34
Is the cast really necessary?
kwiberg-webrtc
2015/10/22 11:35:33
Yes. I get
candidate template ignored: could no
|
| + |
| + // Construct an ArrayView for a (pointer,size) pair. |
| + template <typename U> |
| + ArrayView(U* data, size_t size) : data_(data), size_(size) { |
| + // Check invariant: !data_ iff size_ == 0. |
| + RTC_DCHECK_EQ(!data_, size_ == 0); |
| + } |
| + |
| + // Construct an ArrayView for an array. |
| + template <typename U, size_t N> |
| + ArrayView(U (&array)[N]) : ArrayView(array + 0, N) {} |
|
mgraczyk
2015/10/22 00:12:26
Why array + 0 instead of &array[0]?
kwiberg-webrtc
2015/10/22 11:35:33
It seemed simpler: it's one operation instead of t
|
| + |
| + // Construct an ArrayView for any type U that has a size() method whose |
| + // return value converts implicitly to size_t, and a data() method whose |
| + // return value converts implicitly to T*. In particular, this means we allow |
| + // conversion from ArrayView<T> to ArrayView<const T>, but not the other way |
| + // around. Other allowed conversions include std::vector<T> to ArrayView<T> |
| + // and rtc::Buffer to ArrayView<uint8_t>. |
| + template <typename U> |
| + ArrayView(U& u) : ArrayView(u.data(), u.size()) {} |
| + // TODO(kwiberg): Remove the special case for std::vector (and the include of |
| + // <vector>); it is handled by the general case in C++11, where std::vector |
| + // has a data() method. |
| + template <typename U> |
| + ArrayView(std::vector<U>& u) |
| + : ArrayView(u.empty() ? nullptr : &u[0], u.size()) {} |
| + |
| + // Indexing, size, and iteration. These allow mutation even if the ArrayView |
| + // is const, because the ArrayView doesn't own the array. (To prevent |
| + // mutation, use ArrayView<const T>.) |
| + size_t size() const { return size_; } |
| + T* data() const { return data_; } |
|
the sun
2015/10/21 14:06:34
RTC_DCHECK(data_ != nullptr)
here and below
+ deat
kwiberg-webrtc
2015/10/22 11:35:33
We already check in the constructor that data_ is
the sun
2015/10/22 12:02:49
I think we still need checks, at the very least in
kwiberg-webrtc
2015/10/22 13:07:00
Checks in the iterators would take some work. Curr
|
| + T& operator[](size_t idx) const { |
| + RTC_DCHECK_LT(idx, size_); |
| + return data_[idx]; |
| + } |
| + T* begin() const { return data_; } |
| + T* end() const { return data_ + size_; } |
| + const T* cbegin() const { return data_; } |
| + const T* cend() const { return data_ + size_; } |
| + |
| + private: |
| + // Invariant: !data_ iff size_ == 0. |
| + T* data_; |
| + size_t size_; |
| +}; |
| + |
| +} // namespace rtc |
| + |
| +#endif // WEBRTC_BASE_ARRAY_VIEW_H_ |