conststr 0.2.1
Loading...
Searching...
No Matches
Classes | Typedefs | Functions | Variables
reflect Namespace Reference

Compile-time reflection for aggregate types. More...

Classes

struct  any_type
 Type that can be implicitly converted to any type. More...
 
struct  cptr
 Wrapper of pointer type. More...
 

Typedefs

template<typename T , std::size_t N>
using type_of = std::remove_cvref_t< std::tuple_element_t< N, decltype(to_tuple(fake_obj< T >))> >
 Type of N-th member of a default-constructible aggregate type T. More...
 
template<typename T , conststr::cstr Name>
using type_of_member = type_of< T, index_of< T, Name > >
 Get the type of the member by its name. More...
 

Functions

template<typename T , typename... Args>
requires (std::is_aggregate_v<std::remove_cvref_t<T>> && std::is_default_constructible_v<std::remove_cvref_t<T>>)
consteval std::size_t number_of_members_impl ()
 Internal implementation of number_of_members. Get The number of a default-constructible aggregate type's members. More...
 
template<class T , std::size_t N = number_of_members<T>>
requires (N <= 128)
constexpr decltype(auto) to_tuple (T &&t)
 Convert a value of type T to a tuple containing references to all its members. More...
 
template<typename T , std::size_t N>
consteval auto cptr_of_member ()
 Get the pointer of the N-th member as a constexpr value. More...
 
template<cptr Ptr>
consteval auto pretty_name ()
 Core function to get member name via compiler built-in macro. More...
 
template<conststr::cstr Name>
consteval auto basename_of_impl ()
 Internal implementation of basename_of. get the Base name of a full qualified name. More...
 
template<cptr Ptr>
consteval auto name_of_ptr_impl ()
 Internal implementation of name_of_ptr. Extract the real name from the output of pretty_name(). More...
 
template<typename T , conststr::cstr Name, std::size_t SrchIdx>
requires (number_of_members<T> > SrchIdx)
constexpr size_t index_of_impl ()
 Internal implementation of index_of. Get the index of the member by its name. More...
 
template<std::size_t N, typename T >
constexpr decltype(auto) member_of (T &&t)
 Get member reference of object t. More...
 
template<conststr::cstr Name, typename T >
requires std::same_as<typename decltype(Name)::value_type, char>
constexpr decltype(auto) member_of (T &&t)
 Get member reference of object t via member's name. More...
 

Variables

template<typename T >
const T fake_obj
 An object of T that can be used in constant context without really construct it. More...
 
template<typename T >
constexpr std::size_t number_of_members = number_of_members_impl<T>()
 The number of a default-constructible aggregate type's members. More...
 
template<conststr::cstr Name>
constexpr auto basename_of = basename_of_impl<Name>()
 Base name of a full qualified name. More...
 
template<cptr Ptr>
constexpr auto name_of_ptr = name_of_ptr_impl<Ptr>()
 Get the variable or class member name from its pointer. More...
 
template<typename T , std::size_t N>
constexpr auto name_of = name_of_ptr<cptr_of_member<T, N>()>
 Name of N-th member of a default-constructible aggregate type T. More...
 
template<typename T , conststr::cstr Name>
constexpr size_t index_of = index_of_impl<T, Name, 0>()
 Get the index of the member by its name. More...
 

Detailed Description

Compile-time reflection for aggregate types.

Typedef Documentation

◆ type_of

template<typename T , std::size_t N>
using reflect::type_of = typedef std::remove_cvref_t< std::tuple_element_t<N, decltype(to_tuple(fake_obj<T>))> >

Type of N-th member of a default-constructible aggregate type T.

Template Parameters
Tany default-constructible aggregate type
Nindex of member

◆ type_of_member

template<typename T , conststr::cstr Name>
using reflect::type_of_member = typedef type_of<T, index_of<T, Name> >

Get the type of the member by its name.

This is just a simplified API of reflect::type_of<T, reflect::index_of<T, Name>>.

Template Parameters
Tany default-constructible aggregate type
Namename of the member to search

Function Documentation

◆ basename_of_impl()

template<conststr::cstr Name>
consteval auto reflect::basename_of_impl ( )

Internal implementation of basename_of. get the Base name of a full qualified name.

Template Parameters
Namestring containing a name that may be quailfied
Returns
Base name of a full qualified name.
See also
basename_of

◆ cptr_of_member()

template<typename T , std::size_t N>
consteval auto reflect::cptr_of_member ( )

Get the pointer of the N-th member as a constexpr value.

Template Parameters
Tany type
Nindex of the member
Returns
cptr wrapped pointer.

◆ index_of_impl()

template<typename T , conststr::cstr Name, std::size_t SrchIdx>
requires (number_of_members<T> > SrchIdx)
constexpr size_t reflect::index_of_impl ( )
constexpr

Internal implementation of index_of. Get the index of the member by its name.

Template Parameters
Tany default-constructible aggregate type
Namename of the member to search
SrchIdxfor recursion only, keep it 0
Returns
Index of the member.
See also
index_of

◆ member_of() [1/2]

template<std::size_t N, typename T >
constexpr decltype(auto) reflect::member_of ( T &&  t)
constexpr

