EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
VariantHelpers.hpp
Go to the documentation of this file.
1
9
10#ifndef ECHOMAP_VARIANTHELPERS_HPP
11#define ECHOMAP_VARIANTHELPERS_HPP
12
13#include <stdexcept>
14#include <utility>
15#include <variant>
16
17namespace echomap::variant_helpers
18{
19
28template <typename T, typename Variant> struct IsVariantAlternative : std::false_type
29{};
30
40template <typename T, typename... Alternatives>
41struct IsVariantAlternative<T, std::variant<Alternatives...>>
42 : std::bool_constant<(std::same_as<std::remove_cvref_t<T>, Alternatives> || ...)>
43{};
44
53template <typename T, typename Variant>
54inline constexpr bool is_variant_alternative_v =
56
66template <typename T, typename Variant>
68
90template <class... Ts> struct Overloaded : Ts...
91{
92 using Ts::operator()...;
93};
94
123template <
124 class Variant,
125 class NameGetter,
126 std::size_t... indices>
129)
130{
131 std::ignore = sequence;
132 return std::array<std::string_view, sizeof...(indices)>{
133 NameGetter::template get<std::variant_alternative_t<indices, Variant>>()...
134 };
135}
136
156template <class Variant, class NameGetter>
157inline constexpr auto variant_name_array =
159
178template <
179 class Variant,
180 std::size_t index>
181constexpr Variant make_variant_alternative()
182{
183 return Variant{std::in_place_index<index>};
184}
185
212template <
213 class Variant,
214 std::size_t... indices>
217)
218{
219 std::ignore = sequence;
220 using Factory = Variant (*)();
221
222 return std::array<Factory, sizeof...(indices)>{&make_variant_alternative<Variant, indices>...};
223}
224
242template <class Variant>
244 const std::size_t index
245)
246{
247 static constexpr auto factories =
249
250 if (index >= factories.size())
251 throw std::out_of_range("Invalid variant index.");
252
253 return factories[index]();
254}
255
256} // namespace echomap::variant_helpers
257
258#endif // ECHOMAP_VARIANTHELPERS_HPP
constexpr bool is_variant_alternative_v
Whether a type is an alternative of a std::variant.
consteval auto make_variant_name_array(std::index_sequence< indices... > sequence)
Produces an array of human-readable names for selected alternatives of a variant.
constexpr auto variant_name_array
Array of human-readable names for every alternative in a variant.
constexpr Variant make_variant_alternative()
Constructs a variant holding the alternative at a compile-time index.
consteval auto make_variant_factory_array(std::index_sequence< indices... > sequence)
Produces an array of factory function pointers for constructing a variant by alternative index.
Variant variant_from_index(const std::size_t index)
Constructs a std::variant holding the alternative at the given runtime index.
Constrains a type to be one of the alternatives of a std::variant.
T in_place_index
STL namespace.
Determines whether a type is one of the alternatives of a std::variant.
A helper type for succinctly specifying callable visitors over a variant alternative.
T variant_size_v