ArrayView, adding ctor for fixed-size views of const(expr) std::array.

This CL allows to reduce the code required to create fixed-size ArrayView
objects for const(expr) std::array instances. Instead of passing .data() and
size(), it is now sufficient to pass the const(expr) std::array instance.
When instancing an array view with variable size, a different ctor is called.

Bug: webrtc:9076
Change-Id: Ie1182fdc33c6b5657f510b6723552813d5933e3e
Reviewed-on: https://webrtc-review.googlesource.com/76820
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23243}
This commit is contained in:
Alessio Bazzica 2018-05-15 14:57:51 +02:00 committed by Commit Bot
parent ae93f0412a
commit 28a325b523
2 changed files with 32 additions and 3 deletions

View File

@ -178,9 +178,9 @@ class ArrayView final : public impl::ArrayViewBase<T, Size> {
"Array size must match ArrayView size"); "Array size must match ArrayView size");
} }
// (Only if size is fixed.) Construct an ArrayView with fixed size from an // (Only if size is fixed.) Construct a fixed size ArrayView<T, N> from a
// std::array instance. For an ArrayView with variable size, the used ctor is // non-const std::array instance. For an ArrayView with variable size, the
// ArrayView(U& u) instead - i.e., the next one. // used ctor is ArrayView(U& u) instead.
template <typename U, template <typename U,
size_t N, size_t N,
typename std::enable_if< typename std::enable_if<
@ -188,6 +188,16 @@ class ArrayView final : public impl::ArrayViewBase<T, Size> {
ArrayView(std::array<U, N>& u) // NOLINT ArrayView(std::array<U, N>& u) // NOLINT
: ArrayView(u.data(), u.size()) {} : ArrayView(u.data(), u.size()) {}
// (Only if size is fixed.) Construct a fixed size ArrayView<T, N> where T is
// const from a const(expr) std::array instance. For an ArrayView with
// variable size, the used ctor is ArrayView(U& u) instead.
template <typename U,
size_t N,
typename std::enable_if<
Size == static_cast<std::ptrdiff_t>(N)>::type* = nullptr>
ArrayView(const std::array<U, N>& u) // NOLINT
: ArrayView(u.data(), u.size()) {}
// (Only if size is fixed.) Construct an ArrayView from any type U that has a // (Only if size is fixed.) Construct an ArrayView from any type U that has a
// static constexpr size() method whose return value is equal to Size, and a // static constexpr size() method whose return value is equal to Size, and a
// data() method whose return value converts implicitly to T*. In particular, // data() method whose return value converts implicitly to T*. In particular,

View File

@ -194,6 +194,25 @@ TEST(ArrayViewTest, TestStdArray) {
EXPECT_EQ(size, arr_view.size()); EXPECT_EQ(size, arr_view.size());
} }
TEST(ArrayViewTest, TestConstStdArray) {
constexpr size_t size = 5;
constexpr std::array<float, size> constexpr_arr{};
rtc::ArrayView<const float, size> constexpr_arr_view(constexpr_arr);
EXPECT_EQ(constexpr_arr.data(), constexpr_arr_view.data());
static_assert(constexpr_arr.size() == constexpr_arr_view.size(), "");
const std::array<float, size> const_arr{};
rtc::ArrayView<const float, size> const_arr_view(const_arr);
EXPECT_EQ(const_arr.data(), const_arr_view.data());
static_assert(const_arr.size() == const_arr_view.size(), "");
std::array<float, size> non_const_arr{};
rtc::ArrayView<const float, size> non_const_arr_view(non_const_arr);
EXPECT_EQ(non_const_arr.data(), non_const_arr_view.data());
static_assert(non_const_arr.size() == non_const_arr_view.size(), "");
}
TEST(ArrayViewTest, TestStdVector) { TEST(ArrayViewTest, TestStdVector) {
std::vector<int> v; std::vector<int> v;
v.push_back(3); v.push_back(3);