refl-cpp |

Classes | Typedefs | Functions
refl::util Namespace Reference

Contains utility types and functions for working with those types. More...

## Classes
struct | const_string
| Represents a compile-time string. More...
struct | type_list
| Represents a compile-time list of types provided as variadic template parameters. More...
struct | type_list< T >

## Typedefs
template<typename T >
using | type_tag = type_list< T >

## Functions
constexpr const_string< 0 > | make_const_string () noexcept
| Creates an empty instance of const_string<N> More...
template<size_t N>
constexpr const_string< N - 1 > | make_const_string (const char(&str)[N]) noexcept
| Creates an instance of const_string<N> More...
constexpr const_string< 1 > | make_const_string (char ch) noexcept
| Creates an instance of const_string<N> More...
template<size_t N, size_t M>
constexpr const_string< N+M > | operator+ (const const_string< N > &a, const const_string< M > &b) noexcept
| Concatenates two const_strings together. More...
template<size_t N, size_t M>
constexpr const_string< N+M - 1 > | operator+ (const const_string< N > &a, const char(&b)[M]) noexcept
| Concatenates a const_string with a C-style string. More...
template<size_t N, size_t M>
constexpr const_string< N+M - 1 > | operator+ (const char(&a)[N], const const_string< M > &b) noexcept
| Concatenates a C-style string with a const_string. More...
template<size_t N, size_t M>
constexpr bool | operator== (const const_string< N > &a, const const_string< M > &b) noexcept
| Compares two const_strings for equality. More...
template<size_t N, size_t M>
constexpr bool | operator!= (const const_string< N > &a, const const_string< M > &b) noexcept
| Compares two const_strings for equality. More...
template<size_t N, size_t M>
constexpr bool | operator== (const const_string< N > &a, const char(&b)[M]) noexcept
| Compares a const_string with a C-style string for equality. More...
template<size_t N, size_t M>
constexpr bool | operator!= (const const_string< N > &a, const char(&b)[M]) noexcept
| Compares a const_string with a C-style string for equality. More...
template<size_t N, size_t M>
constexpr bool | operator== (const char(&a)[N], const const_string< M > &b) noexcept
| Compares a C-style string with a const_string for equality. More...
template<size_t N, size_t M>
constexpr bool | operator!= (const char(&a)[N], const const_string< M > &b) noexcept
| Compares a C-style string with a const_string for equality. More...
template<size_t N>
constexpr std::ostream & | operator<< (std::ostream &os, const const_string< N > &str) noexcept
template<typename T = int, typename... Ts>
constexpr int | ignore (Ts &&...) noexcept
| Ignores all parameters. More...
template<typename T >
constexpr decltype(auto) | identity (T &&t) noexcept
| Returns the input paratemeter as-is. More...
template<typename T >
constexpr const T & | make_const (const T &value) noexcept
| Adds const to the input reference. More...
template<typename T >
constexpr const T & | make_const (T &value) noexcept
| Adds const to the input reference. More...
template<typename T , typename... Ts>
constexpr std::array< T, sizeof...(Ts)> | to_array (const std::tuple< Ts... > &tuple) noexcept
| Creates an array of type 'T' from the provided tuple. More...
template<typename T , size_t N>
constexpr auto | to_tuple (const std::array< T, N > &array) noexcept
| Creates a tuple from the provided array. More...
template<typename... Ts>
constexpr std::tuple< Ts... > | as_tuple (type_list< Ts... >) noexcept
| Creates a matching std::tuple from a type_list. More...
template<typename... Ts>
constexpr type_list< Ts... > | as_type_list (const std::tuple< Ts... > &) noexcept
| Creates a matching type_list from a std::tuple. More...
template<typename F , typename... Ts>
constexpr auto | map_to_tuple (type_list< Ts... > list, F &&f)
| Applies function F to each type in the type_list, aggregating the results in a tuple. More...
template<typename T , typename F , typename... Ts>
constexpr auto | map_to_array (type_list< Ts... > list, F &&f)
| Applies function F to each type in the type_list, aggregating the results in an array. More...
template<typename F , typename... Ts>
constexpr void | for_each (type_list< Ts... > list, F &&f)
| Applies function F to each type in the type_list. More...
template<typename R , typename F , typename T , typename... Ts>
constexpr auto | accumulate (type_list< T, Ts... >, F &&f, R &&initial_value)
template<typename F , typename... Ts>
constexpr size_t | count_if (type_list< Ts... > list, F &&f)
| Counts the number of times the predicate F returns true. More...
template<typename F , typename... Ts>
constexpr auto | filter (type_list< Ts... > list, F &&f)
| Filters the list according to a constexpr predicate. More...
template<typename F , typename... Ts>
constexpr auto | find_first (type_list< Ts... > list, F &&f)
| Returns the first instance that matches the constexpr predicate. More...
template<typename F , typename... Ts>
constexpr auto | find_one (type_list< Ts... > list, F &&f)
| Returns the only instance that matches the constexpr predicate. More...
template<typename F , typename... Ts>
constexpr bool | contains (type_list< Ts... > list, F &&f)
| Returns true if any item in the list matches the predicate. More...
template<typename T , typename... Ts>
constexpr bool | contains (type_list< Ts... >)
| Returns true if the type_list contains the specified type. More...
template<typename T , typename... Ts>
constexpr bool | contains_base (const std::tuple< Ts... > &)
| Returns true if the tuple contains the specified type or a supertype. More...
template<template< typename... > typename T, typename... Ts>
constexpr bool | contains_instance (const std::tuple< Ts... > &)
| Returns true if the tuple contains an instance of the specified type. More...
template<typename... Ts, typename F >
constexpr auto | apply (type_list< Ts... >, F &&f)
| Applies a function to the elements of the type_list. More...
template<size_t N, typename... Ts>
constexpr auto & | get (std::tuple< Ts... > &ts) noexcept
| A synonym for std::get<N>(tuple). More...
template<size_t N, typename... Ts>
constexpr const auto & | get (const std::tuple< Ts... > &ts) noexcept
| A synonym for std::get<N>(tuple). More...
template<typename T , typename... Ts>
constexpr T & | get (std::tuple< Ts... > &ts) noexcept
| A synonym for std::get<T>(tuple). More...
template<typename T , typename... Ts>
constexpr const T & | get (const std::tuple< Ts... > &ts) noexcept
| A synonym for std::get<T>(tuple). More...
template<template< typename... > typename T, typename... Ts>
constexpr auto & | get_instance (std::tuple< Ts... > &ts) noexcept
| Returns the value of type U, where U is a template instance of T. More...
template<template< typename... > typename T, typename... Ts>
constexpr const auto & | get_instance (const std::tuple< Ts... > &ts) noexcept
| Returns the value of type U, where U is a template instance of T. More...
template<typename... Ts>
constexpr type_list< descriptor::type_descriptor< Ts >... > | reflect_types (type_list< Ts... >) noexcept
| Converts a type_list of types to a type_list of the type_descriptors for these types. More...
template<typename... Ts>
constexpr type_list< Ts... > | unreflect_types (type_list< descriptor::type_descriptor< Ts >... >) noexcept
| Converts a type_list of type_descriptors to a type_list of the target types. More...

