|
|
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
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.
See also: ENUM_DEFINITION
sdata (struct) | sdata |
static sdata staticData | staticData |
E value | value |
Enum ()
| Enum |
default constructor. Initialize with default value.
Enum (E init)
| Enum |
initialize with Enumeration given.
Enum (const std::string& s)
| Enum |
initialize with the string representation XXX: throw Exception if not found ?
inline Enum& operator = (E setval)
| operator = |
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)
| operator = |
inline operator E ()
| E |
[const]
returns the enumeration value hold with this enum.
std::string toString ()
| toString |
[const]
returns the String representation for the value represented by this instance.
operator const char * ()
| operator const char * |
[const]
returns the C string representation for the value represented by this instance.
bool inRange (long i)
| inRange |
[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
std::string getEnumName ()
| getEnumName |
[static]
returns the Name for this enumeration. Useful for error reporting.
std::string getStringFor (E e)
| getStringFor |
[static]
gives the String represenatation of a specific value of this Enumeration.
E getValueFor (const std::string &s)
| getValueFor |
[static]
returns the Value for a specific String. XXX: throw OutOfRangeException ?