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... |
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.
|
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 } |
|
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 } |
|
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} }
Definition at line 177 of file enumTranslate.cpp. |