## Detailed Description

Contains utility types and functions for working with those types.

## Typedef Documentation

## ◆ type_tag

template<typename T >

using refl::util::type_tag = typedef type_list<T>

## Function Documentation

## ◆ accumulate()

template<typename R , typename F , typename T , typename... Ts>

constexpr auto refl::util::accumulate | ( | type_list< T, Ts... > | ,
|  | F && | f,
|  | R && | initial_value
| ) |  |
| constexpr

## ◆ apply()

template<typename... Ts, typename F >

constexpr auto refl::util::apply | ( | type_list< Ts... > | ,
|  | F && | f
| ) |  |
| constexpr

Applies a function to the elements of the type_list.

apply(reflect_types(type_list<int, long, float>{}), [](auto td_int, auto td_long, auto td_float) {
return get_name(td_int) + " " +get_name(td_long) + " " + get_name(td_float);
})
-> "int long float"

## ◆ as_tuple()

template<typename... Ts>

constexpr std::tuple<Ts...> refl::util::as_tuple | ( | type_list< Ts... > |  | ) |
| constexpr noexcept

Creates a matching std::tuple from a type_list.

Types in the type_list must be Trivial.

## ◆ as_type_list()

template<typename... Ts>

constexpr type_list<Ts...> refl::util::as_type_list | ( | const std::tuple< Ts... > & |  | ) |
| constexpr noexcept

