Enum.h File Reference

Contains the utility classes and macros that allow for easy conversion to and from an enum value and its textual representation. More...

#include <map>
#include <string>

Go to the source code of this file.

Namespaces

namespace  GG

Classes

struct  GG::EnumMapBase
 A base type for all templated EnumMap types. More...
struct  GG::EnumMap< E >
 A mapping between the values of an enum and the string representations of the enum's values. More...

Defines

#define GG_ENUM_MAP_BEGIN(name)
#define GG_ENUM_MAP_INSERT(value)   m_map[ value ] = #value ;
#define GG_ENUM_MAP_END
#define GG_ENUM_STREAM_IN(name)
#define GG_ENUM_STREAM_OUT(name)

Functions

template<class E>
EnumMap< E > GG::GetEnumMap ()


Detailed Description

Contains the utility classes and macros that allow for easy conversion to and from an enum value and its textual representation.

Definition in file Enum.h.


Define Documentation

#define GG_ENUM_MAP_BEGIN ( name   ) 

Value:

template <> struct EnumMap< name > : EnumMapBase                        \
{                                                                       \
    typedef name EnumType;                                              \
    typedef std::map<EnumType, std::string> MapType;                    \
    EnumMap ()                                                          \
    {
Declares the beginning of a template specialization of EnumMap, for enumerated type name.

Text-to-enum conversion is one of those places that calls for macro magic. To use these for e.g. "enum Foo {FOO, BAR};", write:

    GG_ENUM_MAP_BEGIN( Foo ) 
        GG_ENUM_MAP_INSERT( FOO )
        GG_ENUM_MAP_INSERT( BAR )
        ...
    GG_ENUM_MAP_END 

Definition at line 81 of file Enum.h.

#define GG_ENUM_MAP_INSERT ( value   )     m_map[ value ] = #value ;

Adds a single value from an enumerated type, and its corresponding string representation, to an EnumMap.

Definition at line 90 of file Enum.h.

#define GG_ENUM_MAP_END

Value:

}                                                                   \
    virtual const std::string& FromEnum(int i) const                    \
    {                                                                   \
        static const std::string ERROR_STR;                             \
        std::map<EnumType, std::string>::const_iterator it =            \
            m_map.find(EnumType(i));                                    \
        return it == m_map.end() ? ERROR_STR : it->second;              \
    }                                                                   \
    int FromString (const std::string &str) const                       \
    {                                                                   \
        for (MapType::const_iterator it = m_map.begin();                \
             it != m_map.end();                                         \
             ++it) {                                                    \
            if (it->second == str)                                      \
                return it->first;                                       \
        }                                                               \
        return BAD_VALUE;                                               \
    }                                                                   \
    MapType m_map;                                                      \
};
Declares the end of a template specialization of EnumMap, for enumerated type name.

Definition at line 93 of file Enum.h.

#define GG_ENUM_STREAM_IN ( name   ) 

Value:

inline std::istream& operator>>(std::istream& is, name& v)          \
    {                                                                   \
        std::string str;                                                \
        is >> str;                                                      \
        v = name (GG::GetEnumMap< name >().FromString(str));            \
        return is;                                                      \
    }
Defines an input stream operator for enumerated type name.

Note that the generated function requires that EnumMap<name> be defined.

Definition at line 116 of file Enum.h.

#define GG_ENUM_STREAM_OUT ( name   ) 

Value:

inline std::ostream& operator<<(std::ostream& os, name v)           \
    {                                                                   \
        os << GG::GetEnumMap< name >().FromEnum(v);                     \
        return os;                                                      \
    }
Defines an output stream operator for enumerated type name.

Note that the generated function requires that EnumMap<name> be defined.

Definition at line 126 of file Enum.h.


Generated on Wed Mar 26 14:35:42 2008 for GG by  doxygen 1.5.2