Main Page   Compound List   File List   Compound Members   File Members  

EnumTranslate Class Template Reference

Translate between an enumeration value and a string. More...

List of all members.

Public Methods

const T enumVal (const char *name)
 Given a name, return the associated enumeration value or the enumeration for zero (which is assumed to be invalid). More...

const char * enumName (T val)
 Given an enumeration value, return the associated character string (the name of the enumeration). More...


Static Protected Attributes

EnumTable table []
 Table of enumeration values and names. More...


Detailed Description

template<class T>
class EnumTranslate< T >

Translate between an enumeration value and a string.

Enumerations sets and enumeration values provide a way to represent named values in software. In many cases a translation is needed between the enumeration value (which is an unsigned integer) and its name. For example, the enumeration value CountZero and the string "CountZero".

The EnumTranslate class supports this translation. The class defines a structure consisting of a "tuple" composed of the enumeration and enumeraton name (a const char *) pair. A static table of these values is also defined. This table is initialized with a static initializer.

This table is ordered by enumeration values, allowing the enumeration value to serve as an index to fetch the enumeration name. The last entry in the table consists of the tuple composed of the enumeration value for 0 (which this code assumes is not value) and the null string. This allows the table to be searched (until the null entry is found) to find the string that corresponds to a given enumeration value.

The enumeration set always starts with badEnum and ends with lastEnum.

The classes that instantiate this template package the enumeration within the class. This avoid enumerations in the global name space.

Definition at line 40 of file enumTranslate.cpp.


Member Function Documentation

template<class T>
const char* EnumTranslate< T >::enumName T   val [inline]
 

Given an enumeration value, return the associated character string (the name of the enumeration).

Definition at line 65 of file enumTranslate.cpp.

Referenced by Fruit::enumName(), and Numbers::enumName().

00066    {
00067       const char *name = table[val].name;
00068       return name;
00069    }

template<class T>
const T EnumTranslate< T >::enumVal const char *   name [inline]
 

Given a name, return the associated enumeration value or the enumeration for zero (which is assumed to be invalid).

Definition at line 47 of file enumTranslate.cpp.

Referenced by Fruit::enumVal(), and Numbers::enumVal().

00048    {
00049       int retval = 0;
00050 
00051       size_t i;
00052       for (i = 1; table[i].name != 0; i++) {
00053          if (strcmp( name, table[i].name ) == 0) {
00054             retval = (int)table[i].enumeration;
00055             break;
00056          }
00057       }
00058       return (T)retval;
00059    }


Member Data Documentation

template<class T>
EnumTranslate< Fruit::fruit >::EnumTable EnumTranslate< T >::table [static, protected]
 

Initial value:

 {
   {Numbers::badEnum,   "bad enum"},
   {Numbers::zero,      "zero"},
   {Numbers::one,       "one"},
   {Numbers::two,       "two"},
   {Numbers::three,     "three"},
   {Numbers::four,      "four"},
   {(Numbers::numbers)0, 0}
}
Table of enumeration values and names.

Definition at line 177 of file enumTranslate.cpp.


The documentation for this class was generated from the following file:
Generated at Mon May 13 14:07:32 2002 for An Enumeration to const char * translation template by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001