Creates a matching type_list from a std::tuple.

## ◆ contains() [1/2]

template<typename F , typename... Ts>

constexpr bool refl::util::contains | ( | type_list< Ts... > | list,
|  | F && | f
| ) |  |
| constexpr

Returns true if any item in the list matches the predicate.

Calling f(Ts{})... should be valid in a constexpr context.

## ◆ contains() [2/2]

template<typename T , typename... Ts>

constexpr bool refl::util::contains | ( | type_list< Ts... > |  | ) |
| constexpr

Returns true if the type_list contains the specified type.

See also
  refl::trait::contains

## ◆ contains_base()

template<typename T , typename... Ts>

constexpr bool refl::util::contains_base | ( | const std::tuple< Ts... > & |  | ) |
| constexpr

Returns true if the tuple contains the specified type or a supertype.

See also
  refl::trait::contains_base

## ◆ contains_instance()

template<template< typename... > typename T, typename... Ts>

constexpr bool refl::util::contains_instance | ( | const std::tuple< Ts... > & |  | ) |
| constexpr

Returns true if the tuple contains an instance of the specified type.

See also
  refl::trait::contains_instance

## ◆ count_if()

template<typename F , typename... Ts>

constexpr size_t refl::util::count_if | ( | type_list< Ts... > | list,
|  | F && | f
| ) |  |
| constexpr

Counts the number of times the predicate F returns true.

Note: Breaking changes introduced in v0.7.0: F can now no longer take a second index argument.

## ◆ filter()

template<typename F , typename... Ts>

constexpr auto refl::util::filter | ( | type_list< Ts... > | list,
|  | F && | f
| ) |  |
| constexpr

Filters the list according to a constexpr predicate.

Calling f(Ts{})... should be valid in a constexpr context.

filter(reflect_types(type_list<int, long, float>{}), [](auto td) {
return std::is_integral_v<typename decltype(td)::type>;
})
-> type_list<type_descriptor<int>, type_descriptor<long>>

## ◆ find_first()

template<typename F , typename... Ts>

constexpr auto refl::util::find_first | ( | type_list< Ts... > | list,
|  | F && | f
| ) |  |
| constexpr

Returns the first instance that matches the constexpr predicate.

Calling f(Ts{})... should be valid in a constexpr context.

## ◆ find_one()

template<typename F , typename... Ts>

constexpr auto refl::util::find_one | ( | type_list< Ts... > | list,
|  | F && | f
| ) |  |
| constexpr

Returns the only instance that matches the constexpr predicate.

If there is no match or multiple matches, fails with static_assert. Calling f(Ts{})... should be valid in a constexpr context.

## ◆ for_each()

template<typename F , typename... Ts>

constexpr void refl::util::for_each | ( | type_list< Ts... > | list,
|  | F && | f
| ) |  |
| constexpr

Applies function F to each type in the type_list.

F can optionally take an index of type size_t.

for_each(reflect_types(type_list<int, float, double>{}), [](auto td) {
std::cout << get_name(td) << '\n';
});

## ◆ get() [1/4]

template<size_t N, typename... Ts>

constexpr const auto& refl::util::get | ( | const std::tuple< Ts... > & | ts | ) |
| constexpr noexcept

A synonym for std::get<N>(tuple).

## ◆ get() [2/4]

template<typename T , typename... Ts>

constexpr const T& refl::util::get | ( | const std::tuple< Ts... > & | ts | ) |
| constexpr noexcept

A synonym for std::get<T>(tuple).

## ◆ get() [3/4]

template<size_t N, typename... Ts>

constexpr auto& refl::util::get | ( | std::tuple< Ts... > & | ts | ) |
| constexpr noexcept

A synonym for std::get<N>(tuple).

## ◆ get() [4/4]

template<typename T , typename... Ts>

constexpr T& refl::util::get | ( | std::tuple< Ts... > & | ts | ) |
| constexpr noexcept

A synonym for std::get<T>(tuple).

## ◆ get_instance() [1/2]

template<template< typename... > typename T, typename... Ts>

constexpr const auto& refl::util::get_instance | ( | const std::tuple< Ts... > & | ts | ) |
| constexpr noexcept

Returns the value of type U, where U is a template instance of T.

## ◆ get_instance() [2/2]

