rtc::Buffer: Let SetData and AppendData accept anything with .data() and .size()
In addition to setting or appending from another Buffer, which was already possible, this allows for e.g. std::vector and rtc::ArrayView arguments. Review-Url: https://codereview.webrtc.org/2293983002 Cr-Commit-Position: refs/heads/master@{#14073}
This commit is contained in:
parent
84c8528f1e
commit
d313403564
@ -171,6 +171,7 @@ rtc_static_library("rtc_base_approved") {
|
||||
"timeutils.cc",
|
||||
"timeutils.h",
|
||||
"trace_event.h",
|
||||
"type_traits.h",
|
||||
]
|
||||
|
||||
if (is_android) {
|
||||
|
||||
@ -11,37 +11,11 @@
|
||||
#ifndef WEBRTC_BASE_ARRAY_VIEW_H_
|
||||
#define WEBRTC_BASE_ARRAY_VIEW_H_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/base/type_traits.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
namespace internal {
|
||||
|
||||
// (Internal; please don't use outside this file.) Determines if the given
|
||||
// class has zero-argument .data() and .size() methods whose return values are
|
||||
// convertible to T* and size_t, respectively.
|
||||
template <typename DS, typename T>
|
||||
class HasDataAndSize {
|
||||
private:
|
||||
template <
|
||||
typename C,
|
||||
typename std::enable_if<
|
||||
std::is_convertible<decltype(std::declval<C>().data()), T*>::value &&
|
||||
std::is_convertible<decltype(std::declval<C>().size()),
|
||||
size_t>::value>::type* = nullptr>
|
||||
static int Test(int);
|
||||
|
||||
template <typename>
|
||||
static char Test(...);
|
||||
|
||||
public:
|
||||
static constexpr bool value = std::is_same<decltype(Test<DS>(0)), int>::value;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
// Many functions read from or write to arrays. The obvious way to do this is
|
||||
// to use two arguments, a pointer to the first element and an element count:
|
||||
//
|
||||
@ -122,9 +96,9 @@ class ArrayView final {
|
||||
// or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and
|
||||
// rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as
|
||||
// std::vector).
|
||||
template <typename U,
|
||||
typename std::enable_if<
|
||||
internal::HasDataAndSize<U, T>::value>::type* = nullptr>
|
||||
template <
|
||||
typename U,
|
||||
typename std::enable_if<HasDataAndSize<U, T>::value>::type* = nullptr>
|
||||
ArrayView(U& u) : ArrayView(u.data(), u.size()) {}
|
||||
|
||||
// Indexing, size, and iteration. These allow mutation even if the ArrayView
|
||||
|
||||
@ -21,41 +21,6 @@ namespace rtc {
|
||||
|
||||
namespace {
|
||||
|
||||
namespace test_has_data_and_size {
|
||||
|
||||
template <typename C, typename T>
|
||||
using DS = internal::HasDataAndSize<C, T>;
|
||||
|
||||
template <typename DR, typename SR>
|
||||
struct Test1 {
|
||||
DR data();
|
||||
SR size();
|
||||
};
|
||||
static_assert(DS<Test1<int*, int>, int>::value, "");
|
||||
static_assert(DS<Test1<int*, int>, const int>::value, "");
|
||||
static_assert(DS<Test1<const int*, int>, const int>::value, "");
|
||||
static_assert(!DS<Test1<const int*, int>, int>::value, ""); // Wrong const.
|
||||
static_assert(!DS<Test1<char*, size_t>, int>::value, ""); // Wrong ptr type.
|
||||
|
||||
struct Test2 {
|
||||
int* data;
|
||||
size_t size;
|
||||
};
|
||||
static_assert(!DS<Test2, int>::value, ""); // Because they aren't methods.
|
||||
|
||||
struct Test3 {
|
||||
int* data();
|
||||
};
|
||||
static_assert(!DS<Test3, int>::value, ""); // Because .size() is missing.
|
||||
|
||||
class Test4 {
|
||||
int* data();
|
||||
size_t size();
|
||||
};
|
||||
static_assert(!DS<Test4, int>::value, ""); // Because methods are private.
|
||||
|
||||
} // namespace test_has_data_and_size
|
||||
|
||||
template <typename T>
|
||||
void Call(ArrayView<T>) {}
|
||||
|
||||
|
||||
@ -103,6 +103,7 @@
|
||||
'timeutils.cc',
|
||||
'timeutils.h',
|
||||
'trace_event.h',
|
||||
'type_traits.h',
|
||||
],
|
||||
'conditions': [
|
||||
['os_posix==1', {
|
||||
|
||||
@ -19,6 +19,7 @@
|
||||
|
||||
#include "webrtc/base/array_view.h"
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/base/type_traits.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
@ -197,7 +198,12 @@ class BufferT {
|
||||
SetData(array, N);
|
||||
}
|
||||
|
||||
void SetData(const BufferT& buf) { SetData(buf.data(), buf.size()); }
|
||||
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());
|
||||
}
|
||||
|
||||
// Replace the data in the buffer with at most |max_elements| of data, using
|
||||
// the function |setter|, which should have the following signature:
|
||||
@ -239,7 +245,12 @@ class BufferT {
|
||||
AppendData(array, N);
|
||||
}
|
||||
|
||||
void AppendData(const BufferT& buf) { AppendData(buf.data(), buf.size()); }
|
||||
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());
|
||||
}
|
||||
|
||||
template <typename U,
|
||||
typename std::enable_if<
|
||||
|
||||
@ -9,6 +9,8 @@
|
||||
*/
|
||||
|
||||
#include "webrtc/base/buffer.h"
|
||||
|
||||
#include "webrtc/base/array_view.h"
|
||||
#include "webrtc/base/gunit.h"
|
||||
|
||||
#include <type_traits>
|
||||
@ -74,6 +76,11 @@ TEST(BufferTest, TestSetData) {
|
||||
EXPECT_EQ(buf.capacity(), 7u * 3 / 2);
|
||||
EXPECT_FALSE(buf.empty());
|
||||
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 9));
|
||||
Buffer buf2;
|
||||
buf2.SetData(buf);
|
||||
EXPECT_EQ(buf.size(), 9u);
|
||||
EXPECT_EQ(buf.capacity(), 7u * 3 / 2);
|
||||
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 9));
|
||||
}
|
||||
|
||||
TEST(BufferTest, TestAppendData) {
|
||||
@ -81,6 +88,26 @@ TEST(BufferTest, TestAppendData) {
|
||||
buf.AppendData(kTestData + 10, 2);
|
||||
const int8_t exp[] = {0x4, 0x5, 0x6, 0xa, 0xb};
|
||||
EXPECT_EQ(buf, Buffer(exp));
|
||||
Buffer buf2;
|
||||
buf2.AppendData(buf);
|
||||
buf2.AppendData(rtc::ArrayView<uint8_t>(buf));
|
||||
const int8_t exp2[] = {0x4, 0x5, 0x6, 0xa, 0xb, 0x4, 0x5, 0x6, 0xa, 0xb};
|
||||
EXPECT_EQ(buf2, Buffer(exp2));
|
||||
}
|
||||
|
||||
TEST(BufferTest, TestSetAndAppendWithUnknownArg) {
|
||||
struct TestDataContainer {
|
||||
size_t size() const { return 3; }
|
||||
const uint8_t* data() const { return kTestData; }
|
||||
};
|
||||
Buffer buf;
|
||||
buf.SetData(TestDataContainer());
|
||||
EXPECT_EQ(3u, buf.size());
|
||||
EXPECT_EQ(Buffer(kTestData, 3), buf);
|
||||
buf.AppendData(TestDataContainer());
|
||||
EXPECT_EQ(6u, buf.size());
|
||||
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 3));
|
||||
EXPECT_EQ(0, memcmp(buf.data() + 3, kTestData, 3));
|
||||
}
|
||||
|
||||
TEST(BufferTest, TestSetSizeSmaller) {
|
||||
|
||||
77
webrtc/base/type_traits.h
Normal file
77
webrtc/base/type_traits.h
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2016 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_TYPE_TRAITS_H_
|
||||
#define WEBRTC_BASE_TYPE_TRAITS_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
|
||||
namespace rtc {
|
||||
|
||||
// Determines if the given class has zero-argument .data() and .size() methods
|
||||
// whose return values are convertible to T* and size_t, respectively.
|
||||
template <typename DS, typename T>
|
||||
class HasDataAndSize {
|
||||
private:
|
||||
template <
|
||||
typename C,
|
||||
typename std::enable_if<
|
||||
std::is_convertible<decltype(std::declval<C>().data()), T*>::value &&
|
||||
std::is_convertible<decltype(std::declval<C>().size()),
|
||||
std::size_t>::value>::type* = nullptr>
|
||||
static int Test(int);
|
||||
|
||||
template <typename>
|
||||
static char Test(...);
|
||||
|
||||
public:
|
||||
static constexpr bool value = std::is_same<decltype(Test<DS>(0)), int>::value;
|
||||
};
|
||||
|
||||
namespace test_has_data_and_size {
|
||||
|
||||
template <typename DR, typename SR>
|
||||
struct Test1 {
|
||||
DR data();
|
||||
SR size();
|
||||
};
|
||||
static_assert(HasDataAndSize<Test1<int*, int>, int>::value, "");
|
||||
static_assert(HasDataAndSize<Test1<int*, int>, const int>::value, "");
|
||||
static_assert(HasDataAndSize<Test1<const int*, int>, const int>::value, "");
|
||||
static_assert(!HasDataAndSize<Test1<const int*, int>, int>::value,
|
||||
"implicit cast of const int* to int*");
|
||||
static_assert(!HasDataAndSize<Test1<char*, size_t>, int>::value,
|
||||
"implicit cast of char* to int*");
|
||||
|
||||
struct Test2 {
|
||||
int* data;
|
||||
size_t size;
|
||||
};
|
||||
static_assert(!HasDataAndSize<Test2, int>::value,
|
||||
".data and .size aren't functions");
|
||||
|
||||
struct Test3 {
|
||||
int* data();
|
||||
};
|
||||
static_assert(!HasDataAndSize<Test3, int>::value, ".size() is missing");
|
||||
|
||||
class Test4 {
|
||||
int* data();
|
||||
size_t size();
|
||||
};
|
||||
static_assert(!HasDataAndSize<Test4, int>::value,
|
||||
".data() and .size() are private");
|
||||
|
||||
} // namespace test_has_data_and_size
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
#endif // WEBRTC_BASE_TYPE_TRAITS_H_
|
||||
Loading…
x
Reference in New Issue
Block a user