ponder 3.2
C++ reflection library
ponder::Enum Class Reference

ponder::Enum represents a metaenum composed of <name, value> pairs More...

#include <enum.hpp>

+ Inheritance diagram for ponder::Enum:

Classes

struct  Pair
 Structure defining the <name, value> pairs stored in metaenums. More...
 

Public Types

typedef long EnumValue
 Type used to hold the enum value.
 

Public Member Functions

IdReturn name () const
 Return the name of the metaenum. More...
 
size_t size () const
 Return the size of the metaenum. More...
 
Pair pair (size_t index) const
 Get a pair by its index. More...
 
bool hasName (IdRef name) const
 Check if the enum contains a name. More...
 
bool hasValue (EnumValue value) const
 Check if the enum contains a value. More...
 
template<typename E >
bool hasValue (E value) const
 Check if the enum contains a value. More...
 
IdReturn name (EnumValue value) const
 Return the name corresponding to given a value. More...
 
template<typename E >
IdReturn name (E value) const
 Return the name corresponding to given a value for enum class. More...
 
EnumValue value (IdRef name) const
 Return the value corresponding to given a name. More...
 
template<typename E >
value (IdRef name) const
 Return the value corresponding to given a name for enum class. More...
 
bool operator== (const Enum &other) const
 Operator == to check equality between two metaenums. More...
 
bool operator!= (const Enum &other) const
 Operator != to check inequality between two metaenums. More...
 

Static Public Member Functions

template<typename T >
static EnumBuilder declare (IdRef name=IdRef())
 Declare a new metaenum. More...
 
template<typename T >
static void undeclare ()
 Undeclare an existing metaenum. More...
 

Friends

class EnumBuilder
 
class detail::EnumManager
 

Related Functions

(Note that these are not member functions.)

size_t enumCount ()
 Get the total number of existing metaenums. More...
 
const EnumenumByName (IdRef name)
 Get a metaenum from its name. More...
 
template<typename T >
const EnumenumByObject (T value)
 Get a metaenum from a C++ object. More...
 
template<typename T >
const EnumenumByType ()
 Get a metaenum from its C++ type. More...
 
template<typename T >
const EnumenumByTypeSafe ()
 Get a metaenum from its C++ type. More...
 

Detailed Description

ponder::Enum represents a metaenum composed of <name, value> pairs

Enums are declared, bound to a C++ type and filled with the declare template function.

enum MyEnum {one = 1, two = 2, ten = 10};
ponder::Enum::declare<MyEnum>("MyEnum")
.value("one", one)
.value("two", two)
.value("ten", ten);

It then provides a set of accessors to retrieve names, values and pairs contained in it.

const ponder::Enum& metaenum = ponder::enumByType<MyEnum>();
bool b1 = metaenum.hasName("one"); // b1 == true
bool b2 = metaenum.hasValue(5); // b2 == false
Id s = metaenum.name(10); // s == "ten"
EnumValue l = metaenum.value("two"); // l == 2
ponder::Enum::Pair p = metaenum.pair(0); // p == {"one", one}
Remarks
All values and names are unique within the metaenum.
See also
Class, EnumBuilder

Member Function Documentation

◆ declare()

template<typename T >
static EnumBuilder ponder::Enum::declare ( IdRef  name = IdRef())
static

Declare a new metaenum.

This is the function to call to create a new metaenum. The template parameter T is the C++ enum type that will be bound to the metaclass.

Parameters
nameName of the metaenum in Ponder. This name identifies the metaenum and thus has to be unique
Returns
A EnumBuilder object that will provide functions to fill the new metaenum with values.

◆ hasName()

bool ponder::Enum::hasName ( IdRef  name) const

Check if the enum contains a name.

Parameters
nameName to check
Returns
True if the metaenum contains a pair whose name is name

◆ hasValue() [1/2]

template<typename E >
bool ponder::Enum::hasValue ( value) const
inline

Check if the enum contains a value.

This generic version of hasValue() allows enum classes to be queried.

Parameters
valueValue to check
Returns
True if the metaenum contains a pair whose value is value

◆ hasValue() [2/2]

bool ponder::Enum::hasValue ( EnumValue  value) const

Check if the enum contains a value.

Parameters
valueValue to check
Returns
True if the metaenum contains a pair whose value is value

◆ name() [1/3]

IdReturn ponder::Enum::name ( ) const

Return the name of the metaenum.

Returns
String containing the name of the metaenum