template<template< typename... > typename T, typename... Ts>

constexpr auto& refl::util::get_instance | ( | std::tuple< Ts... > & | ts | ) |
| constexpr noexcept

Returns the value of type U, where U is a template instance of T.

## ◆ identity()

template<typename T >

constexpr decltype(auto) refl::util::identity | ( | T && | t | ) |
| constexpr noexcept

Returns the input paratemeter as-is.

Useful for expanding variadic template lists when only one arguments is known to be present.

## ◆ ignore()

template<typename T = int, typename... Ts>

constexpr int refl::util::ignore | ( | Ts && | ... | ) |
| constexpr noexcept

Ignores all parameters.

Can take an optional template parameter specifying the return type of ignore. The return object is iniailized by {}.

## ◆ make_const() [1/2]

template<typename T >

constexpr const T& refl::util::make_const | ( | const T & | value | ) |
| constexpr noexcept

Adds const to the input reference.

## ◆ make_const() [2/2]

template<typename T >

constexpr const T& refl::util::make_const | ( | T & | value | ) |
| constexpr noexcept

Adds const to the input reference.

## ◆ make_const_string() [1/3]

constexpr const_string<0> refl::util::make_const_string | ( |  | ) |
| constexpr noexcept

Creates an empty instance of const_string<N>

See also
  refl::util::const_string

## ◆ make_const_string() [2/3]

constexpr const_string<1> refl::util::make_const_string | ( | char | ch | ) |
| constexpr noexcept

Creates an instance of const_string<N>

See also
  refl::util::const_string

## ◆ make_const_string() [3/3]

template<size_t N>

constexpr const_string<N - 1> refl::util::make_const_string | ( | const char(&) | str[N] | ) |
| constexpr noexcept

Creates an instance of const_string<N>

See also
  refl::util::const_string

## ◆ map_to_array()

template<typename T , typename F , typename... Ts>

constexpr auto refl::util::map_to_array | ( | type_list< Ts... > | list,
|  | F && | f
| ) |  |
| constexpr

Applies function F to each type in the type_list, aggregating the results in an array.

F can optionally take an index of type size_t.

map_to_array<std::string>(reflect_types(type_list<int, float, double>{}), [](auto td) {
return get_name(td).str();
})
-> std::array{std::string{"int"}, std::string{"float"}, std::string{"double"}}

## ◆ map_to_tuple()

template<typename F , typename... Ts>

constexpr auto refl::util::map_to_tuple | ( | type_list< Ts... > | list,
|  | F && | f
| ) |  |
| constexpr

Applies function F to each type in the type_list, aggregating the results in a tuple.

F can optionally take an index of type size_t.

map_to_tuple(reflect_types(type_list<int, float, double>{}), [](auto td) {
return get_name(td);
})
-> std::tuple{const_string{"int"}, const_string{"float"}, const_string{"double"}}

## ◆ operator!=() [1/3]

template<size_t N, size_t M>

constexpr bool refl::util::operator!= | ( | const char(&) | a[N],
|  | const const_string< M > & | b
| ) |  |
| constexpr noexcept

Compares a C-style string with a const_string for equality.

See also
  refl::util::const_string

## ◆ operator!=() [2/3]

template<size_t N, size_t M>

constexpr bool refl::util::operator!= | ( | const const_string< N > & | a,
|  | const char(&) | b[M]
| ) |  |
| constexpr noexcept

Compares a const_string with a C-style string for equality.

See also
  refl::util::const_string

## ◆ operator!=() [3/3]

template<size_t N, size_t M>

constexpr bool refl::util::operator!= | ( | const const_string< N > & | a,
|  | const const_string< M > & | b
| ) |  |
| constexpr noexcept

Compares two const_strings for equality.

See also
  refl::util::const_string

## ◆ operator+() [1/3]

template<size_t N, size_t M>

constexpr const_string<N + M - 1> refl::util::operator+ | ( | const char(&) | a[N],
|  | const const_string< M > & | b
| ) |  |
| constexpr noexcept

Concatenates a C-style string with a const_string.

See also
  refl::util::const_string

## ◆ operator+() [2/3]

template<size_t N, size_t M>

constexpr const_string<N + M - 1> refl::util::operator+ | ( | const const_string< N > & | a,
|  | const char(&) | b[M]
| ) |  |
| constexpr noexcept

