GG: Enum.h Source File

GG

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

- File List
- File Members

Enum.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /* GG is a GUI for SDL and OpenGL.
3 Copyright (C) 2003-2008 T. Zachary Laine
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 2.1
8 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA
19
20 If you do not wish to comply with the terms of the LGPL please
21 contact the author as other terms are available for a fee.
22
23 Zach Laine
24 whatwasthataddress@gmail.com */
25
30 #ifndef _GG_Enum_h_
31 #define _GG_Enum_h_
32
33 #include <boost/config.hpp>
34
35 #include <map>
36 #include <string>
37
38
39 namespace GG {
40
42 struct EnumMapBase
43 {
44 BOOST_STATIC_CONSTANT(long int, BAD_VALUE = -5000000);
45
46 virtual ~EnumMapBase() {}
47
50 virtual const std::string& FromEnum(long int i) const = 0;
51
54 virtual long int FromString (const std::string& str) const = 0;
55 };
56
62 template <class E> struct EnumMap : EnumMapBase
63 {
64 virtual ~EnumMap() {}
65 virtual const std::string& FromEnum(long int) const
66 { static std::string empty; return empty; }
67 virtual long int FromString (const std::string&) const {return 0;}
68 };
69
72 template <class E> EnumMap<E> GetEnumMap()
73 {
74 static EnumMap<E> enum_map;
75 return enum_map;
76 }
77
88 #define GG_ENUM_MAP_BEGIN( name ) \
89 template <> struct EnumMap< name > : EnumMapBase \
90 { \
91 typedef name EnumType; \
92 typedef std::map<EnumType, std::string> MapType; \
93 EnumMap () \
94 {
95
98 #define GG_ENUM_MAP_INSERT( value ) m_map[ value ] = #value ;
99
102 #define GG_ENUM_MAP_END \
103 } \
104 virtual const std::string& FromEnum(long int i) const \
105 { \
106 static const std::string ERROR_STR; \
107 std::map<EnumType, std::string>::const_iterator it = \
108 m_map.find(EnumType(i)); \
109 return it == m_map.end() ? ERROR_STR : it->second; \
110 } \
111 long int FromString (const std::string &str) const \
112 { \
113 for (MapType::const_iterator it = m_map.begin(); \
114 it != m_map.end(); \
115 ++it) { \
116 if (it->second == str) \
117 return it->first; \
118 } \
119 return BAD_VALUE; \
120 } \
121 MapType m_map; \
122 };
123
126 #define GG_ENUM_STREAM_IN( name ) \
127 inline std::istream& operator>>(std::istream& is, name& v) \
128 { \
129 std::string str; \
130 is >> str; \
131 v = name (GG::GetEnumMap< name >().FromString(str)); \
132 return is; \
133 }
134
137 #define GG_ENUM_STREAM_OUT( name ) \
138 inline std::ostream& operator<<(std::ostream& os, name v) \
139 { \
140 os << GG::GetEnumMap< name >().FromEnum(v); \
141 return os; \
142 }
143
144 } // namespace GG
145
146 #endif