Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(329)

Side by Side Diff: webrtc/base/array_view.h

Issue 1408403002: Introduce rtc::ArrayView<T>, which keeps track of an array that it doesn't own (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Improve tests Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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) : data_(data), size_(size) {
35 // Check invariant: !data_ iff size_ == 0.
36 RTC_DCHECK_EQ(!data_, size_ == 0);
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 // and rtc::Buffer to ArrayView<uint8_t>.
49 template <typename U>
50 ArrayView(U& u) : ArrayView(u.data(), u.size()) {}
51 // TODO(kwiberg): Remove the special case for std::vector (and the include of
52 // <vector>); it is handled by the general case in C++11, where std::vector
53 // has a data() method.
54 template <typename U>
55 ArrayView(std::vector<U>& u)
56 : ArrayView(u.empty() ? nullptr : &u[0], u.size()) {}
57
58 // Indexing, size, and iteration. These allow mutation even if the ArrayView
59 // is const, because the ArrayView doesn't own the array. (To prevent
60 // mutation, use ArrayView<const T>.)
the sun 2015/10/21 10:53:42 Why not add clearly non-mutating data() and operat
kwiberg-webrtc 2015/10/21 12:08:55 We discussed this face to face; the tl;dr is that
61 size_t size() const { return size_; }
62 T* data() const { return data_; }
63 T& operator[](size_t idx) const {
64 RTC_DCHECK_LT(idx, size_);
65 return data_[idx];
66 }
67 T* begin() const { return data_; }
68 T* end() const { return data_ + size_; }
69 const T* cbegin() const { return data_; }
70 const T* cend() const { return data_ + size_; }
71
72 private:
73 // Invariant: !data_ iff size_ == 0.
74 T* data_;
75 size_t size_;
76 };
77
78 } // namespace rtc
79
80 #endif // WEBRTC_BASE_ARRAY_VIEW_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698