
1. Constructors, SetData(), and AppendData() now accept uint8_t*, int8_t*, and char*. Previously, they accepted void*, meaning that any kind of pointer was accepted. I think requiring an explicit cast in cases where the input array isn't already of a byte-sized type is a better compromise between convenience and safety. 2. data() can now return a uint8_t* instead of a char*, which seems more appropriate for a byte array, and is harder to mix up with zero-terminated C strings. data<int8_t>() is also available so that callers that want that type instead won't have to cast, as is data<char>() (which remains the default until all existing callers have been fixed). 3. Constructors, SetData(), and AppendData() now accept arrays natively, not just decayed to pointers. The advantage of this is that callers don't have to pass the size separately. 4. There are new constructors that allow setting size and capacity without initializing the array. Previously, this had to be done separately after construction. 5. Instead of TransferTo(), Buffer now supports swap(), and move construction and assignment, and has a Pass() method that works just like std::move(). (The Pass method is modeled after scoped_ptr::Pass().) R=jmarusic@webrtc.org, tommi@webrtc.org Review URL: https://webrtc-codereview.appspot.com/42989004 Cr-Commit-Position: refs/heads/master@{#9033}
44 lines
1.1 KiB
C++
44 lines
1.1 KiB
C++
/*
|
|
* 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.
|
|
*/
|
|
|
|
#include "webrtc/base/buffer.h"
|
|
|
|
#include <cassert>
|
|
|
|
namespace rtc {
|
|
|
|
Buffer::Buffer() : size_(0), capacity_(0), data_(nullptr) {
|
|
assert(IsConsistent());
|
|
}
|
|
|
|
Buffer::Buffer(const Buffer& buf) : Buffer(buf.data(), buf.size()) {
|
|
}
|
|
|
|
Buffer::Buffer(Buffer&& buf)
|
|
: size_(buf.size()), capacity_(buf.capacity()), data_(buf.data_.Pass()) {
|
|
assert(IsConsistent());
|
|
buf.OnMovedFrom();
|
|
}
|
|
|
|
Buffer::Buffer(size_t size) : Buffer(size, size) {
|
|
}
|
|
|
|
Buffer::Buffer(size_t size, size_t capacity)
|
|
: size_(size),
|
|
capacity_(std::max(size, capacity)),
|
|
data_(new uint8_t[capacity_]) {
|
|
assert(IsConsistent());
|
|
}
|
|
|
|
// Note: The destructor works even if the buffer has been moved from.
|
|
Buffer::~Buffer() = default;
|
|
|
|
}; // namespace rtc
|