rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Public Member Functions | List of all members
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...
 
std::vector< std::string > get_keys () const
 Returns all enum keys registered for this enumeration. More...
 
variant get_metadata (int key) const
 Returns the metadata for the given key key. More...
 
variant get_metadata (const std::string &key) const
 Returns the metadata for the given key key. More...
 
std::string get_name () const
 Returns the name of 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 key_to_value (const std::string &key) const
 Returns the value of the given enumeration key, or an empty variant if the key 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_key (detail::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. See type::get_enum() and type::get_enumerations().

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

Meta Information

An enumeration is described by it's name (get_name()), it's keys (get_keys()) and it's corresponding values (get_values()). The key 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 key_to_value(), value_to_key() 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("MyStruct").get_enumeration("E_Alignment");
if (enum_align)
{
MyStruct::E_Alignment enum_value = MyStruct::AlignLeft;
std::string key = enum_align.value_to_key(enum_value);
std::cout << key; // prints "AlignLeft"
variant var = enum_align.key_to_value("AlignJustify");
std::cout << var.get_value<MyStruct::E_Alignment>(); // prints "8";
}
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.
std::vector<std::string> rttr::enumeration::get_keys ( ) const

Returns all enum keys registered for this enumeration.

Remarks
When the enumeration is invalid then an empty vector is returned.
Returns
A vector of enumeration keys.
variant rttr::enumeration::get_metadata ( int  key) const

Returns the metadata for the given key key.

Remarks
When no metadata is registered with the given key, an invalid variant object is returned (see variant::is_valid).
Returns
A variant object, containing arbitrary data.
variant rttr::enumeration::get_metadata ( const std::string &  key) const

Returns the metadata for the given key key.

Remarks
When no metadata 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 name of this enumeration.

Returns
Name of the enumeration.
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::key_to_value ( const std::string &  key) const

Returns the value of the given enumeration key, or an empty variant if the key is not defined.

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

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_key ( detail::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 key for the given value.

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