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

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

Issue 1717273002: Added functional variants of Buffer::SetData and Buffer::AppendData. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « no previous file | webrtc/base/buffer_unittest.cc » ('j') | webrtc/base/buffer_unittest.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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_BUFFER_H_ 11 #ifndef WEBRTC_BASE_BUFFER_H_
12 #define WEBRTC_BASE_BUFFER_H_ 12 #define WEBRTC_BASE_BUFFER_H_
13 13
14 #include <algorithm> // std::swap (pre-C++11)
15 #include <cassert> 14 #include <cassert>
16 #include <cstring> 15 #include <cstring>
17 #include <memory> 16 #include <memory>
18 #include <utility> // std::swap (C++11 and later) 17 #include <utility>
19 18
19 #include "webrtc/base/array_view.h"
20 #include "webrtc/base/constructormagic.h" 20 #include "webrtc/base/constructormagic.h"
21 #include "webrtc/base/deprecation.h" 21 #include "webrtc/base/deprecation.h"
22 22
23 namespace rtc { 23 namespace rtc {
24 24
25 namespace internal { 25 namespace internal {
26 26
27 // (Internal; please don't use outside this file.) ByteType<T>::t is int if T 27 // (Internal; please don't use outside this file.) ByteType<T>::t is int if T
28 // is uint8_t, int8_t, or char; otherwise, it's a compilation error. Use like 28 // is uint8_t, int8_t, or char; otherwise, it's a compilation error. Use like
29 // this: 29 // this:
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 bool operator!=(const Buffer& buf) const { return !(*this == buf); } 120 bool operator!=(const Buffer& buf) const { return !(*this == buf); }
121 121
122 // Replace the contents of the buffer. Accepts the same types as the 122 // Replace the contents of the buffer. Accepts the same types as the
123 // constructors. 123 // constructors.
124 template <typename T, typename internal::ByteType<T>::t = 0> 124 template <typename T, typename internal::ByteType<T>::t = 0>
125 void SetData(const T* data, size_t size) { 125 void SetData(const T* data, size_t size) {
126 assert(IsConsistent()); 126 assert(IsConsistent());
127 size_ = 0; 127 size_ = 0;
128 AppendData(data, size); 128 AppendData(data, size);
129 } 129 }
130
kwiberg-webrtc 2016/02/22 13:27:26 The lack of a blank line here was intended to indi
ossu 2016/02/22 14:20:50 Alright. I'm not certain that it worked, though. A
kwiberg-webrtc 2016/02/22 18:23:10 OK, fair enough. But inset blank lines between all
130 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> 131 template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
131 void SetData(const T(&array)[N]) { 132 void SetData(const T(&array)[N]) {
132 SetData(array, N); 133 SetData(array, N);
133 } 134 }
134 void SetData(const Buffer& buf) { SetData(buf.data(), buf.size()); } 135 void SetData(const Buffer& buf) { SetData(buf.data(), buf.size()); }
135 136
kwiberg-webrtc 2016/02/22 13:27:26 Doc comment here?
ossu 2016/02/22 14:20:50 Acknowledged.
137 template <typename T = uint8_t, typename F,
138 typename internal::ByteType<T>::t = 0>
139 size_t SetData(size_t max_bytes, F&& setter) {
140 assert(IsConsistent());
tommi 2016/02/22 11:24:18 kwiberg - can we s/assert/RTC_DCHECK in this file?
kwiberg-webrtc 2016/02/22 13:27:27 I presume so. Not in this CL, though. Oskar, your
ossu 2016/02/22 14:20:50 Could IsConsistent() be replaced by (or complement
kwiberg-webrtc 2016/02/22 18:23:10 Yes. :-) RTC_DCHECK prints an error message that
141 size_ = 0;
142 return AppendData<T>(max_bytes, std::forward<F>(setter));
143 }
144
136 // Append data to the buffer. Accepts the same types as the constructors. 145 // Append data to the buffer. Accepts the same types as the constructors.
137 template <typename T, typename internal::ByteType<T>::t = 0> 146 template <typename T, typename internal::ByteType<T>::t = 0>
138 void AppendData(const T* data, size_t size) { 147 void AppendData(const T* data, size_t size) {
139 assert(IsConsistent()); 148 assert(IsConsistent());
140 const size_t new_size = size_ + size; 149 const size_t new_size = size_ + size;
141 EnsureCapacity(new_size); 150 EnsureCapacity(new_size);
142 std::memcpy(data_.get() + size_, data, size); 151 std::memcpy(data_.get() + size_, data, size);
143 size_ = new_size; 152 size_ = new_size;
144 assert(IsConsistent()); 153 assert(IsConsistent());
145 } 154 }
155
kwiberg-webrtc 2016/02/22 13:27:27 See previous comment about a blank line (at line 1
146 template <typename T, size_t N, typename internal::ByteType<T>::t = 0> 156 template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
147 void AppendData(const T(&array)[N]) { 157 void AppendData(const T(&array)[N]) {
148 AppendData(array, N); 158 AppendData(array, N);
149 } 159 }
150 void AppendData(const Buffer& buf) { AppendData(buf.data(), buf.size()); } 160 void AppendData(const Buffer& buf) { AppendData(buf.data(), buf.size()); }
151 161
kwiberg-webrtc 2016/02/22 13:27:27 Doc comment here?
ossu 2016/02/22 14:20:50 Acknowledged.
162 template <typename T = uint8_t, typename F,
163 typename internal::ByteType<T>::t = 0>
164 size_t AppendData(size_t max_bytes, F&& setter) {
165 assert(IsConsistent());
166 const size_t old_size = size_;
167 SetSize(old_size + max_bytes);
168 auto base_ptr = data<T>() + old_size;
tommi 2016/02/22 11:24:18 nit: when using auto, it's still good if you can e
ossu 2016/02/22 14:20:50 Makes sense. Though, looking at it now, T * is sho
169 size_t written_bytes =
170 setter(rtc::ArrayView<T>(base_ptr, max_bytes));
171
172 RTC_DCHECK_LE(written_bytes, max_bytes);
kwiberg-webrtc 2016/02/22 13:27:26 I'd argue this should be a CHECK instead of a DCHE
ossu 2016/02/22 14:20:50 Acknowledged.
173 size_ = old_size + written_bytes;
174 assert(IsConsistent());
175 return written_bytes;
kwiberg-webrtc 2016/02/22 13:27:27 Hmm. Why return this rather than, say, void?
ossu 2016/02/22 14:20:50 The number of bytes written is returned for purely
kwiberg-webrtc 2016/02/22 18:23:10 Hmm. It's very easy to set via a reference, since
176 }
177
152 // Sets the size of the buffer. If the new size is smaller than the old, the 178 // Sets the size of the buffer. If the new size is smaller than the old, the
153 // buffer contents will be kept but truncated; if the new size is greater, 179 // buffer contents will be kept but truncated; if the new size is greater,
154 // the existing contents will be kept and the new space will be 180 // the existing contents will be kept and the new space will be
155 // uninitialized. 181 // uninitialized.
156 void SetSize(size_t size) { 182 void SetSize(size_t size) {
157 EnsureCapacity(size); 183 EnsureCapacity(size);
158 size_ = size; 184 size_ = size;
159 } 185 }
160 186
161 // Ensure that the buffer size can be increased to at least capacity without 187 // Ensure that the buffer size can be increased to at least capacity without
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 } 246 }
221 247
222 size_t size_; 248 size_t size_;
223 size_t capacity_; 249 size_t capacity_;
224 std::unique_ptr<uint8_t[]> data_; 250 std::unique_ptr<uint8_t[]> data_;
225 }; 251 };
226 252
227 } // namespace rtc 253 } // namespace rtc
228 254
229 #endif // WEBRTC_BASE_BUFFER_H_ 255 #endif // WEBRTC_BASE_BUFFER_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/buffer_unittest.cc » ('j') | webrtc/base/buffer_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698