class Enum

Wrapper class featuring range-checking and string representation of enumerated values. More...

Definition#include <lib/Enum.h>
Template formEnum<templatetypename E>
InheritsEnumBase [private]
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

Public Methods

Public Static Methods


Detailed Description

Wrapper class featuring range-checking and string representation of enumerated values.

The string representation capability is needed to provide a generic input frontend for any Enumeration because text labels are needed in GUIs, and, of course, aids debugging, because you can provide a readable presentation of an entry if something goes wrong.

NOTE, that wrapping an enumeration with this class does not mean any performance overhead at all since the compiler optimizes the member calls away. Nor does an instance of this class use more memory than the use of an usual Enumeration. (i.e. sizeof(E) == sizeof(Enum<E>)).

Besides that, it provides a great opportunity to check the code and make it bug free, esp. if you've to read the Enumeration from some dubious integer source (.. to make the dubious integer source bug free ;-)

So there is no reason, not to use this class. Alas, you've to provide a StringRepresentation for it, which may be tedious for large enumerations. To make the Definition easier and more readable, an ENUM_DEFINITION() macro is provided.

 Enum () : value(staticData.defaultValue)

default constructor. Initialize with default value.

 Enum (E init) : value(init)

initialize with Enumeration given.

 Enum (const std::string& s) : value(getValueFor(s))

initialize with the string representation XXX: throw Exception if not found ?

inline Enum&  operator = (E setval)

assign an Enumeration of this type. In debug version, assert, that it is really in the Range of this Enumeration.

inline Enum&  operator = (const Enum& rhs)

inline  operator E ()

[const]

returns the enumeration value hold with this enum.

std::string  toString ()

[const]

returns the String representation for the value represented by this instance.

 operator const char * ()

[const]

returns the C string representation for the value represented by this instance.

bool  inRange (long i)

[static]

This static member returns true, if the integer value given fits int the range of this Enumeration. Use this to verify input/output. Fitting in the range of Enumeration here means, that there actually exists a String representation for it, so this Enumeration is needed to be initialized properly in its Enum<E>::sdata::sdata() constructor, you've to provide. For convenience, use the ENUM_DEFINITION() macro for this.

std::string  getEnumName ()

[static]

returns the Name for this enumeration. Useful for error reporting.

std::string  getStringFor (E e)

[static]

gives the String represenatation of a specific value of this Enumeration.

E  getValueFor (const std::string &s)

[static]

returns the Value for a specific String. XXX: throw OutOfRangeException ?