#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 () |
Definition in file Enum.h.
#define GG_ENUM_MAP_BEGIN | ( | name | ) |
Value:
template <> struct EnumMap< name > : EnumMapBase \ { \ typedef name EnumType; \ typedef std::map<EnumType, std::string> MapType; \ EnumMap () \ {
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
#define GG_ENUM_MAP_INSERT | ( | value | ) | m_map[ value ] = #value ; |
#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; \ };
#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; \ }
Note that the generated function requires that EnumMap<name> be defined.
#define GG_ENUM_STREAM_OUT | ( | name | ) |
Value:
inline std::ostream& operator<<(std::ostream& os, name v) \ { \ os << GG::GetEnumMap< name >().FromEnum(v); \ return os; \ }
Note that the generated function requires that EnumMap<name> be defined.