Debian Codegraph @

src/atlas/array/ArrayViewVariant.h

raw
1 /*
2 * (C) Crown Copyright 2024 Met Office
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 */
7
8 #pragma once
9
10 #include <type_traits>
11 #include <variant>
12
13 #include "atlas/array/ArrayView.h"
14
15 namespace atlas {
16 namespace array {
17
18 namespace detail {
19
20 // Container struct for a list of types.
21 template <typename... Ts>
22 struct Types {
23 using add_const = Types<std::add_const_t<Ts>...>;
24 };
25
26 // Container struct for a list of integers.
27 template <int... Is>
28 struct Ints {};
29
30 template <typename ValueTypes, typename Ranks, typename... ArrayViews>
31 struct VariantHelper;
32
33 // Recursively construct ArrayView std::variant from types Ts and ranks Is.
34 template <typename T, typename... Ts, int... Is, typename... ArrayViews>
35 struct VariantHelper<Types<T, Ts...>, Ints<Is...>, ArrayViews...> {
36 using type = typename VariantHelper<Types<Ts...>, Ints<Is...>, ArrayViews...,
37 ArrayView<T, Is>...>::type;
38 };
39
40 // End recursion.
41 template <int... Is, typename... ArrayViews>
42 struct VariantHelper<Types<>, Ints<Is...>, ArrayViews...> {
43 using type = std::variant<ArrayViews...>;
44 };
45
46 template <typename ValueTypes, typename Ranks>
47 using Variant = typename VariantHelper<ValueTypes, Ranks>::type;
48
49 using VariantValueTypes =
50 detail::Types<float, double, int, long, unsigned long>;
51
52 using VariantRanks = detail::Ints<1, 2, 3, 4, 5, 6, 7, 8, 9>;
53
54 } // namespace detail
55
56 class Array;
57
58 /// @brief Variant containing all supported non-const ArrayView alternatives.
59 using ArrayViewVariant =
60 detail::Variant<detail::VariantValueTypes, detail::VariantRanks>;
61
62 /// @brief Variant containing all supported const ArrayView alternatives.
63 using ConstArrayViewVariant =
64 detail::Variant<detail::VariantValueTypes::add_const, detail::VariantRanks>;
65
66 /// @brief Create an ArrayView and assign to an ArrayViewVariant.
67 ArrayViewVariant make_view_variant(Array& array);
68
69 /// @brief Create a const ArrayView and assign to an ArrayViewVariant.
70 ConstArrayViewVariant make_view_variant(const Array& array);
71
72 /// @brief Create a host ArrayView and assign to an ArrayViewVariant.
73 ArrayViewVariant make_host_view_variant(Array& array);
74
75 /// @brief Create a const host ArrayView and assign to an ArrayViewVariant.
76 ConstArrayViewVariant make_host_view_variant(const Array& array);
77
78 /// @brief Create a device ArrayView and assign to an ArrayViewVariant.
79 ArrayViewVariant make_device_view_variant(Array& array);
80
81 /// @brief Create a const device ArrayView and assign to an ArrayViewVariant.
82 ConstArrayViewVariant make_device_view_variant(const Array& array);
83
84 /// @brief Return true if View::rank() is any of Ranks...
85 template <int... Ranks, typename View>
86 constexpr bool is_rank(const View&) {
87 return ((std::decay_t<View>::rank() == Ranks) || ...);
88 }
89 /// @brief Return true if View::value_type is any of ValuesTypes...
90 template <typename... ValueTypes, typename View>
91 constexpr bool is_value_type(const View&) {
92 using ValueType = typename std::decay_t<View>::value_type;
93 return ((std::is_same_v<ValueType, ValueTypes>) || ...);
94 }
95
96 /// @brief Return true if View::non_const_value_type is any of ValuesTypes...
97 template <typename... ValueTypes, typename View>
98 constexpr bool is_non_const_value_type(const View&) {
99 using ValueType = typename std::decay_t<View>::non_const_value_type;
100 return ((std::is_same_v<ValueType, ValueTypes>) || ...);
101 }
102
103 } // namespace array
104 } // namespace atlas