Get member reference of object t.

Please use decltype(auto) for deducing the return value type. For example:

struct S {
int x;
int y;
int z;
};
S s = {};
decltype(auto) zref = reflect::member_of<2>(s);
zref = 10;
if (s.z == 10)
std::cout << "Ok" << std::endl;
Template Parameters
Nindex of member
TDO NOT specify it, let it be automatically deduced
Parameters
tobject of type T
Returns
L-value reference if t is a l-value reference.
R-value reference if t is a r-value reference.

◆ member_of() [2/2]

template<conststr::cstr Name, typename T >
requires std::same_as<typename decltype(Name)::value_type, char>
constexpr decltype(auto) reflect::member_of ( T &&  t)
constexpr

Get member reference of object t via member's name.

Please use decltype(auto) for deducing the return value type. For example:

struct S {
int x;
int y;
int z;
};
S s = {};
decltype(auto) zref = reflect::member_of<"z">(s);
zref = 10;
if (s.z == 10)
std::cout << "Ok" << std::endl;
Template Parameters
Namename of member
TDO NOT specify it, let it be automatically deduced
Parameters
tobject of type T
Returns
L-value reference if t is a l-value reference.
R-value reference if t is a r-value reference.

◆ name_of_ptr_impl()

template<cptr Ptr>
consteval auto reflect::name_of_ptr_impl ( )

Internal implementation of name_of_ptr. Extract the real name from the output of pretty_name().

Template Parameters
Ptrpointer to the the variable or class member you want to reflect.
Returns
Name of the variable or class member.
See also
name_of_ptr

◆ number_of_members_impl()

template<typename T , typename... Args>
requires (std::is_aggregate_v<std::remove_cvref_t<T>> && std::is_default_constructible_v<std::remove_cvref_t<T>>)
consteval std::size_t reflect::number_of_members_impl ( )

Internal implementation of number_of_members. Get The number of a default-constructible aggregate type's members.

Just call number_of_members_impl<T>() and keep the template pack Args empty. On how it works, it first tests whether T can be constructed with one parameter, if so, then tries to construct it with two parameters, and so on until T cannot be constructed with N parameters, then the number of members of T is N - 1.

Template Parameters
Tmust be a default-constructible aggregate type
Argsfor recursion only, keep it empty
Returns
The number of T's members.
See also
number_of_members

◆ pretty_name()

template<cptr Ptr>
consteval auto reflect::pretty_name ( )

Core function to get member name via compiler built-in macro.

Note
When using GCC, will return a string in the form of "consteval auto reflect::pretty_name() [with cptr<...auto...> Ptr = ...]".
When using clang, will return a string in the form of "auto reflect::pretty_name() [Ptr = ...]".
When using MSVC, will return a string in the form of "auto __cdecl reflect::pretty_name<...>(void)".
Template Parameters
Ptrpointer to the the variable or member you want to reflect
Returns
A compile-time string containing the name of the variable or member and of type conststr::cstr.
See also
conststr::cstr
name_of()

◆ to_tuple()

template<class T , std::size_t N = number_of_members<T>>
requires (N <= 128)
constexpr decltype(auto) reflect::to_tuple ( T &&  t)
constexpr

Convert a value of type T to a tuple containing references to all its members.

Note
Only supports 128 members at most.
Template Parameters
Tany type.
Nnumber of T's members, automatically calculated if T is a default constructible aggregate type. Otherwise, the caller needs to provide it.
Returns
Tuple containing references to all t's members.

Variable Documentation

◆ basename_of

template<conststr::cstr Name>
constexpr auto reflect::basename_of = basename_of_impl<Name>()
constexpr

Base name of a full qualified name.

For example, basename_of<"xxx::yyy::zzz->nnn"_cs> is "nnn"_cs.

Template Parameters
NameName the name that may be quailfied.

◆ fake_obj

template<typename T >
const T reflect::fake_obj
extern

An object of T that can be used in constant context without really construct it.

Template Parameters
Tany type

◆ index_of

template<typename T , conststr::cstr Name>
constexpr size_t reflect::index_of = index_of_impl<T, Name, 0>()
constexpr

Get the index of the member by its name.

For example, reflect::index_of<S, "point"> if the type S has a member named point.

Template Parameters
Tany default-constructible aggregate type
Namename of the member to search
Returns
Index of the member.

◆ name_of

template<typename T , std::size_t N>
constexpr auto reflect::name_of = name_of_ptr<cptr_of_member<T, N>()>
constexpr

Name of N-th member of a default-constructible aggregate type T.

Template Parameters
Tany default-constructible aggregate type
Nindex of member

◆ name_of_ptr

template<cptr Ptr>
constexpr auto reflect::name_of_ptr = name_of_ptr_impl<Ptr>()
constexpr

Get the variable or class member name from its pointer.

Template Parameters
Ptrpointer to the the variable or class member you want to reflect.
Note
MSVC currently fails to compile it when Ptr points to an array.

◆ number_of_members

template<typename T >
constexpr std::size_t reflect::number_of_members = number_of_members_impl<T>()
constexpr

The number of a default-constructible aggregate type's members.

Template Parameters
Tmust be a default-constructible aggregate type