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

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

Issue 1470843003: Remove the special case for std::vector in rtc::ArrayView (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <vector>
15
16 #include "webrtc/base/checks.h" 14 #include "webrtc/base/checks.h"
17 15
18 namespace rtc { 16 namespace rtc {
19 17
20 // Keeps track of an array (a pointer and a size) that it doesn't own. 18 // 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 19 // ArrayView objects are immutable except for assignment, and small enough to
22 // be cheaply passed by value. 20 // be cheaply passed by value.
23 // 21 //
24 // Note that ArrayView<T> and ArrayView<const T> are distinct types; this is 22 // 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. 23 // how you would represent mutable and unmutable views of an array.
(...skipping 17 matching lines...) Expand all
43 // Construct an ArrayView for any type U that has a size() method whose 41 // 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 42 // 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 43 // 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 44 // 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> 45 // around. Other allowed conversions include std::vector<T> to ArrayView<T>
48 // or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and 46 // or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and
49 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as 47 // rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as
50 // std::vector). 48 // std::vector).
51 template <typename U> 49 template <typename U>
52 ArrayView(U& u) : ArrayView(u.data(), u.size()) {} 50 ArrayView(U& u) : ArrayView(u.data(), u.size()) {}
53 // TODO(kwiberg): Remove the special case for std::vector (and the include of
54 // <vector>); it is handled by the general case in C++11, since std::vector
55 // has a data() method there.
56 template <typename U>
57 ArrayView(std::vector<U>& u)
58 : ArrayView(u.empty() ? nullptr : &u[0], u.size()) {}
59 51
60 // Indexing, size, and iteration. These allow mutation even if the ArrayView 52 // Indexing, size, and iteration. These allow mutation even if the ArrayView
61 // is const, because the ArrayView doesn't own the array. (To prevent 53 // is const, because the ArrayView doesn't own the array. (To prevent
62 // mutation, use ArrayView<const T>.) 54 // mutation, use ArrayView<const T>.)
63 size_t size() const { return size_; } 55 size_t size() const { return size_; }
64 bool empty() const { return size_ == 0; } 56 bool empty() const { return size_ == 0; }
65 T* data() const { return data_; } 57 T* data() const { return data_; }
66 T& operator[](size_t idx) const { 58 T& operator[](size_t idx) const {
67 RTC_DCHECK_LT(idx, size_); 59 RTC_DCHECK_LT(idx, size_);
68 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant. 60 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant.
(...skipping 16 matching lines...) Expand all
85 private: 77 private:
86 // Invariant: !data_ iff size_ == 0. 78 // Invariant: !data_ iff size_ == 0.
87 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); } 79 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); }
88 T* data_; 80 T* data_;
89 size_t size_; 81 size_t size_;
90 }; 82 };
91 83
92 } // namespace rtc 84 } // namespace rtc
93 85
94 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ 86 #endif // WEBRTC_BASE_ARRAY_VIEW_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698