GG: Enum.h File Reference

GG

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

Classes | Namespaces | Macros | Functions
Enum.h File Reference
`#include <boost/config.hpp>`
`#include <map>`
`#include <string>`

Go to the source code of this file.

## Classes
struct | GG::EnumMapBase
struct | GG::EnumMap< E >

## Namespaces
namespace | GG

## Macros
#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.

## Macro Definition 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 88 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 98 of file Enum.h.

#define GG_ENUM_MAP_END

Value:
} \
virtual const std::string& FromEnum(long 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; \
} \
long 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 102 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 126 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 137 of file Enum.h.