Public Member Functions | Protected Attributes

EnumDef Class Reference

Some utilities surrounding the often needed enum <-> string table. More...

Inheritance diagram for EnumDef:
NamedObject CallBacker

List of all members.

Public Member Functions

 EnumDef (const char *nm, const char *s[], short nrs=0)
bool isValidName (const char *s) const
int convert (const char *s) const
const char * convert (int i) const
int size () const

Protected Attributes

const char ** names_
short nrsign_

Detailed Description

Some utilities surrounding the often needed enum <-> string table.

The C func getIndexInStringArrCI returns the enum (integer) value from a text string. The first arg is string you wish to convert to the enum, the second is the array with enum names. Then, the integer value of the first enum value (also returned when no match is found) and the number of characters to be matched (0=all). Make absolutely sure the char** namearr has a closing ' ... ,0 };'.

Normally, you'll have a class with an enum member. In that case, you'll want to use the EnumDef classes. These are normally almost hidden by a few simple macros: DeclareEnumUtils(enm) will make sure the enum will have a string conversion. DeclareEnumUtilsWithVar(enm,varnm) will also create an instance variable with accessors. DefineEnumNames(clss,enm,deflen,prettynm) defines the names. For namespaces, you can use DeclareNameSpaceEnumUtils only

The 'Declare' macros should be placed in the public section of the class. Example of usage:

in myclass.h:

#include <enums.h>

class MyClass
{
public:
    enum State  { Good, Bad, Ugly };
                DeclareEnumUtils(State)
    enum Type   { Yes, No, Maybe };
                DeclareEnumUtilsWithVar(Type,type)

    // rest of class

};

in myclass.cc:

#include <myclass.h>

DefineEnumNames(MyClass,State,1,"My class state")
        { "Good", "Bad", "Not very handsome", 0 };
DefineEnumNames(MyClass,Type,0,"My class type")
        { "Yes", "No", "Not sure", 0 };

Note the '1' in the first one telling the EnumDef that only one character needs to be matched when converting string -> enum. The '0' in the second indicates that the entire string must match.

This will expand to (added newlines, removed some superfluous stuff:

class MyClass
{
public:

    enum                        State { Good, Bad, Ugly };
    static const EnumDef&       StateDef();
    static const char**         StateNames();
    static bool                 parseEnumState(const char*, State& );
    static const char*          getStateString(State);

protected:

    static const char*          StateNames_[];
    static const EnumDef        StateDefinition_;

public:

    enum                        Type { Yes, No, Maybe };
    Type                        type() const { return type_; }
    void                        setType(Type _e_) { type_ = _e_; }
    static const EnumDef&       TypeDef();
    static const char**         TypeNames();
    static bool                 parseEnumType(const char*, Type& );
    static const char*          getTypeString(State);

protected:

    static const char*          TypeNames_[];
    static const EnumDef        TypeDefinition_;

    Type                        type_;

};

and, in myclass.cc:

const EnumDef& MyClass::StateDef()    { return StateDefinition_; }
bool MyClass::parseEnumState(const char* str, State& res )
{ \
    const int idx = StateDef().convert( txt ); \
    if ( idx<0 ) \
        return false; \
 \
    res = (enm) idx; \
    return true; \
} \

const EnumDef MyClass::StateDefinition_("My class state",MyClass::Statenames,1);

const char* MyClass::Statenames_[] =
        { "Good", "Bad", "Not very handsome", 0 };


const EnumDef& MyClass::TypeDef()   { return TypeDefinition_; }
const EnumDef MyClass::TypeDefinition_( "My class type", MyClass::Typenames, 0 );
bool MyClass::parseEnumType(const char* str, Type& res )
{ \
    const int idx = TypeDef().convert( txt ); \
    if ( idx<0 ) \
        return false; \
 \
    res = (enm) idx; \
    return true; \
} \

const char* MyClass::Typenames_[] =
        { "Yes", "No", "Not sure", 0 };

Constructor & Destructor Documentation

EnumDef::EnumDef ( const char *  nm,
const char *  s[],
short  nrs = 0 
) [inline]

Member Function Documentation

int EnumDef::convert ( const char *  s  )  const [inline]
const char* EnumDef::convert ( int  i  )  const [inline]
bool EnumDef::isValidName ( const char *  s  )  const [inline]
int EnumDef::size (  )  const [inline]

Member Data Documentation

const char** EnumDef::names_ [protected]
short EnumDef::nrsign_ [protected]