rttr::enumeration Class - 0.9.7 |RTTR
rttr::enumeration Class Reference

The enumeration class provides several meta information about an enum. More...

#include <enumeration.h>

Public Member Functions

type get_declaring_type () const noexcept
 Returns the type of the class or struct that declares this enumeration. More...
 
variant get_metadata (const variant &key) const
 Returns the meta data for the given key key. More...
 
string_view get_name () const noexcept
 Returns the declared name of this enumeration. More...
 
array_range< string_viewget_names () const noexcept
 Returns all enum names registered for this enumeration. More...
 
type get_type () const noexcept
 Returns the type object of this enumeration. More...
 
type get_underlying_type () const noexcept
 Returns the underlying type (int, unsigned int, etc.) of this enumeration. More...
 
array_range< variantget_values () const noexcept
 Returns all enum values registered for this enumeration. More...
 
bool is_valid () const noexcept
 Returns true if this enumeration is valid, otherwise false. More...
 
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. More...
 
 operator bool () const noexcept
 Convenience function to check if this enumeration is valid or not. More...
 
bool operator!= (const enumeration &other) const noexcept
 Returns true if this enumeration is the not the same like the other. More...
 
bool operator== (const enumeration &other) const noexcept
 Returns true if this enumeration is the same like the other. More...
 
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_view if the value is not defined. More...
 

Detailed Description

The enumeration class provides several meta information about an enum.

A instance of an enumeration class can only be obtained from the type class or the property class. See type::get_enumeration() and property::get_enumeration().

For registration an enum, nested inside a class, see registration::class_<T>::enumeration() and for global enums see registration::enumeration().

Meta Information

An enumeration is described by it's declared name (get_name()), it's enumerator names (get_names()) and it's corresponding constant values (get_values()). The name is represented as string_view and the values are stored as the underlying enum value. When the enumeration was declared inside a class, then get_declaring_type() can be used to obtain the type of this class.

The conversion functions name_to_value(), value_to_name() allow conversion between the value representation of an enumeration and its literal representation.

Copying and Assignment

A enumeration object is lightweight and can be copied by value. However, each copy will refer to the same underlying enumeration.

Typical Usage

using namespace rttr;
enum class E_Alignment
{
AlignLeft = 0x0001,
AlignRight = 0x0002,
AlignHCenter = 0x0004,
AlignJustify = 0x0008
};
{
rttr::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)
);
}
enumeration enum_align = type::get_by_name("E_Alignment").get_enumeration();
if (enum_align)
{
E_Alignment enum_value = E_Alignment::AlignLeft;
string_view name = enum_align.value_to_name(enum_value);
std::cout << name; // prints "AlignLeft"
variant var = enum_align.name_to_value("AlignJustify");
enum_value = var.get_value<E_Alignment>(); // is now: "AlignJustify"
std::cout << var.to_int(); // prints "8";
std::cout << var.to_string(); // prints "AlignJustify";
}
See also
method, property, constructor and type

Member Function Documentation

type rttr::enumeration::get_declaring_type ( ) const
noexcept

Returns the type of the class or struct that declares this enumeration.

Remarks
When this enumeration does not belong to a class (i.e. is a global enumeration) it will return an invalid type object. When this enumeration is not valid, this function will return an invalid type object (see type::is_valid).
Returns
Type of the declaring class/struct for this enumeration.
variant rttr::enumeration::get_metadata ( const variant key) const

Returns the meta data for the given key key.

Remarks
When no meta data is registered with the given key, an invalid variant object is returned (see variant::is_valid).
Returns
A variant object, containing arbitrary data.
string_view rttr::enumeration::get_name ( ) const
noexcept

Returns the declared name of this enumeration.

Returns
Name of the enumeration.
array_range<string_view> rttr::enumeration::get_names ( ) const
noexcept

Returns all enum names registered for this enumeration.

Remarks
When the enumeration is invalid then an empty range is returned.
Returns
A range of enumeration names.
type rttr::enumeration::get_type ( ) const
noexcept

Returns the type object of this enumeration.

Returns
Data type of the enumeration.
type rttr::enumeration::get_underlying_type ( ) const
noexcept

Returns the underlying type (int, unsigned int, etc.) of this enumeration.

Returns
Data type of the enumeration.
array_range<variant> rttr::enumeration::get_values ( ) const
noexcept

Returns all enum values registered for this enumeration.

Remarks
When the enumeration is invalid then an empty range is returned.
Returns
A range of enumeration values.
bool rttr::enumeration::is_valid ( ) const
noexcept

Returns true if this enumeration is valid, otherwise false.

Returns
True if this enumeration is valid, otherwise false.
variant rttr::enumeration::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.

Returns
A variant object, containing the value for the given name.
rttr::enumeration::operator bool ( ) const
explicitnoexcept

Convenience function to check if this enumeration is valid or not.

Returns
True if this enumeration is valid, otherwise false.
bool rttr::enumeration::operator!= ( const enumeration other) const
noexcept

Returns true if this enumeration is the not the same like the other.

Returns
True if both enumerations are different, otherwise false.
bool rttr::enumeration::operator== ( const enumeration other) const
noexcept

Returns true if this enumeration is the same like the other.

Returns
True if both enumerations are equal, otherwise false.
string_view rttr::enumeration::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_view if the value is not defined.

Returns
A string_view object, containing the name for the given value.

The documentation for this class was generated from the following file: