rttr::enumeration Class - 0.9.5 |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
 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...
 
std::string get_name () const
 Returns the declared name of this enumeration. More...
 
std::vector< std::string > get_names () const
 Returns all enum names registered for this enumeration. More...
 
type get_type () const
 Returns the type object of this enumeration. More...
 
type get_underlying_type () const
 Returns the underlying type (int, unsigned int, etc.) of this enumeration. More...
 
std::vector< variantget_values () const
 Returns all enum values registered for this enumeration. More...
 
bool is_valid () const
 Returns true if this enumeration is valid, otherwise false. More...
 
variant name_to_value (const std::string &name) const
 Returns the value of the given enumeration name, or an empty variant if the name is not defined. More...
 
 operator bool () const
 Convenience function to check if this enumeration is valid or not. More...
 
bool operator!= (const enumeration &other) const
 Returns true if this enumeration is the not the same like the other. More...
 
bool operator== (const enumeration &other) const
 Returns true if this enumeration is the same like the other. More...
 
std::string value_to_name (argument value) const
 Returns the string that is used as the name of the given enumeration value, or an empty string 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 std::string 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;
struct MyStruct
{
enum E_Alignment
{
AlignLeft = 0x0001,
AlignRight = 0x0002,
AlignHCenter = 0x0004,
AlignJustify = 0x0008
};
};
enumeration enum_align = type::get_by_name("MyStruct").get_enumeration("E_Alignment");
if (enum_align)
{
MyStruct::E_Alignment enum_value = MyStruct::AlignLeft;
std::string name = enum_align.value_to_name(enum_value);
std::cout << name; // prints "AlignLeft"
variant var = enum_align.name_to_value("AlignJustify");
std::cout << var.get_value<MyStruct::E_Alignment>(); // prints "8";
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

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.
std::string rttr::enumeration::get_name ( ) const

Returns the declared name of this enumeration.

Returns
Name of the enumeration.
std::vector<std::string> rttr::enumeration::get_names ( ) const

Returns all enum names registered for this enumeration.

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

Returns the type object of this enumeration.

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

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

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

Returns all enum values registered for this enumeration.

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

Returns true if this enumeration is valid, otherwise false.

Returns
True if this enumeration is valid, otherwise false.
variant rttr::enumeration::name_to_value ( const std::string &  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
explicit

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

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

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

Returns
True if both enumerations are equal, otherwise false.
std::string rttr::enumeration::value_to_name ( argument  value) const

Returns the string that is used as the name of the given enumeration value, or an empty string if the value is not defined.

Returns
A std::string object, containing the name for the given value.

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