Main Page   Compound List   File List   Compound Members   File Members  

enumTranslate.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include <assert.h>
00004 #include <string.h>
00005 #include <stdio.h>
00006 
00039 template <class T>
00040 class EnumTranslate
00041 {
00042    public:
00047    const T enumVal( const char *name )
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    }
00060 
00065    const char *enumName( T val )
00066    {
00067       const char *name = table[val].name;
00068       return name;
00069    }
00070 
00071    protected:
00076    typedef struct
00077    {
00079       T enumeration;
00081       const char *name;
00082    } EnumTable;
00083 
00085    static EnumTable table[];
00086 
00087 }; // EnumTranslate
00088 
00089 
00096 class Numbers {
00097    public:
00098    typedef enum {
00099       badEnum,
00100       zero,
00101       one,
00102       two,
00103       three,
00104       four,
00105       lastEnum } numbers;
00106 
00107    public:
00109    virtual const numbers enumVal( const char *name )
00110    {
00111       return t.enumVal( name );
00112    }
00116    virtual const char *enumName( numbers val )
00117    {
00118       assert( val > badEnum && val < lastEnum );
00119 
00120       return t.enumName( val );
00121    }
00122 
00123    private:
00125    EnumTranslate<numbers> t;
00126 }; // Numbers
00127 
00128 
00130 EnumTranslate<Numbers::numbers>::EnumTable EnumTranslate<Numbers::numbers>::table[] = {
00131    {Numbers::badEnum,   "bad enum"},
00132    {Numbers::zero,      "zero"},
00133    {Numbers::one,       "one"},
00134    {Numbers::two,       "two"},
00135    {Numbers::three,     "three"},
00136    {Numbers::four,      "four"},
00137    {(Numbers::numbers)0, 0}
00138 };
00139 
00140 
00146 class Fruit {
00147    public:
00148    typedef enum {
00149       badEnum,
00150       apples,
00151       oranges,
00152       pears,
00153       peaches,
00154       lastEnum } fruit;
00155 
00156    private:
00157    EnumTranslate<fruit> t;
00158 
00159    public:
00161    virtual const fruit enumVal( const char *name )
00162    {
00163       return t.enumVal( name );
00164    }
00168    virtual const char *enumName( fruit val )
00169    {
00170       assert( val > badEnum && val < lastEnum );
00171       return t.enumName( val );
00172    }
00173 };  // Fruit
00174 
00175 
00177 EnumTranslate<Fruit::fruit>::EnumTable EnumTranslate<Fruit::fruit>::table[] = {
00178    {Fruit::badEnum, "bad enum"},
00179    {Fruit::apples, "apples"},
00180    {Fruit::oranges, "oranges"},
00181    {Fruit::pears, "pears"},
00182    {Fruit::peaches, "peaches"},
00183    {(Fruit::fruit)0, 0}
00184 };
00185 
00186 
00190 main()
00191 {
00192    Fruit f;
00193    Numbers n;
00194    const char *str;
00195 
00196    str = f.enumName( Fruit::pears );
00197    printf("pears = %s\n", str );
00198    str = n.enumName( Numbers::three );
00199    printf("three = %s\n", str );
00200 }

Generated at Mon May 13 14:07:31 2002 for An Enumeration to const char * translation template by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001