EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
VariantHelpers.hpp File Reference

VariantHelpers specification. More...

#include <stdexcept>
#include <utility>
#include <variant>

Go to the source code of this file.

Classes

struct  echomap::variant_helpers::IsVariantAlternative< T, Variant >
 Determines whether a type is one of the alternatives of a std::variant. More...
struct  echomap::variant_helpers::IsVariantAlternative< T, std::variant< Alternatives... > >
 Specialisation for std::variant types. More...
struct  echomap::variant_helpers::Overloaded< Ts >
 A helper type for succinctly specifying callable visitors over a variant alternative. More...

Namespaces

namespace  echomap
 The main EchoMap outermost namespace for all non-exported symbols.

Concepts

concept  echomap::variant_helpers::VariantAlternative
 Constrains a type to be one of the alternatives of a std::variant.

Functions

template<class Variant, class NameGetter, std::size_t... indices>
consteval auto echomap::variant_helpers::make_variant_name_array (std::index_sequence< indices... > sequence)
 Produces an array of human-readable names for selected alternatives of a variant.
template<class Variant, std::size_t index>
constexpr Variant echomap::variant_helpers::make_variant_alternative ()
 Constructs a variant holding the alternative at a compile-time index.
template<class Variant, std::size_t... indices>
consteval auto echomap::variant_helpers::make_variant_factory_array (std::index_sequence< indices... > sequence)
 Produces an array of factory function pointers for constructing a variant by alternative index.
template<class Variant>
Variant echomap::variant_helpers::variant_from_index (const std::size_t index)
 Constructs a std::variant holding the alternative at the given runtime index.

Variables

template<typename T, typename Variant>
constexpr bool echomap::variant_helpers::is_variant_alternative_v
 Whether a type is an alternative of a std::variant.
template<class Variant, class NameGetter>
constexpr auto echomap::variant_helpers::variant_name_array
 Array of human-readable names for every alternative in a variant.

Detailed Description

VariantHelpers specification.

Author
Oliver Dixon
Date
2026-07-17

Definition in file VariantHelpers.hpp.

Function Documentation

◆ make_variant_alternative()

template<class Variant, std::size_t index>
Variant echomap::variant_helpers::make_variant_alternative ( )
constexpr

Constructs a variant holding the alternative at a compile-time index.

The returned variant is constructed with std::in_place_index for Index. Therefore, the initially held alternative is the default-constructed Index-th alternative type of Variant.

For example, for std::variant<A, B, C>, calling this function with Index set to 1 returns a variant holding a default-constructed B.

Template Parameters
VariantThe variant type to construct.
indexThe index of the alternative to hold initially.
Returns
A Variant holding a default-constructed instance of the alternative at Index.
Precondition
Variant must be a specialization of std::variant.
Index must be a valid alternative index for Variant.
The selected alternative type must be default-constructible.

Definition at line 181 of file VariantHelpers.hpp.

182{
183 return Variant{std::in_place_index<index>};
184}
T in_place_index

◆ make_variant_factory_array()

template<class Variant, std::size_t... indices>
auto echomap::variant_helpers::make_variant_factory_array ( std::index_sequence< indices... > sequence)
consteval

Produces an array of factory function pointers for constructing a variant by alternative index.

Given a variant type std::variant<A, B, C> and an index sequence equivalent to std::index_sequence<0, 1, 2>, this function returns an array of type:

The function pointer at position \( i \) constructs a Variant whose initially held alternative is the default-constructed \( i \)-th alternative type.

For example, calling the factory at index 1 for std::variant<A, B, C> returns a variant holding a default-constructed B.

Template Parameters
VariantThe variant type constructed by the returned factory functions.
indicesThe alternative indices for which factory functions are generated.
Parameters
sequenceThe index sequence identifying the variant alternatives. The value is used only for template argument deduction.
Returns
An array of function pointers. Each function constructs a Variant holding the default-constructed alternative corresponding to its index.
Precondition
Each value in Indices must be a valid alternative index for Variant.
Each selected alternative type must be default-constructible.

Definition at line 215 of file VariantHelpers.hpp.

218{
219 std::ignore = sequence;
220 using Factory = Variant (*)();
221
222 return std::array<Factory, sizeof...(indices)>{&make_variant_alternative<Variant, indices>...};
223}
constexpr Variant make_variant_alternative()
Constructs a variant holding the alternative at a compile-time index.

◆ make_variant_name_array()

template<class Variant, class NameGetter, std::size_t... indices>
auto echomap::variant_helpers::make_variant_name_array ( std::index_sequence< indices... > sequence)
consteval

Produces an array of human-readable names for selected alternatives of a variant.

Given a variant type std::variant<A, B, C>, a name getter NameGetter, and an index sequence equivalent to std::index_sequence<0, 1, 2>, this function returns an array containing:

NameGetter::get<A>(),
NameGetter::get<B>(),
NameGetter::get<C>()

The returned array preserves the alternative ordering of Variant. Therefore, the name at position \( i \) corresponds to the \( i \)-th selected alternative index in the supplied index sequence.

Template Parameters
VariantThe variant type whose alternative names are generated.
NameGetterA policy type providing NameGetter::template get<T>() for each selected alternative type.
indicesThe alternative indices for which names are generated.
Parameters
sequenceThe index sequence identifying the variant alternatives. The value is used only for template argument deduction.
Returns
An array of human-readable names corresponding to the selected alternatives.
Precondition
Variant must be a specialization of std::variant.
Each value in Indices must be a valid alternative index for Variant.
NameGetter must provide a valid get<T>() function for every selected alternative type.

Definition at line 127 of file VariantHelpers.hpp.

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}

◆ variant_from_index()

template<class Variant>
Variant echomap::variant_helpers::variant_from_index ( const std::size_t index)

Constructs a std::variant holding the alternative at the given runtime index.

The returned variant is constructed with std::in_place_index for the requested alternative index. The selected alternative is default-constructed.

Template Parameters
VariantThe variant type to construct.
Parameters
indexThe index of the alternative that should be initially held by the variant.
Returns
A Variant holding a default-constructed instance of the alternative at index.
Exceptions
std::out_of_rangeindex is not a valid alternative index for Variant.
Precondition
Variant must be a specialization of std::variant.
The selected alternative type must be default-constructible.

Definition at line 243 of file VariantHelpers.hpp.

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}
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.
T variant_size_v

Variable Documentation

◆ is_variant_alternative_v

template<typename T, typename Variant>
bool echomap::variant_helpers::is_variant_alternative_v
inlineconstexpr
Initial value:

Whether a type is an alternative of a std::variant.

Removes cv-qualifiers and references from both T and Variant before performing the test.

Template Parameters
TThe type to test.
VariantThe variant type whose alternatives should be inspected.

Definition at line 54 of file VariantHelpers.hpp.

◆ variant_name_array

template<class Variant, class NameGetter>
auto echomap::variant_helpers::variant_name_array
inlineconstexpr
Initial value:
=
consteval auto make_variant_name_array(std::index_sequence< indices... > sequence)
Produces an array of human-readable names for selected alternatives of a variant.

Array of human-readable names for every alternative in a variant.

The array order matches the alternative order of Variant. Therefore, index \( i \) in this array names the same alternative selected by std::variant::index() == i.

Names are obtained from NameGetter, which must provide a templated static function of the form:

template<class T>
static consteval std::string_view get();
Template Parameters
VariantThe variant type whose alternatives are named.
NameGetterA policy type used to obtain names for each alternative type.
Precondition
Variant must be a specialization of std::variant.
Every alternative type in Variant must be accepted by NameGetter.

Definition at line 157 of file VariantHelpers.hpp.