EchoMap
2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Toggle main menu visibility
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
17
namespace
echomap::variant_helpers
18
{
19
28
template
<
typename
T,
typename
Variant>
struct
IsVariantAlternative
:
std::false_type
29
{};
30
40
template
<
typename
T,
typename
... Alternatives>
41
struct
IsVariantAlternative
<T,
std
::variant<Alternatives...>>
42
:
std::bool_constant
<(std::same_as<std::remove_cvref_t<T>, Alternatives> || ...)>
43
{};
44
53
template
<
typename
T,
typename
Variant>
54
inline
constexpr
bool
is_variant_alternative_v
=
55
IsVariantAlternative<std::remove_cvref_t<T>
,
std::remove_cvref_t<Variant>
>::value;
56
66
template
<
typename
T,
typename
Variant>
67
concept
VariantAlternative
=
is_variant_alternative_v<T, Variant>
;
68
90
template
<
class
... Ts>
struct
Overloaded
: Ts...
91
{
92
using
Ts::operator()...;
93
};
94
123
template
<
124
class
Variant,
125
class
NameGetter,
126
std::size_t
... indices>
127
consteval
auto
make_variant_name_array
(
128
std::index_sequence<indices...>
sequence
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
156
template
<
class
Variant,
class
NameGetter>
157
inline
constexpr
auto
variant_name_array
=
158
make_variant_name_array<Variant, NameGetter>
(
std::make_index_sequence
<
std::variant_size_v<Variant>
>{});
159
178
template
<
179
class
Variant,
180
std::size_t
index>
181
constexpr
Variant
make_variant_alternative
()
182
{
183
return
Variant{
std::in_place_index<index>
};
184
}
185
212
template
<
213
class
Variant,
214
std::size_t
... indices>
215
consteval
auto
make_variant_factory_array
(
216
std::index_sequence<indices...>
sequence
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
242
template
<
class
Variant>
243
Variant
variant_from_index
(
244
const
std::size_t
index
245
)
246
{
247
static
constexpr
auto
factories =
248
make_variant_factory_array<Variant>
(
std::make_index_sequence
<
std::variant_size_v<Variant>
>{});
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
echomap::variant_helpers::is_variant_alternative_v
constexpr bool is_variant_alternative_v
Whether a type is an alternative of a std::variant.
Definition
VariantHelpers.hpp:54
echomap::variant_helpers::make_variant_name_array
consteval auto make_variant_name_array(std::index_sequence< indices... > sequence)
Produces an array of human-readable names for selected alternatives of a variant.
Definition
VariantHelpers.hpp:127
echomap::variant_helpers::variant_name_array
constexpr auto variant_name_array
Array of human-readable names for every alternative in a variant.
Definition
VariantHelpers.hpp:157
echomap::variant_helpers::make_variant_alternative
constexpr Variant make_variant_alternative()
Constructs a variant holding the alternative at a compile-time index.
Definition
VariantHelpers.hpp:181
echomap::variant_helpers::make_variant_factory_array
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.
Definition
VariantHelpers.hpp:215
echomap::variant_helpers::variant_from_index
Variant variant_from_index(const std::size_t index)
Constructs a std::variant holding the alternative at the given runtime index.
Definition
VariantHelpers.hpp:243
std::array
std::string_view
echomap::variant_helpers::VariantAlternative
Constrains a type to be one of the alternatives of a std::variant.
Definition
VariantHelpers.hpp:67
std::in_place_index
T in_place_index
std::index_sequence
std::false_type
std
STL namespace.
std::out_of_range
std::remove_cvref_t
std::size_t
stdexcept
echomap::variant_helpers::IsVariantAlternative
Determines whether a type is one of the alternatives of a std::variant.
Definition
VariantHelpers.hpp:29
echomap::variant_helpers::Overloaded
A helper type for succinctly specifying callable visitors over a variant alternative.
Definition
VariantHelpers.hpp:91
utility
variant
std::variant_size_v
T variant_size_v
src
utility
VariantHelpers.hpp
Generated by
1.17.0