mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 13:50:40 +01:00
ArrayView, adding ctor for fixed-size views of std::array.
This CL allows to reduce the code required to create fixed-size ArrayView objects for std::array instances. Instead of passing .data() and .size(), it is now sufficient to pass the std::array instance. When instancing an array view with variable size, a different ctor is called. Bug: webrtc:9076 Change-Id: I4fe133b27cd12827ed0206d40184279fc3a196f5 Reviewed-on: https://webrtc-review.googlesource.com/76160 Commit-Queue: Alessio Bazzica <alessiob@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#23220}
This commit is contained in:
parent
6e0c145538
commit
858c4d70cd
2 changed files with 26 additions and 1 deletions
|
@ -11,6 +11,7 @@
|
||||||
#ifndef API_ARRAY_VIEW_H_
|
#ifndef API_ARRAY_VIEW_H_
|
||||||
#define API_ARRAY_VIEW_H_
|
#define API_ARRAY_VIEW_H_
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
|
@ -169,7 +170,7 @@ class ArrayView final : public impl::ArrayViewBase<T, Size> {
|
||||||
RTC_DCHECK_EQ(0, size);
|
RTC_DCHECK_EQ(0, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct an ArrayView from an array.
|
// Construct an ArrayView from a C-style array.
|
||||||
template <typename U, size_t N>
|
template <typename U, size_t N>
|
||||||
ArrayView(U (&array)[N]) // NOLINT
|
ArrayView(U (&array)[N]) // NOLINT
|
||||||
: ArrayView(array, N) {
|
: ArrayView(array, N) {
|
||||||
|
@ -177,6 +178,16 @@ 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
|
||||||
|
// std::array instance. For an ArrayView with variable size, the used ctor is
|
||||||
|
// ArrayView(U& u) instead - i.e., the next one.
|
||||||
|
template <typename U,
|
||||||
|
size_t N,
|
||||||
|
typename std::enable_if<
|
||||||
|
Size == static_cast<std::ptrdiff_t>(N)>::type* = nullptr>
|
||||||
|
ArrayView(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,
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -180,6 +181,19 @@ TEST(ArrayViewTest, TestCopyAssignmentFixed) {
|
||||||
// v = z; // Compile error, because can't drop const.
|
// v = z; // Compile error, because can't drop const.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(ArrayViewTest, TestStdArray) {
|
||||||
|
constexpr size_t size = 5;
|
||||||
|
std::array<float, size> arr{};
|
||||||
|
// Fixed size view.
|
||||||
|
rtc::ArrayView<float, size> arr_view_fixed(arr);
|
||||||
|
EXPECT_EQ(arr.data(), arr_view_fixed.data());
|
||||||
|
static_assert(size == arr_view_fixed.size(), "");
|
||||||
|
// Variable size view.
|
||||||
|
rtc::ArrayView<float> arr_view(arr);
|
||||||
|
EXPECT_EQ(arr.data(), arr_view.data());
|
||||||
|
EXPECT_EQ(size, 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);
|
||||||
|
|
Loading…
Reference in a new issue