◆ name() [2/3]

template<typename E >
IdReturn ponder::Enum::name ( value) const
inline

Return the name corresponding to given a value for enum class.

Parameters
valueValue to get
Returns
Name of the requested value
Exceptions
InvalidEnumValuevalue doesn't exist in the metaenum

◆ name() [3/3]

IdReturn ponder::Enum::name ( EnumValue  value) const

Return the name corresponding to given a value.

Parameters
valueValue to get
Returns
Name of the requested value
Exceptions
InvalidEnumValuevalue doesn't exist in the metaenum

◆ operator!=()

bool ponder::Enum::operator!= ( const Enum other) const

Operator != to check inequality between two metaenums.

Parameters
otherMetaenum to compare with this
Returns
True if metaenums are different, false if they are equal

◆ operator==()

bool ponder::Enum::operator== ( const Enum other) const

Operator == to check equality between two metaenums.

Two metaenums are equal if their name is the same.

Parameters
otherMetaenum to compare with this
Returns
True if both metaenums are the same, false otherwise

◆ pair()

Pair ponder::Enum::pair ( size_t  index) const

Get a pair by its index.

Parameters
indexIndex of the pair to get
Returns
index-th pair
Exceptions
OutOfRangeindex is out of range

◆ size()

size_t ponder::Enum::size ( ) const

Return the size of the metaenum.

Returns
Total number of values contained in the metaenum

◆ undeclare()

template<typename T >
static void ponder::Enum::undeclare ( )
static

Undeclare an existing metaenum.

Call this to undeclare an Enum that you no longer require.

Parameters
nameName of the existing metaenum in Ponder.
Note
See notes for Class::undeclare about registration.
See also
Class::undeclare

◆ value() [1/2]

EnumValue ponder::Enum::value ( IdRef  name) const

Return the value corresponding to given a name.

Parameters
nameName to get
Returns
Value of the requested name
Exceptions
InvalidEnumNamename doesn't exist in the metaenum

◆ value() [2/2]

template<typename E >
E ponder::Enum::value ( IdRef  name) const
inline

Return the value corresponding to given a name for enum class.

Enum classes are strongly typed so the return type needs to be specified, e.g. MyEnum a = enum.value<MyEnum>("one");

Parameters
nameName to get
Returns
Value of the requested name as requested type
Exceptions
InvalidEnumNamename doesn't exist in the metaenum

Friends And Related Function Documentation

◆ enumByName()

const Enum & enumByName ( IdRef  name)
related

Get a metaenum from its name.

Parameters
nameName of the metaenum to retrieve (case sensitive)
Returns
Reference to the requested metaenum
Exceptions
EnumNotFoundname is not a valid metaenum name

◆ enumByObject()

template<typename T >
const Enum & enumByObject ( value)
related

Get a metaenum from a C++ object.

It is equivalent to calling enumByType<T>(index).

Parameters
valueValue to get the metaenum of
Returns
Reference to the metaenum bound to type T
Exceptions
EnumNotFoundno metaenum has been declared for T

◆ enumByType()

template<typename T >
const Enum & enumByType ( )
related

Get a metaenum from its C++ type.

Returns
Reference to the metaenum bound to type T
Exceptions
EnumNotFoundno metaenum has been declared for T

◆ enumByTypeSafe()

template<typename T >
const Enum * enumByTypeSafe ( )
related

Get a metaenum from its C++ type.

Returns
Pointer to the metaenum bound to type T, or null pointer if no metaenum has been declared

◆ enumCount()

size_t enumCount ( )
related

Get the total number of existing metaenums.

Returns
Global metaenum count

The documentation for this class was generated from the following files:
ponder::Enum::pair
Pair pair(size_t index) const
Get a pair by its index.
ponder::Enum::hasName
bool hasName(IdRef name) const
Check if the enum contains a name.
ponder::Enum::EnumValue
long EnumValue
Type used to hold the enum value.
Definition: enum.hpp:81
ponder::Enum::name
IdReturn name() const
Return the name of the metaenum.
ponder::Enum
ponder::Enum represents a metaenum composed of <name, value> pairs
Definition: enum.hpp:78
ponder::Enum::Pair
Structure defining the <name, value> pairs stored in metaenums.
Definition: enum.hpp:86
ponder::Enum::value
EnumValue value(IdRef name) const
Return the value corresponding to given a name.
ponder::Enum::hasValue
bool hasValue(EnumValue value) const
Check if the enum contains a value.