Concatenates a const_string with a C-style string.

See also
  refl::util::const_string

## ◆ operator+() [3/3]

template<size_t N, size_t M>

constexpr const_string<N + M> refl::util::operator+ | ( | const const_string< N > & | a,
|  | const const_string< M > & | b
| ) |  |
| constexpr noexcept

Concatenates two const_strings together.

See also
  refl::util::const_string

## ◆ operator<<()

template<size_t N>

constexpr std::ostream& refl::util::operator<< | ( | std::ostream & | os,
|  | const const_string< N > & | str
| ) |  |
| constexpr noexcept

## ◆ operator==() [1/3]

template<size_t N, size_t M>

constexpr bool refl::util::operator== | ( | const char(&) | a[N],
|  | const const_string< M > & | b
| ) |  |
| constexpr noexcept

Compares a C-style string with a const_string for equality.

See also
  refl::util::const_string

## ◆ operator==() [2/3]

template<size_t N, size_t M>

constexpr bool refl::util::operator== | ( | const const_string< N > & | a,
|  | const char(&) | b[M]
| ) |  |
| constexpr noexcept

Compares a const_string with a C-style string for equality.

See also
  refl::util::const_string

## ◆ operator==() [3/3]

template<size_t N, size_t M>

constexpr bool refl::util::operator== | ( | const const_string< N > & | a,
|  | const const_string< M > & | b
| ) |  |
| constexpr noexcept

Compares two const_strings for equality.

See also
  refl::util::const_string

## ◆ reflect_types()

template<typename... Ts>

constexpr type_list<descriptor::type_descriptor<Ts>...> refl::util::reflect_types | ( | type_list< Ts... > |  | ) |
| constexpr noexcept

Converts a type_list of types to a type_list of the type_descriptors for these types.

reflect_types(type_list<int, float>{}) -> type_list<type_descriptor<int>, type_descriptor<float>>{}

## ◆ to_array()

template<typename T , typename... Ts>

constexpr std::array<T, sizeof...(Ts)> refl::util::to_array | ( | const std::tuple< Ts... > & | tuple | ) |
| constexpr noexcept

Creates an array of type 'T' from the provided tuple.

The common type T needs to be specified, in order to prevent any errors when using the overload taking an empty std::tuple (as there is no common type then).

## ◆ to_tuple()

template<typename T , size_t N>

constexpr auto refl::util::to_tuple | ( | const std::array< T, N > & | array | ) |
| constexpr noexcept

Creates a tuple from the provided array.

## ◆ unreflect_types()

template<typename... Ts>

constexpr type_list<Ts...> refl::util::unreflect_types | ( | type_list< descriptor::type_descriptor< Ts >... > |  | ) |
| constexpr noexcept

Converts a type_list of type_descriptors to a type_list of the target types.

unreflect_types(type_list<type_descriptor<int>, type_descriptor<float>>{}) -> type_list<int, float>{}
refl::util::apply
constexpr auto apply(type_list< Ts... >, F &&f)
Applies a function to the elements of the type_list.
Definition: refl.hpp:1857
refl::util::unreflect_types
constexpr type_list< Ts... > unreflect_types(type_list< descriptor::type_descriptor< Ts >... >) noexcept
Converts a type_list of type_descriptors to a type_list of the target types.
Definition: refl.hpp:1929
refl::util::reflect_types
constexpr type_list< descriptor::type_descriptor< Ts >... > reflect_types(type_list< Ts... >) noexcept
Converts a type_list of types to a type_list of the type_descriptors for these types.
Definition: refl.hpp:1916
refl::util::filter
constexpr auto filter(type_list< Ts... > list, F &&f)
Filters the list according to a constexpr predicate.
Definition: refl.hpp:1774
refl::util::map_to_tuple
constexpr auto map_to_tuple(type_list< Ts... > list, F &&f)
Applies function F to each type in the type_list, aggregating the results in a tuple.
Definition: refl.hpp:1653
refl::descriptor::get_name
constexpr auto get_name(Descriptor d) noexcept
Returns the full name of the descriptor.
Definition: refl.hpp:2655
refl::util::for_each
constexpr void for_each(type_list< Ts... > list, F &&f)
Applies function F to each type in the type_list.
Definition: refl.hpp:1686