Enums
RTTR allows also to register enumerated constants (enums). Therefore use the function registration::enumeration().
It has following synopsis:
template<typename Enum_Type>
registration rttr::registration::enumeration( string_view name );
static bind< detail::enum_, detail::invalid_type, Enum_Type > enumeration(string_view name)
Register a global enumeration of type Enum_Type.
- name the declared name of this enum
#include <rttr/registration>
using namespace rttr;
enum class E_Alignment
{
AlignLeft = 0x0001,
AlignRight = 0x0002,
AlignHCenter = 0x0004,
AlignJustify = 0x0008
};
{
registration::enumeration<E_Alignment>("E_Alignment")
(
value("AlignLeft", E_Alignment::AlignLeft),
value("AlignRight", E_Alignment::AlignRight),
value("AlignHCenter", E_Alignment::AlignHCenter),
value("AlignJustify", E_Alignment::AlignJustify)
);
}
Definition access_levels.h:34
detail::enum_data< Enum_Type > value(string_view, Enum_Type value)
The value function should be used to add a mapping from enum name to value during the registration pr...
#define RTTR_REGISTRATION
Use this macro to automatically register your reflection information to RTTR before main is called.
Definition registration.h:745
In order to add the individual enumerators you have to use the () operator of the returned bind object. Then you call for every enumerator you want to add, the function value().
It has following synopsis:
template<typename Enum_Type>
basic_string_view< char > string_view
A class to hold a reference to a continuous sequence of char objects.
Definition string_view.h:493
The name is a string_view and the value is the enum value.
The class enumeration contains several meta information about an enum with conversion functions between the value representation and its literal representation.
How to use the enumeration class shows following example:
using namespace rttr;
if (enum_type && enum_type.is_enumeration())
{
enumeration enum_align = enum_type.get_enumeration();
std::string name = enum_align.value_to_name(E_Alignment::AlignHCenter);
std::cout << name; // prints "AlignHCenter"
variant var = enum_align.name_to_value(name);
}
The enumeration class provides several meta information about an enum.
Definition enumeration.h:113
string_view value_to_name(argument value) const
Returns the string_view that is used as the name of the given enumeration value, or an empty string_v...
variant name_to_value(string_view name) const
Returns the value of the given enumeration name, or an empty variant if the name is not defined.
bool is_enumeration() const noexcept
Returns true whether the given type represents an enumeration.
static type get_by_name(string_view name) noexcept
Returns the type object with the given name name.
enumeration get_enumeration() const noexcept
Returns the enumerator if this type is an enum type; otherwise the returned value is not valid.
The variant class allows to store data of any type and convert between these types transparently.
Definition variant.h:198
- Remarks
- You can also use the variant class to convert from an enum value to is integral or string representation. variant var = E_Alignment::AlignHCenter;std::cout << var.to_int() << std::endl; // prints '4'std::cout << var.to_string() << std::endl; // prints 'AlignHCenter'std::string to_string(bool *ok=nullptr) constReturns the containing variant as a std::string when the type is a std::string.int to_int(bool *ok=nullptr) constReturns the containing variant as an int when the type is an integer.
Generated on Thu Jul 3 2025 04:49:29 for rttr - 0.9.6 by doxygen.