rtc::Buffer: Remove backwards compatibility band-aids
This CL makes two changes to rtc::Buffer that have had to wait for
Chromium's use of it to be modernized:
1. Change default return type of rtc::Buffer::data() from char* to
uint8_t*. uint8_t is a more natural type for bytes, and won't
accidentally convert to a string. (Chromium previously expected
the default return type to be char, which is why
rtc::Buffer::data() initially got char as default return type in
9478437f, but that's been fixed now.)
2. Stop accepting void* inputs in constructors and methods. While
this is convenient, it's also dangerous since any pointer type
will implicitly convert to void*.
(This was previously committed (9e1a6d7c) but had to be reverted
(cbf09274) because Chromium on Android wasn't quite ready for it).
TBR=tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/47109004
Cr-Commit-Position: refs/heads/master@{#9132}
This commit is contained in:
@ -40,20 +40,6 @@ struct ByteType {
|
||||
using t = decltype(F(static_cast<T*>(nullptr)));
|
||||
};
|
||||
|
||||
// Deprecated: Accept void* in addition to the byte-sized types.
|
||||
// TODO(kwiberg): Remove once Chromium doesn't need this anymore.
|
||||
template <typename T>
|
||||
struct ByteTypeOrVoid {
|
||||
private:
|
||||
static int F(uint8_t*);
|
||||
static int F(int8_t*);
|
||||
static int F(char*);
|
||||
static int F(void*);
|
||||
|
||||
public:
|
||||
using t = decltype(F(static_cast<T*>(nullptr)));
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
// Basic buffer class, can be grown and shrunk dynamically.
|
||||
@ -70,10 +56,10 @@ class Buffer final {
|
||||
|
||||
// Construct a buffer and copy the specified number of bytes into it. The
|
||||
// source array may be (const) uint8_t*, int8_t*, or char*.
|
||||
template <typename T, typename internal::ByteTypeOrVoid<T>::t = 0>
|
||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
||||
Buffer(const T* data, size_t size)
|
||||
: Buffer(data, size, size) {}
|
||||
template <typename T, typename internal::ByteTypeOrVoid<T>::t = 0>
|
||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
||||
Buffer(const T* data, size_t size, size_t capacity)
|
||||
: Buffer(size, capacity) {
|
||||
std::memcpy(data_.get(), data, size);
|
||||
@ -86,15 +72,14 @@ class Buffer final {
|
||||
|
||||
~Buffer();
|
||||
|
||||
// Get a pointer to the data. Just .data() will give you a (const) char*,
|
||||
// but you may also use .data<int8_t>() and .data<uint8_t>().
|
||||
// TODO(kwiberg): Change default to uint8_t
|
||||
template <typename T = char, typename internal::ByteType<T>::t = 0>
|
||||
// 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>().
|
||||
template <typename T = uint8_t, typename internal::ByteType<T>::t = 0>
|
||||
const T* data() const {
|
||||
assert(IsConsistent());
|
||||
return reinterpret_cast<T*>(data_.get());
|
||||
}
|
||||
template <typename T = char, typename internal::ByteType<T>::t = 0>
|
||||
template <typename T = uint8_t, typename internal::ByteType<T>::t = 0>
|
||||
T* data() {
|
||||
assert(IsConsistent());
|
||||
return reinterpret_cast<T*>(data_.get());
|
||||
@ -133,7 +118,7 @@ class Buffer final {
|
||||
|
||||
// Replace the contents of the buffer. Accepts the same types as the
|
||||
// constructors.
|
||||
template <typename T, typename internal::ByteTypeOrVoid<T>::t = 0>
|
||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
||||
void SetData(const T* data, size_t size) {
|
||||
assert(IsConsistent());
|
||||
size_ = 0;
|
||||
@ -146,7 +131,7 @@ class Buffer final {
|
||||
void SetData(const Buffer& buf) { SetData(buf.data(), buf.size()); }
|
||||
|
||||
// Append data to the buffer. Accepts the same types as the constructors.
|
||||
template <typename T, typename internal::ByteTypeOrVoid<T>::t = 0>
|
||||
template <typename T, typename internal::ByteType<T>::t = 0>
|
||||
void AppendData(const T* data, size_t size) {
|
||||
assert(IsConsistent());
|
||||
const size_t new_size = size_ + size;
|
||||
|
||||
Reference in New Issue
Block a user