Honeycomb  0.1
Component-Model Framework
Classes | Macros
Enumeration Classes

Factory method to generate type-safe enumeration classes with runtime reflection. More...

Classes

struct  honey::EnumError
 
class  honey::EnumElem
 Base class of all generated enum classes. A single element in the enumeration. See Enumeration Classes. More...
 
struct  honey::EnumInfo_< EnumType >::Elem
 
class  honey::EnumInfo_< EnumType >
 Run-time info about an enum class. Contains a list of elements and maps for element lookups. See Enumeration Classes for more info and examples. More...
 

Macros

#define ENUM(Base, Class)
 Enum class factory method. See Enumeration Classes for more info. More...
 

Detailed Description

Factory method to generate type-safe enumeration classes with runtime reflection.

To generate an enum class, define ENUM_LIST and call ENUM():

#define ENUM_LIST(e,_)      e(_, elem_name)     e(_, elem_name, elem_string_id, elem_value)
ENUM(BaseClass, EnumClass);
#undef ENUM_LIST

BaseClass is the name of the enclosing class scope, which will be used to create the enum's classId.
EnumClass is the name of the enum class that will be generated.
If there is no base class then leave it empty. If the base class parameter has a comma then it must be bracketed.

See also
EnumInfo_, reflection info returned by EnumClass::enumInfo()

Example:

struct Vehicle
{
#define ENUM_LIST(e,_) \
e(_, car) \
e(_, truck) \
e(_, bus, "school bus", 10) \
e(_, plane, "airplane", Bus+1) \
e(_, boat, , 12) \ //Leave string id field empty to use default ("boat")
e(_, train, "choo-choo", ) \ //Leave value field empty to use default (boat+1)
ENUM(Vehicle, Type);
#undef ENUM_LIST
};
void func()
{
Vehicle::Type v = Vehicle::Type::car; //Construct from enum constant
v = Vehicle::Type("car"); //Construct from id
v = Vehicle::Type(0); //Construct from value
if (v == Vehicle::Type::car) {} //True
Animal::Type a = Vehicle::Type::car; //Won't compile
if (v == Animal::Type::dog) {} //Won't compile
if (v == a) {} //Won't compile
if (v == 0) {} //Won't compile
switch (v) //Implicit cast to int
{
case Vehicle::Type::car:
break;
}
//Print all vehicle types
const Vehicle::Type::EnumInfo::ElemList& list = Vehicle::Type::enumInfo().elemList();
for (szt i = 0; i < list.size(); ++i) { debug_print(sout() << list[i].id << endl); }
}

Macro Definition Documentation

#define ENUM (   Base,
  Class 
)

Enum class factory method. See Enumeration Classes for more info.