From d7743eed77172f4479615d2466903912e0446599 Mon Sep 17 00:00:00 2001 From: odlomax Date: Wed, 2 Oct 2024 10:24:33 +0100 Subject: [PATCH] Refactored introspection helpers. --- src/atlas/array/ArrayViewVariant.h | 27 ++++++---- src/tests/array/test_array_view_variant.cc | 57 ++++++++++++---------- 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/atlas/array/ArrayViewVariant.h b/src/atlas/array/ArrayViewVariant.h index 0ea3f87c6..a32a90539 100644 --- a/src/atlas/array/ArrayViewVariant.h +++ b/src/atlas/array/ArrayViewVariant.h @@ -50,16 +50,16 @@ using Variant = typename VariantHelper::type; } // namespace detail /// @brief Supported ArrayView value types. -using Values = detail::Types; +using ValueTypes = detail::Types; /// @brief Supported ArrayView ranks. using Ranks = detail::Ints<1, 2, 3, 4, 5, 6, 7, 8, 9>; /// @brief Variant containing all supported non-const ArrayView alternatives. -using ArrayViewVariant = detail::Variant; +using ArrayViewVariant = detail::Variant; /// @brief Variant containing all supported const ArrayView alternatives. -using ConstArrayViewVariant = detail::Variant; +using ConstArrayViewVariant = detail::Variant; /// @brief Create an ArrayView and assign to an ArrayViewVariant. ArrayViewVariant make_view_variant(Array& array); @@ -79,17 +79,24 @@ ArrayViewVariant make_device_view_variant(Array& array); /// @brief Create a const device ArrayView and assign to an ArrayViewVariant. ConstArrayViewVariant make_device_view_variant(const Array& array); -/// @brief Return true if ArrayView::rank() is any of Ranks... +/// @brief Return true if View::rank() is any of Ranks... template -constexpr bool RankIs() { +constexpr bool is_rank() { return ((std::decay_t::rank() == Ranks) || ...); } -/// @brief Return true if View::non_const_value_type is any of Values... -template -constexpr bool ValueIs() { - using Value = typename std::decay_t::non_const_value_type; - return ((std::is_same_v) || ...); +/// @brief Return true if View::value_type is any of ValuesTypes... +template +constexpr bool is_value_type() { + using ValueType = typename std::decay_t::value_type; + return ((std::is_same_v) || ...); +} + +/// @brief Return true if View::non_const_value_type is any of ValuesTypes... +template +constexpr bool is_non_const_value_type() { + using ValueType = typename std::decay_t::non_const_value_type; + return ((std::is_same_v) || ...); } } // namespace array diff --git a/src/tests/array/test_array_view_variant.cc b/src/tests/array/test_array_view_variant.cc index 29a3cb044..a052d234f 100644 --- a/src/tests/array/test_array_view_variant.cc +++ b/src/tests/array/test_array_view_variant.cc @@ -33,10 +33,10 @@ CASE("test variant assignment") { auto view3 = make_view_variant(array3); auto view4 = make_view_variant(arrayRef); - auto hostView1 = make_host_view_variant(array1); - auto hostView2 = make_host_view_variant(array2); - auto hostView3 = make_host_view_variant(array3); - auto hostView4 = make_host_view_variant(arrayRef); + const auto hostView1 = make_host_view_variant(array1); + const auto hostView2 = make_host_view_variant(array2); + const auto hostView3 = make_host_view_variant(array3); + const auto hostView4 = make_host_view_variant(arrayRef); auto deviceView1 = make_device_view_variant(array1); auto deviceView2 = make_device_view_variant(array2); @@ -46,33 +46,37 @@ CASE("test variant assignment") { const auto visitVariants = [](auto& var1, auto& var2, auto var3, auto var4) { std::visit( [](auto&& view) { - using View = std::remove_reference_t; - EXPECT_EQ(View::rank(), 1); - EXPECT((std::is_same_v)); + using View = decltype(view); + EXPECT((is_rank())); + EXPECT((is_value_type())); + EXPECT((is_non_const_value_type())); }, var1); std::visit( [](auto&& view) { - using View = std::remove_reference_t; - EXPECT_EQ(View::rank(), 2); - EXPECT((std::is_same_v)); + using View = decltype(view); + EXPECT((is_rank())); + EXPECT((is_value_type())); + EXPECT((is_non_const_value_type())); }, var2); std::visit( [](auto&& view) { - using View = std::remove_reference_t; - EXPECT_EQ(View::rank(), 3); - EXPECT((std::is_same_v)); + using View = decltype(view); + EXPECT((is_rank())); + EXPECT((is_value_type())); + EXPECT((is_non_const_value_type())); }, var3); std::visit( [](auto&& view) { - using View = std::remove_reference_t; - EXPECT_EQ(View::rank(), 1); - EXPECT((std::is_same_v)); + using View = decltype(view); + EXPECT((is_rank())); + EXPECT((is_value_type())); + EXPECT((is_non_const_value_type())); }, var4); }; @@ -90,11 +94,13 @@ CASE("test std::visit") { make_view(array2).assign({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); const auto testRank1 = [](auto&& view) { + + using View = std::remove_reference_t; - EXPECT_EQ(View::rank(), 1); using Value = typename View::value_type; - EXPECT((std::is_same_v)); + EXPECT((is_rank())); + EXPECT((is_value_type())); for (auto i = size_t{0}; i < view.size(); ++i) { EXPECT_EQ(view(i), static_cast(i)); } @@ -102,9 +108,10 @@ CASE("test std::visit") { const auto testRank2 = [](auto&& view) { using View = std::remove_reference_t; - EXPECT_EQ(View::rank(), 2); using Value = typename View::value_type; - EXPECT((std::is_same_v)); + + EXPECT((is_rank())); + EXPECT((is_value_type())); auto testValue = int{0}; for (auto i = idx_t{0}; i < view.shape(0); ++i) { @@ -122,11 +129,11 @@ CASE("test std::visit") { auto rank2Tested = false; const auto visitor = [&](auto&& view) { - if constexpr (RankIs()) { + if constexpr (is_rank()) { rank1Tested = true; return testRank1(view); } - if constexpr (RankIs()) { + if constexpr (is_rank()) { rank2Tested = true; return testRank2(view); } @@ -146,15 +153,15 @@ CASE("test std::visit") { auto rank1Tested = false; auto rank2Tested = false; const auto visitor = eckit::Overloaded{ - [&](auto&& view) -> std::enable_if_t()> { + [&](auto&& view) -> std::enable_if_t()> { testRank1(view); rank1Tested = true; }, - [&](auto&& view) -> std::enable_if_t()> { + [&](auto&& view) -> std::enable_if_t()> { testRank2(view); rank2Tested = true; }, - [](auto&& view) -> std::enable_if_t()> { + [](auto&& view) -> std::enable_if_t()> { // Test should not reach here. EXPECT(false); }};