Define rtc::BufferT, like rtc::Buffer but for any trivial type
And redefine rtc::Buffer as using Buffer = BufferT<uint8_t>; (In the long run, I'd like to remove the type alias and rename the template to just rtc::Buffer, but that requires all current users of Buffer to start saying Buffer<uint8_t> instead, and since Buffer is used in the API, we can't do that in one step.) The immediate reason for the new template is that we'd like to use BufferT<int16_t> in the AudioDecoder interface. BUG=webrtc:5801 Review-Url: https://codereview.webrtc.org/1929903002 Cr-Commit-Position: refs/heads/master@{#12564}
This commit is contained in:
@ -103,7 +103,6 @@ static_library("rtc_base_approved") {
|
|||||||
"bind.h",
|
"bind.h",
|
||||||
"bitbuffer.cc",
|
"bitbuffer.cc",
|
||||||
"bitbuffer.h",
|
"bitbuffer.h",
|
||||||
"buffer.cc",
|
|
||||||
"buffer.h",
|
"buffer.h",
|
||||||
"bufferqueue.cc",
|
"bufferqueue.cc",
|
||||||
"bufferqueue.h",
|
"bufferqueue.h",
|
||||||
|
@ -34,7 +34,6 @@
|
|||||||
'bind.h',
|
'bind.h',
|
||||||
'bitbuffer.cc',
|
'bitbuffer.cc',
|
||||||
'bitbuffer.h',
|
'bitbuffer.h',
|
||||||
'buffer.cc',
|
|
||||||
'buffer.h',
|
'buffer.h',
|
||||||
'bufferqueue.cc',
|
'bufferqueue.cc',
|
||||||
'bufferqueue.h',
|
'bufferqueue.h',
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 <algorithm>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
namespace rtc {
|
|
||||||
|
|
||||||
Buffer::Buffer() : size_(0), capacity_(0), data_(nullptr) {
|
|
||||||
RTC_DCHECK(IsConsistent());
|
|
||||||
}
|
|
||||||
|
|
||||||
Buffer::Buffer(Buffer&& buf)
|
|
||||||
: size_(buf.size()),
|
|
||||||
capacity_(buf.capacity()),
|
|
||||||
data_(std::move(buf.data_)) {
|
|
||||||
RTC_DCHECK(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_]) {
|
|
||||||
RTC_DCHECK(IsConsistent());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note: The destructor works even if the buffer has been moved from.
|
|
||||||
Buffer::~Buffer() = default;
|
|
||||||
|
|
||||||
}; // namespace rtc
|
|
@ -13,79 +13,114 @@
|
|||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "webrtc/base/array_view.h"
|
#include "webrtc/base/array_view.h"
|
||||||
#include "webrtc/base/checks.h"
|
#include "webrtc/base/checks.h"
|
||||||
#include "webrtc/base/constructormagic.h"
|
|
||||||
|
|
||||||
namespace rtc {
|
namespace rtc {
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
// (Internal; please don't use outside this file.) ByteType<T>::t is int if T
|
// (Internal; please don't use outside this file.) Determines if elements of
|
||||||
// is uint8_t, int8_t, or char; otherwise, it's a compilation error. Use like
|
// type U are compatible with a BufferT<T>. For most types, we just ignore
|
||||||
// this:
|
// top-level const and forbid top-level volatile and require T and U to be
|
||||||
//
|
// otherwise equal, but all byte-sized integers (notably char, int8_t, and
|
||||||
// template <typename T, typename ByteType<T>::t = 0>
|
// uint8_t) are compatible with each other. (Note: We aim to get rid of this
|
||||||
// void foo(T* x);
|
// behavior, and treat all types the same.)
|
||||||
//
|
template <typename T, typename U>
|
||||||
// to let foo<T> be defined only for byte-sized integers.
|
struct BufferCompat {
|
||||||
template <typename T>
|
static constexpr bool value =
|
||||||
struct ByteType {
|
!std::is_volatile<U>::value &&
|
||||||
private:
|
((std::is_integral<T>::value && sizeof(T) == 1)
|
||||||
static int F(uint8_t*);
|
? (std::is_integral<U>::value && sizeof(U) == 1)
|
||||||
static int F(int8_t*);
|
: (std::is_same<T, typename std::remove_const<U>::type>::value));
|
||||||
static int F(char*);
|
|
||||||
|
|
||||||
public:
|
|
||||||
using t = decltype(F(static_cast<T*>(nullptr)));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
// Basic buffer class, can be grown and shrunk dynamically.
|
// Basic buffer class, can be grown and shrunk dynamically.
|
||||||
// Unlike std::string/vector, does not initialize data when expanding capacity.
|
// Unlike std::string/vector, does not initialize data when increasing size.
|
||||||
class Buffer {
|
template <typename T>
|
||||||
|
class BufferT {
|
||||||
|
// We want T's destructor and default constructor to be trivial, i.e. perform
|
||||||
|
// no action, so that we don't have to touch the memory we allocate and
|
||||||
|
// deallocate. And we want T to be trivially copyable, so that we can copy T
|
||||||
|
// instances with std::memcpy. This is precisely the definition of a trivial
|
||||||
|
// type.
|
||||||
|
static_assert(std::is_trivial<T>::value, "T must be a trivial type.");
|
||||||
|
|
||||||
|
// This class relies heavily on being able to mutate its data.
|
||||||
|
static_assert(!std::is_const<T>::value, "T may not be const");
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Buffer(); // An empty buffer.
|
// An empty BufferT.
|
||||||
Buffer(Buffer&& buf); // Move contents from an existing buffer.
|
BufferT() : size_(0), capacity_(0), data_(nullptr) {
|
||||||
|
RTC_DCHECK(IsConsistent());
|
||||||
|
}
|
||||||
|
|
||||||
// Construct a buffer with the specified number of uninitialized bytes.
|
// Disable copy construction and copy assignment, since copying a buffer is
|
||||||
explicit Buffer(size_t size);
|
// expensive enough that we want to force the user to be explicit about it.
|
||||||
Buffer(size_t size, size_t capacity);
|
BufferT(const BufferT&) = delete;
|
||||||
|
BufferT& operator=(const BufferT&) = delete;
|
||||||
|
|
||||||
// Construct a buffer and copy the specified number of bytes into it. The
|
BufferT(BufferT&& buf)
|
||||||
// source array may be (const) uint8_t*, int8_t*, or char*.
|
: size_(buf.size()),
|
||||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
capacity_(buf.capacity()),
|
||||||
Buffer(const T* data, size_t size)
|
data_(std::move(buf.data_)) {
|
||||||
: Buffer(data, size, size) {}
|
RTC_DCHECK(IsConsistent());
|
||||||
|
buf.OnMovedFrom();
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
// Construct a buffer with the specified number of uninitialized elements.
|
||||||
Buffer(const T* data, size_t size, size_t capacity)
|
explicit BufferT(size_t size) : BufferT(size, size) {}
|
||||||
: Buffer(size, capacity) {
|
|
||||||
std::memcpy(data_.get(), data, size);
|
BufferT(size_t size, size_t capacity)
|
||||||
|
: size_(size),
|
||||||
|
capacity_(std::max(size, capacity)),
|
||||||
|
data_(new T[capacity_]) {
|
||||||
|
RTC_DCHECK(IsConsistent());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construct a buffer and copy the specified number of elements into it.
|
||||||
|
template <typename U,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
BufferT(const U* data, size_t size) : BufferT(data, size, size) {}
|
||||||
|
|
||||||
|
template <typename U,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
BufferT(U* data, size_t size, size_t capacity) : BufferT(size, capacity) {
|
||||||
|
static_assert(sizeof(T) == sizeof(U), "");
|
||||||
|
std::memcpy(data_.get(), data, size * sizeof(U));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct a buffer from the contents of an array.
|
// Construct a buffer from the contents of an array.
|
||||||
template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
|
template <typename U,
|
||||||
Buffer(const T(&array)[N])
|
size_t N,
|
||||||
: Buffer(array, N) {}
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
BufferT(U (&array)[N]) : BufferT(array, N) {}
|
||||||
|
|
||||||
~Buffer();
|
// Get a pointer to the data. Just .data() will give you a (const) T*, but if
|
||||||
|
// T is a byte-sized integer, you may also use .data<U>() for any other
|
||||||
// Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
|
// byte-sized integer U.
|
||||||
// but you may also use .data<int8_t>() and .data<char>().
|
template <typename U = T,
|
||||||
template <typename T = uint8_t, typename internal::ByteType<T>::t = 0>
|
typename std::enable_if<
|
||||||
const T* data() const {
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
const U* data() const {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
return reinterpret_cast<T*>(data_.get());
|
return reinterpret_cast<U*>(data_.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T = uint8_t, typename internal::ByteType<T>::t = 0>
|
template <typename U = T,
|
||||||
T* data() {
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
U* data() {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
return reinterpret_cast<T*>(data_.get());
|
return reinterpret_cast<U*>(data_.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
@ -98,7 +133,7 @@ class Buffer {
|
|||||||
return capacity_;
|
return capacity_;
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer& operator=(Buffer&& buf) {
|
BufferT& operator=(BufferT&& buf) {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
RTC_DCHECK(buf.IsConsistent());
|
RTC_DCHECK(buf.IsConsistent());
|
||||||
size_ = buf.size_;
|
size_ = buf.size_;
|
||||||
@ -108,94 +143,120 @@ class Buffer {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Buffer& buf) const {
|
bool operator==(const BufferT& buf) const {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
return size_ == buf.size() && memcmp(data_.get(), buf.data(), size_) == 0;
|
if (size_ != buf.size_) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (std::is_integral<T>::value) {
|
||||||
|
// Optimization.
|
||||||
|
return std::memcmp(data_.get(), buf.data_.get(), size_ * sizeof(T)) == 0;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < size_; ++i) {
|
||||||
|
if (data_[i] != buf.data_[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const Buffer& buf) const { return !(*this == buf); }
|
bool operator!=(const BufferT& buf) const { return !(*this == buf); }
|
||||||
|
|
||||||
uint8_t& operator[](size_t index) {
|
T& operator[](size_t index) {
|
||||||
RTC_DCHECK_LT(index, size_);
|
RTC_DCHECK_LT(index, size_);
|
||||||
return data()[index];
|
return data()[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t operator[](size_t index) const {
|
T operator[](size_t index) const {
|
||||||
RTC_DCHECK_LT(index, size_);
|
RTC_DCHECK_LT(index, size_);
|
||||||
return data()[index];
|
return data()[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
// The SetData functions replace the contents of the buffer. They accept the
|
// The SetData functions replace the contents of the buffer. They accept the
|
||||||
// same input types as the constructors.
|
// same input types as the constructors.
|
||||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
template <typename U,
|
||||||
void SetData(const T* data, size_t size) {
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
void SetData(const U* data, size_t size) {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
size_ = 0;
|
size_ = 0;
|
||||||
AppendData(data, size);
|
AppendData(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
|
template <typename U,
|
||||||
void SetData(const T(&array)[N]) {
|
size_t N,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
void SetData(const U (&array)[N]) {
|
||||||
SetData(array, N);
|
SetData(array, N);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetData(const Buffer& buf) { SetData(buf.data(), buf.size()); }
|
void SetData(const BufferT& buf) { SetData(buf.data(), buf.size()); }
|
||||||
|
|
||||||
// Replace the data in the buffer with at most |max_bytes| of data, using the
|
// Replace the data in the buffer with at most |max_elements| of data, using
|
||||||
// function |setter|, which should have the following signature:
|
// the function |setter|, which should have the following signature:
|
||||||
// size_t setter(ArrayView<T> view)
|
// size_t setter(ArrayView<U> view)
|
||||||
// |setter| is given an appropriately typed ArrayView of the area in which to
|
// |setter| is given an appropriately typed ArrayView of the area in which to
|
||||||
// write the data (i.e. starting at the beginning of the buffer) and should
|
// write the data (i.e. starting at the beginning of the buffer) and should
|
||||||
// return the number of bytes actually written. This number must be <=
|
// return the number of elements actually written. This number must be <=
|
||||||
// |max_bytes|.
|
// |max_elements|.
|
||||||
template <typename T = uint8_t, typename F,
|
template <typename U = T,
|
||||||
typename internal::ByteType<T>::t = 0>
|
typename F,
|
||||||
size_t SetData(size_t max_bytes, F&& setter) {
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
size_t SetData(size_t max_elements, F&& setter) {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
size_ = 0;
|
size_ = 0;
|
||||||
return AppendData<T>(max_bytes, std::forward<F>(setter));
|
return AppendData<U>(max_elements, std::forward<F>(setter));
|
||||||
}
|
}
|
||||||
|
|
||||||
// The AppendData functions adds data to the end of the buffer. They accept
|
// The AppendData functions add data to the end of the buffer. They accept
|
||||||
// the same input types as the constructors.
|
// the same input types as the constructors.
|
||||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
template <typename U,
|
||||||
void AppendData(const T* data, size_t size) {
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
void AppendData(const U* data, size_t size) {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
const size_t new_size = size_ + size;
|
const size_t new_size = size_ + size;
|
||||||
EnsureCapacity(new_size);
|
EnsureCapacity(new_size);
|
||||||
std::memcpy(data_.get() + size_, data, size);
|
static_assert(sizeof(T) == sizeof(U), "");
|
||||||
|
std::memcpy(data_.get() + size_, data, size * sizeof(U));
|
||||||
size_ = new_size;
|
size_ = new_size;
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
|
template <typename U,
|
||||||
void AppendData(const T(&array)[N]) {
|
size_t N,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
void AppendData(const U (&array)[N]) {
|
||||||
AppendData(array, N);
|
AppendData(array, N);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AppendData(const Buffer& buf) { AppendData(buf.data(), buf.size()); }
|
void AppendData(const BufferT& buf) { AppendData(buf.data(), buf.size()); }
|
||||||
|
|
||||||
// Append at most |max_bytes| of data to the end of the buffer, using the
|
// Append at most |max_elements| to the end of the buffer, using the function
|
||||||
// function |setter|, which should have the following signature:
|
// |setter|, which should have the following signature:
|
||||||
// size_t setter(ArrayView<T> view)
|
// size_t setter(ArrayView<U> view)
|
||||||
// |setter| is given an appropriately typed ArrayView of the area in which to
|
// |setter| is given an appropriately typed ArrayView of the area in which to
|
||||||
// write the data (i.e. starting at the former end of the buffer) and should
|
// write the data (i.e. starting at the former end of the buffer) and should
|
||||||
// return the number of bytes actually written. This number must be <=
|
// return the number of elements actually written. This number must be <=
|
||||||
// |max_bytes|.
|
// |max_elements|.
|
||||||
template <typename T = uint8_t, typename F,
|
template <typename U = T,
|
||||||
typename internal::ByteType<T>::t = 0>
|
typename F,
|
||||||
size_t AppendData(size_t max_bytes, F&& setter) {
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
||||||
|
size_t AppendData(size_t max_elements, F&& setter) {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
const size_t old_size = size_;
|
const size_t old_size = size_;
|
||||||
SetSize(old_size + max_bytes);
|
SetSize(old_size + max_elements);
|
||||||
T *base_ptr = data<T>() + old_size;
|
U* base_ptr = data<U>() + old_size;
|
||||||
size_t written_bytes =
|
size_t written_elements = setter(rtc::ArrayView<U>(base_ptr, max_elements));
|
||||||
setter(rtc::ArrayView<T>(base_ptr, max_bytes));
|
|
||||||
|
|
||||||
RTC_CHECK_LE(written_bytes, max_bytes);
|
RTC_CHECK_LE(written_elements, max_elements);
|
||||||
size_ = old_size + written_bytes;
|
size_ = old_size + written_elements;
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
return written_bytes;
|
return written_elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sets the size of the buffer. If the new size is smaller than the old, the
|
// Sets the size of the buffer. If the new size is smaller than the old, the
|
||||||
@ -214,8 +275,8 @@ class Buffer {
|
|||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
if (capacity <= capacity_)
|
if (capacity <= capacity_)
|
||||||
return;
|
return;
|
||||||
std::unique_ptr<uint8_t[]> new_data(new uint8_t[capacity]);
|
std::unique_ptr<T[]> new_data(new T[capacity]);
|
||||||
std::memcpy(new_data.get(), data_.get(), size_);
|
std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T));
|
||||||
data_ = std::move(new_data);
|
data_ = std::move(new_data);
|
||||||
capacity_ = capacity;
|
capacity_ = capacity;
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
@ -229,7 +290,7 @@ class Buffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Swaps two buffers. Also works for buffers that have been moved from.
|
// Swaps two buffers. Also works for buffers that have been moved from.
|
||||||
friend void swap(Buffer& a, Buffer& b) {
|
friend void swap(BufferT& a, BufferT& b) {
|
||||||
using std::swap;
|
using std::swap;
|
||||||
swap(a.size_, b.size_);
|
swap(a.size_, b.size_);
|
||||||
swap(a.capacity_, b.capacity_);
|
swap(a.capacity_, b.capacity_);
|
||||||
@ -262,11 +323,12 @@ class Buffer {
|
|||||||
|
|
||||||
size_t size_;
|
size_t size_;
|
||||||
size_t capacity_;
|
size_t capacity_;
|
||||||
std::unique_ptr<uint8_t[]> data_;
|
std::unique_ptr<T[]> data_;
|
||||||
|
|
||||||
RTC_DISALLOW_COPY_AND_ASSIGN(Buffer);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// By far the most common sort of buffer.
|
||||||
|
using Buffer = BufferT<uint8_t>;
|
||||||
|
|
||||||
} // namespace rtc
|
} // namespace rtc
|
||||||
|
|
||||||
#endif // WEBRTC_BASE_BUFFER_H_
|
#endif // WEBRTC_BASE_BUFFER_H_
|
||||||
|
@ -11,8 +11,8 @@
|
|||||||
#include "webrtc/base/buffer.h"
|
#include "webrtc/base/buffer.h"
|
||||||
#include "webrtc/base/gunit.h"
|
#include "webrtc/base/gunit.h"
|
||||||
|
|
||||||
#include <algorithm> // std::swap (pre-C++11)
|
#include <type_traits>
|
||||||
#include <utility> // std::swap (C++11 and later)
|
#include <utility>
|
||||||
|
|
||||||
namespace rtc {
|
namespace rtc {
|
||||||
|
|
||||||
@ -301,4 +301,60 @@ TEST(BufferTest, TestBracketWrite) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, TestInt16) {
|
||||||
|
static constexpr int16_t test_data[] = {14, 15, 16, 17, 18};
|
||||||
|
BufferT<int16_t> buf(test_data);
|
||||||
|
EXPECT_EQ(buf.size(), 5u);
|
||||||
|
EXPECT_EQ(buf.capacity(), 5u);
|
||||||
|
EXPECT_NE(buf.data(), nullptr);
|
||||||
|
for (size_t i = 0; i != buf.size(); ++i) {
|
||||||
|
EXPECT_EQ(test_data[i], buf[i]);
|
||||||
|
}
|
||||||
|
BufferT<int16_t> buf2(test_data);
|
||||||
|
EXPECT_EQ(buf, buf2);
|
||||||
|
buf2[0] = 9;
|
||||||
|
EXPECT_NE(buf, buf2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, TestFloat) {
|
||||||
|
static constexpr float test_data[] = {14, 15, 16, 17, 18};
|
||||||
|
BufferT<float> buf;
|
||||||
|
EXPECT_EQ(buf.size(), 0u);
|
||||||
|
EXPECT_EQ(buf.capacity(), 0u);
|
||||||
|
EXPECT_EQ(buf.data(), nullptr);
|
||||||
|
buf.SetData(test_data);
|
||||||
|
EXPECT_EQ(buf.size(), 5u);
|
||||||
|
EXPECT_EQ(buf.capacity(), 5u);
|
||||||
|
EXPECT_NE(buf.data(), nullptr);
|
||||||
|
float* p1 = buf.data();
|
||||||
|
while (buf.data() == p1) {
|
||||||
|
buf.AppendData(test_data);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(buf.size(), buf.capacity());
|
||||||
|
EXPECT_GT(buf.size(), 5u);
|
||||||
|
EXPECT_EQ(buf.size() % 5, 0u);
|
||||||
|
EXPECT_NE(buf.data(), nullptr);
|
||||||
|
for (size_t i = 0; i != buf.size(); ++i) {
|
||||||
|
EXPECT_EQ(test_data[i % 5], buf[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, TestStruct) {
|
||||||
|
struct BloodStone {
|
||||||
|
bool blood;
|
||||||
|
const char* stone;
|
||||||
|
};
|
||||||
|
BufferT<BloodStone> buf(4);
|
||||||
|
EXPECT_EQ(buf.size(), 4u);
|
||||||
|
EXPECT_EQ(buf.capacity(), 4u);
|
||||||
|
EXPECT_NE(buf.data(), nullptr);
|
||||||
|
BufferT<BloodStone*> buf2(4);
|
||||||
|
for (size_t i = 0; i < buf2.size(); ++i) {
|
||||||
|
buf2[i] = &buf[i];
|
||||||
|
}
|
||||||
|
static const char kObsidian[] = "obsidian";
|
||||||
|
buf2[2]->stone = kObsidian;
|
||||||
|
EXPECT_EQ(kObsidian, buf[2].stone);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace rtc
|
} // namespace rtc
|
||||||
|
@ -36,10 +36,14 @@ class CopyOnWriteBuffer {
|
|||||||
|
|
||||||
// Construct a buffer and copy the specified number of bytes into it. The
|
// Construct a buffer and copy the specified number of bytes into it. The
|
||||||
// source array may be (const) uint8_t*, int8_t*, or char*.
|
// source array may be (const) uint8_t*, int8_t*, or char*.
|
||||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
template <typename T,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
CopyOnWriteBuffer(const T* data, size_t size)
|
CopyOnWriteBuffer(const T* data, size_t size)
|
||||||
: CopyOnWriteBuffer(data, size, size) {}
|
: CopyOnWriteBuffer(data, size, size) {}
|
||||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
template <typename T,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
|
CopyOnWriteBuffer(const T* data, size_t size, size_t capacity)
|
||||||
: CopyOnWriteBuffer(size, capacity) {
|
: CopyOnWriteBuffer(size, capacity) {
|
||||||
if (buffer_) {
|
if (buffer_) {
|
||||||
@ -48,22 +52,29 @@ class CopyOnWriteBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Construct a buffer from the contents of an array.
|
// Construct a buffer from the contents of an array.
|
||||||
template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
|
template <typename T,
|
||||||
CopyOnWriteBuffer(const T(&array)[N]) // NOLINT: runtime/explicit
|
size_t N,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
|
CopyOnWriteBuffer(const T (&array)[N]) // NOLINT: runtime/explicit
|
||||||
: CopyOnWriteBuffer(array, N) {}
|
: CopyOnWriteBuffer(array, N) {}
|
||||||
|
|
||||||
~CopyOnWriteBuffer();
|
~CopyOnWriteBuffer();
|
||||||
|
|
||||||
// Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
|
// Get a pointer to the data. Just .data() will give you a (const) uint8_t*,
|
||||||
// but you may also use .data<int8_t>() and .data<char>().
|
// but you may also use .data<int8_t>() and .data<char>().
|
||||||
template <typename T = uint8_t, typename internal::ByteType<T>::t = 0>
|
template <typename T = uint8_t,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
const T* data() const {
|
const T* data() const {
|
||||||
return cdata<T>();
|
return cdata<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get writable pointer to the data. This will create a copy of the underlying
|
// Get writable pointer to the data. This will create a copy of the underlying
|
||||||
// data if it is shared with other buffers.
|
// data if it is shared with other buffers.
|
||||||
template <typename T = uint8_t, typename internal::ByteType<T>::t = 0>
|
template <typename T = uint8_t,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
T* data() {
|
T* data() {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
if (!buffer_) {
|
if (!buffer_) {
|
||||||
@ -75,7 +86,9 @@ class CopyOnWriteBuffer {
|
|||||||
|
|
||||||
// Get const pointer to the data. This will not create a copy of the
|
// Get const pointer to the data. This will not create a copy of the
|
||||||
// underlying data if it is shared with other buffers.
|
// underlying data if it is shared with other buffers.
|
||||||
template <typename T = uint8_t, typename internal::ByteType<T>::t = 0>
|
template <typename T = uint8_t,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
T* cdata() const {
|
T* cdata() const {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
if (!buffer_) {
|
if (!buffer_) {
|
||||||
@ -137,7 +150,9 @@ class CopyOnWriteBuffer {
|
|||||||
|
|
||||||
// Replace the contents of the buffer. Accepts the same types as the
|
// Replace the contents of the buffer. Accepts the same types as the
|
||||||
// constructors.
|
// constructors.
|
||||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
template <typename T,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
void SetData(const T* data, size_t size) {
|
void SetData(const T* data, size_t size) {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
if (!buffer_ || !buffer_->HasOneRef()) {
|
if (!buffer_ || !buffer_->HasOneRef()) {
|
||||||
@ -149,8 +164,11 @@ class CopyOnWriteBuffer {
|
|||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
|
template <typename T,
|
||||||
void SetData(const T(&array)[N]) {
|
size_t N,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
|
void SetData(const T (&array)[N]) {
|
||||||
SetData(array, N);
|
SetData(array, N);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,7 +181,9 @@ class CopyOnWriteBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Append data to the buffer. Accepts the same types as the constructors.
|
// Append data to the buffer. Accepts the same types as the constructors.
|
||||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
template <typename T,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
void AppendData(const T* data, size_t size) {
|
void AppendData(const T* data, size_t size) {
|
||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
if (!buffer_) {
|
if (!buffer_) {
|
||||||
@ -178,8 +198,11 @@ class CopyOnWriteBuffer {
|
|||||||
RTC_DCHECK(IsConsistent());
|
RTC_DCHECK(IsConsistent());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, size_t N, typename internal::ByteType<T>::t = 0>
|
template <typename T,
|
||||||
void AppendData(const T(&array)[N]) {
|
size_t N,
|
||||||
|
typename std::enable_if<
|
||||||
|
internal::BufferCompat<uint8_t, T>::value>::type* = nullptr>
|
||||||
|
void AppendData(const T (&array)[N]) {
|
||||||
AppendData(array, N);
|
AppendData(array, N);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include "webrtc/api/rtpparameters.h"
|
#include "webrtc/api/rtpparameters.h"
|
||||||
#include "webrtc/base/basictypes.h"
|
#include "webrtc/base/basictypes.h"
|
||||||
|
#include "webrtc/base/buffer.h"
|
||||||
#include "webrtc/base/copyonwritebuffer.h"
|
#include "webrtc/base/copyonwritebuffer.h"
|
||||||
#include "webrtc/base/dscp.h"
|
#include "webrtc/base/dscp.h"
|
||||||
#include "webrtc/base/logging.h"
|
#include "webrtc/base/logging.h"
|
||||||
@ -34,7 +35,6 @@
|
|||||||
#include "webrtc/pc/audiomonitor.h"
|
#include "webrtc/pc/audiomonitor.h"
|
||||||
|
|
||||||
namespace rtc {
|
namespace rtc {
|
||||||
class Buffer;
|
|
||||||
class RateLimiter;
|
class RateLimiter;
|
||||||
class Timing;
|
class Timing;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user