plptools
Loading...
Searching...
No Matches
Enum< E > Class Template Reference

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

#include <Enum.h>

Inheritance diagram for Enum< E >:
Collaboration diagram for Enum< E >:

Classes

struct  sdata
 

Public Member Functions

 Enum ()
 default constructor. More...
 
 Enum (E init)
 initialize with Enumeration given. More...
 
 Enum (const std::string &s)
 initialize with the string representation XXX: throw Exception if not found ? More...
 
Enumoperator= (E setval)
 assign an Enumeration of this type. More...
 
Enumoperator= (const Enum &rhs)
 
 operator E () const
 returns the enumeration value hold with this enum. More...
 
std::string toString () const
 returns the String representation for the value represented by this instance. More...
 
 operator const char * () const
 returns the C string representation for the value represented by this instance. More...
 

Static Public Member Functions

static bool inRange (long i)
 This static member returns true, if the integer value given fits int the range of this Enumeration. More...
 
static std::string getEnumName ()
 returns the Name for this enumeration. More...
 
static std::string getStringFor (E e)
 gives the String represenatation of a specific value of this Enumeration. More...
 
static E getValueFor (const std::string &s)
 returns the Value for a specific String. More...
 

Public Attributes

value
 The actual value hold by this instance. More...
 

Static Public Attributes

static sdata staticData
 

Detailed Description

template<typename E>
class Enum< E >

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, a macro is provided.

See also
ENUM_DEFINITION_BEGIN and ENUM_DEFINITION_END
Author
Henner Zeller

Definition at line 136 of file Enum.h.

Constructor & Destructor Documentation

◆ Enum() [1/3]

template<typename E >
Enum< E >::Enum ( )
inline

default constructor.

Initialize with default value.

Definition at line 167 of file Enum.h.

◆ Enum() [2/3]

template<typename E >
Enum< E >::Enum ( init)
inline

initialize with Enumeration given.

Definition at line 172 of file Enum.h.

◆ Enum() [3/3]

template<typename E >
Enum< E >::Enum ( const std::string &  s)
inline

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

Definition at line 183 of file Enum.h.

Member Function Documentation

◆ getEnumName()

template<typename E >
static std::string Enum< E >::getEnumName ( )
inlinestatic

returns the Name for this enumeration.

Useful for error reporting.

Definition at line 241 of file Enum.h.

◆ getStringFor()

template<typename E >
static std::string Enum< E >::getStringFor ( e)
inlinestatic

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

Definition at line 247 of file Enum.h.

◆ getValueFor()

template<typename E >
static E Enum< E >::getValueFor ( const std::string &  s)
inlinestatic

returns the Value for a specific String.

XXX: throw OutOfRangeException ?

Definition at line 255 of file Enum.h.

◆ inRange()

template<typename E >
static bool Enum< E >::inRange ( long  i)
inlinestatic

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_BEGIN and ENUM_DEFINITION_END macros for this.

Definition at line 233 of file Enum.h.

◆ operator const char *()

template<typename E >
Enum< E >::operator const char * ( ) const
inline

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

Definition at line 220 of file Enum.h.

◆ operator E()

template<typename E >
Enum< E >::operator E ( ) const
inline

returns the enumeration value hold with this enum.

Definition at line 208 of file Enum.h.

◆ operator=() [1/2]

template<typename E >
Enum & Enum< E >::operator= ( const Enum< E > &  rhs)
inline

Definition at line 198 of file Enum.h.

◆ operator=() [2/2]

template<typename E >
Enum & Enum< E >::operator= ( setval)
inline

assign an Enumeration of this type.

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

Definition at line 192 of file Enum.h.

◆ toString()

template<typename E >
std::string Enum< E >::toString ( ) const
inline

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

Definition at line 214 of file Enum.h.

Member Data Documentation

◆ staticData

template<typename E >
Enum< E >::sdata Enum< E >::staticData
static

Definition at line 155 of file Enum.h.

◆ value

template<typename E >
E Enum< E >::value

The actual value hold by this instance.

Definition at line 160 of file Enum.h.


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