2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2004 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef WEBRTC_BASE_BUFFER_H_
|
|
|
|
|
#define WEBRTC_BASE_BUFFER_H_
|
|
|
|
|
|
2016-06-16 15:54:44 +02:00
|
|
|
#include <algorithm>
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
#include <cstring>
|
2016-02-11 13:36:43 -08:00
|
|
|
#include <memory>
|
2016-04-29 08:00:22 -07:00
|
|
|
#include <type_traits>
|
2016-02-24 01:05:56 -08:00
|
|
|
#include <utility>
|
2016-01-12 07:24:19 -08:00
|
|
|
|
2016-02-24 01:05:56 -08:00
|
|
|
#include "webrtc/base/array_view.h"
|
|
|
|
|
#include "webrtc/base/checks.h"
|
2016-09-05 07:46:20 -07:00
|
|
|
#include "webrtc/base/type_traits.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
namespace internal {
|
|
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
// (Internal; please don't use outside this file.) Determines if elements of
|
|
|
|
|
// type U are compatible with a BufferT<T>. For most types, we just ignore
|
|
|
|
|
// 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
|
|
|
|
|
// uint8_t) are compatible with each other. (Note: We aim to get rid of this
|
|
|
|
|
// behavior, and treat all types the same.)
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
|
struct BufferCompat {
|
|
|
|
|
static constexpr bool value =
|
|
|
|
|
!std::is_volatile<U>::value &&
|
|
|
|
|
((std::is_integral<T>::value && sizeof(T) == 1)
|
|
|
|
|
? (std::is_integral<U>::value && sizeof(U) == 1)
|
|
|
|
|
: (std::is_same<T, typename std::remove_const<U>::type>::value));
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
// Basic buffer class, can be grown and shrunk dynamically.
|
2016-04-29 08:00:22 -07:00
|
|
|
// Unlike std::string/vector, does not initialize data when increasing size.
|
|
|
|
|
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");
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
public:
|
2016-04-29 08:00:22 -07:00
|
|
|
// An empty BufferT.
|
|
|
|
|
BufferT() : size_(0), capacity_(0), data_(nullptr) {
|
|
|
|
|
RTC_DCHECK(IsConsistent());
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
// Disable copy construction and copy assignment, since copying a buffer is
|
|
|
|
|
// expensive enough that we want to force the user to be explicit about it.
|
|
|
|
|
BufferT(const BufferT&) = delete;
|
|
|
|
|
BufferT& operator=(const BufferT&) = delete;
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
BufferT(BufferT&& buf)
|
|
|
|
|
: size_(buf.size()),
|
|
|
|
|
capacity_(buf.capacity()),
|
|
|
|
|
data_(std::move(buf.data_)) {
|
|
|
|
|
RTC_DCHECK(IsConsistent());
|
|
|
|
|
buf.OnMovedFrom();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Construct a buffer with the specified number of uninitialized elements.
|
|
|
|
|
explicit BufferT(size_t size) : BufferT(size, size) {}
|
|
|
|
|
|
|
|
|
|
BufferT(size_t size, size_t capacity)
|
|
|
|
|
: size_(size),
|
|
|
|
|
capacity_(std::max(size, capacity)),
|
|
|
|
|
data_(new T[capacity_]) {
|
|
|
|
|
RTC_DCHECK(IsConsistent());
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
// 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.
|
|
|
|
|
template <typename U,
|
|
|
|
|
size_t N,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
BufferT(U (&array)[N]) : BufferT(array, N) {}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
// byte-sized integer U.
|
|
|
|
|
template <typename U = T,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
const U* data() const {
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
2016-04-29 08:00:22 -07:00
|
|
|
return reinterpret_cast<U*>(data_.get());
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
}
|
2016-02-24 01:05:56 -08:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
template <typename U = T,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
U* data() {
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
2016-04-29 08:00:22 -07:00
|
|
|
return reinterpret_cast<U*>(data_.get());
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2016-08-31 08:40:04 -07:00
|
|
|
bool empty() const {
|
|
|
|
|
RTC_DCHECK(IsConsistent());
|
|
|
|
|
return size_ == 0;
|
|
|
|
|
}
|
|
|
|
|
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
size_t size() const {
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
return size_;
|
|
|
|
|
}
|
2016-02-24 01:05:56 -08:00
|
|
|
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
size_t capacity() const {
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
return capacity_;
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
BufferT& operator=(BufferT&& buf) {
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
|
|
|
|
RTC_DCHECK(buf.IsConsistent());
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
size_ = buf.size_;
|
|
|
|
|
capacity_ = buf.capacity_;
|
2015-12-17 03:04:15 -08:00
|
|
|
data_ = std::move(buf.data_);
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
buf.OnMovedFrom();
|
2014-05-13 18:00:26 +00:00
|
|
|
return *this;
|
|
|
|
|
}
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
bool operator==(const BufferT& buf) const {
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
2016-04-29 08:00:22 -07:00
|
|
|
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;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
bool operator!=(const BufferT& buf) const { return !(*this == buf); }
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
T& operator[](size_t index) {
|
2016-02-29 09:36:37 -08:00
|
|
|
RTC_DCHECK_LT(index, size_);
|
|
|
|
|
return data()[index];
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
T operator[](size_t index) const {
|
2016-02-29 09:36:37 -08:00
|
|
|
RTC_DCHECK_LT(index, size_);
|
|
|
|
|
return data()[index];
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 01:05:56 -08:00
|
|
|
// The SetData functions replace the contents of the buffer. They accept the
|
|
|
|
|
// same input types as the constructors.
|
2016-04-29 08:00:22 -07:00
|
|
|
template <typename U,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
void SetData(const U* data, size_t size) {
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
size_ = 0;
|
|
|
|
|
AppendData(data, size);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
2016-02-24 01:05:56 -08:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
template <typename U,
|
|
|
|
|
size_t N,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
void SetData(const U (&array)[N]) {
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
SetData(array, N);
|
|
|
|
|
}
|
2016-02-24 01:05:56 -08:00
|
|
|
|
2016-09-05 07:46:20 -07:00
|
|
|
template <typename W,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
HasDataAndSize<const W, const T>::value>::type* = nullptr>
|
|
|
|
|
void SetData(const W& w) {
|
|
|
|
|
SetData(w.data(), w.size());
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
// Replace the data in the buffer with at most |max_elements| of data, using
|
|
|
|
|
// the function |setter|, which should have the following signature:
|
|
|
|
|
// size_t setter(ArrayView<U> view)
|
2016-02-24 01:05:56 -08:00
|
|
|
// |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
|
2016-04-29 08:00:22 -07:00
|
|
|
// return the number of elements actually written. This number must be <=
|
|
|
|
|
// |max_elements|.
|
|
|
|
|
template <typename U = T,
|
|
|
|
|
typename F,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
size_t SetData(size_t max_elements, F&& setter) {
|
2016-02-24 01:05:56 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
|
|
|
|
size_ = 0;
|
2016-04-29 08:00:22 -07:00
|
|
|
return AppendData<U>(max_elements, std::forward<F>(setter));
|
2016-02-24 01:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
// The AppendData functions add data to the end of the buffer. They accept
|
2016-02-24 01:05:56 -08:00
|
|
|
// the same input types as the constructors.
|
2016-04-29 08:00:22 -07:00
|
|
|
template <typename U,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
void AppendData(const U* data, size_t size) {
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
const size_t new_size = size_ + size;
|
2016-06-20 04:47:39 -07:00
|
|
|
EnsureCapacityWithHeadroom(new_size, true);
|
2016-04-29 08:00:22 -07:00
|
|
|
static_assert(sizeof(T) == sizeof(U), "");
|
|
|
|
|
std::memcpy(data_.get() + size_, data, size * sizeof(U));
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
size_ = new_size;
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
2016-02-24 01:05:56 -08:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
template <typename U,
|
|
|
|
|
size_t N,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
void AppendData(const U (&array)[N]) {
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
AppendData(array, N);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
2016-02-24 01:05:56 -08:00
|
|
|
|
2016-09-05 07:46:20 -07:00
|
|
|
template <typename W,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
HasDataAndSize<const W, const T>::value>::type* = nullptr>
|
|
|
|
|
void AppendData(const W& w) {
|
|
|
|
|
AppendData(w.data(), w.size());
|
|
|
|
|
}
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
|
2016-06-02 02:43:32 -07:00
|
|
|
template <typename U,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
void AppendData(const U& item) {
|
|
|
|
|
AppendData(&item, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
// Append at most |max_elements| to the end of the buffer, using the function
|
|
|
|
|
// |setter|, which should have the following signature:
|
|
|
|
|
// size_t setter(ArrayView<U> view)
|
2016-02-24 01:05:56 -08:00
|
|
|
// |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
|
2016-04-29 08:00:22 -07:00
|
|
|
// return the number of elements actually written. This number must be <=
|
|
|
|
|
// |max_elements|.
|
|
|
|
|
template <typename U = T,
|
|
|
|
|
typename F,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
internal::BufferCompat<T, U>::value>::type* = nullptr>
|
|
|
|
|
size_t AppendData(size_t max_elements, F&& setter) {
|
2016-02-24 01:05:56 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
|
|
|
|
const size_t old_size = size_;
|
2016-04-29 08:00:22 -07:00
|
|
|
SetSize(old_size + max_elements);
|
|
|
|
|
U* base_ptr = data<U>() + old_size;
|
|
|
|
|
size_t written_elements = setter(rtc::ArrayView<U>(base_ptr, max_elements));
|
2016-02-24 01:05:56 -08:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
RTC_CHECK_LE(written_elements, max_elements);
|
|
|
|
|
size_ = old_size + written_elements;
|
2016-02-24 01:05:56 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
2016-04-29 08:00:22 -07:00
|
|
|
return written_elements;
|
2016-02-24 01:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
// Sets the size of the buffer. If the new size is smaller than the old, the
|
|
|
|
|
// buffer contents will be kept but truncated; if the new size is greater,
|
|
|
|
|
// the existing contents will be kept and the new space will be
|
|
|
|
|
// uninitialized.
|
2015-03-24 09:19:06 +00:00
|
|
|
void SetSize(size_t size) {
|
2016-06-20 04:47:39 -07:00
|
|
|
EnsureCapacityWithHeadroom(size, true);
|
2015-03-24 09:19:06 +00:00
|
|
|
size_ = size;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
|
|
|
|
|
// Ensure that the buffer size can be increased to at least capacity without
|
|
|
|
|
// further reallocation. (Of course, this operation might need to reallocate
|
|
|
|
|
// the buffer.)
|
|
|
|
|
void EnsureCapacity(size_t capacity) {
|
2016-06-20 04:47:39 -07:00
|
|
|
// Don't allocate extra headroom, since the user is asking for a specific
|
|
|
|
|
// capacity.
|
|
|
|
|
EnsureCapacityWithHeadroom(capacity, false);
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-19 02:38:32 -08:00
|
|
|
// Resets the buffer to zero size without altering capacity. Works even if the
|
|
|
|
|
// buffer has been moved from.
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
void Clear() {
|
|
|
|
|
size_ = 0;
|
2016-03-01 01:57:38 -08:00
|
|
|
RTC_DCHECK(IsConsistent());
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Swaps two buffers. Also works for buffers that have been moved from.
|
2016-04-29 08:00:22 -07:00
|
|
|
friend void swap(BufferT& a, BufferT& b) {
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
using std::swap;
|
|
|
|
|
swap(a.size_, b.size_);
|
|
|
|
|
swap(a.capacity_, b.capacity_);
|
|
|
|
|
swap(a.data_, b.data_);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
private:
|
2016-06-20 04:47:39 -07:00
|
|
|
void EnsureCapacityWithHeadroom(size_t capacity, bool extra_headroom) {
|
|
|
|
|
RTC_DCHECK(IsConsistent());
|
|
|
|
|
if (capacity <= capacity_)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// If the caller asks for extra headroom, ensure that the new capacity is
|
|
|
|
|
// >= 1.5 times the old capacity. Any constant > 1 is sufficient to prevent
|
|
|
|
|
// quadratic behavior; as to why we pick 1.5 in particular, see
|
|
|
|
|
// https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md and
|
|
|
|
|
// http://www.gahcep.com/cpp-internals-stl-vector-part-1/.
|
|
|
|
|
const size_t new_capacity =
|
|
|
|
|
extra_headroom ? std::max(capacity, capacity_ + capacity_ / 2)
|
|
|
|
|
: capacity;
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<T[]> new_data(new T[new_capacity]);
|
|
|
|
|
std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T));
|
|
|
|
|
data_ = std::move(new_data);
|
|
|
|
|
capacity_ = new_capacity;
|
|
|
|
|
RTC_DCHECK(IsConsistent());
|
|
|
|
|
}
|
|
|
|
|
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
// Precondition for all methods except Clear and the destructor.
|
|
|
|
|
// Postcondition for all methods except move construction and move
|
|
|
|
|
// assignment, which leave the moved-from object in a possibly inconsistent
|
|
|
|
|
// state.
|
|
|
|
|
bool IsConsistent() const {
|
|
|
|
|
return (data_ || capacity_ == 0) && capacity_ >= size_;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
rtc::Buffer improvements
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}
2015-04-20 14:03:07 +02:00
|
|
|
// Called when *this has been moved from. Conceptually it's a no-op, but we
|
|
|
|
|
// can mutate the state slightly to help subsequent sanity checks catch bugs.
|
|
|
|
|
void OnMovedFrom() {
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
|
// Make *this consistent and empty. Shouldn't be necessary, but better safe
|
|
|
|
|
// than sorry.
|
|
|
|
|
size_ = 0;
|
|
|
|
|
capacity_ = 0;
|
|
|
|
|
#else
|
|
|
|
|
// Ensure that *this is always inconsistent, to provoke bugs.
|
|
|
|
|
size_ = 1;
|
|
|
|
|
capacity_ = 0;
|
|
|
|
|
#endif
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-24 09:19:06 +00:00
|
|
|
size_t size_;
|
2014-05-13 18:00:26 +00:00
|
|
|
size_t capacity_;
|
2016-04-29 08:00:22 -07:00
|
|
|
std::unique_ptr<T[]> data_;
|
2014-05-13 18:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
// By far the most common sort of buffer.
|
|
|
|
|
using Buffer = BufferT<uint8_t>;
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
} // namespace rtc
|
|
|
|
|
|
|
|
|
|
#endif // WEBRTC_BASE_BUFFER_H_
|