blob: 2874a630ad950d98f29bf8a5cb15dc4862c92d0a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/**
@file G3D/enumclass.h
@maintainer Morgan McGuire, http://graphics.cs.williams.edu
@created 2007-01-27
@edited 2007-07-20
*/
#ifndef G3D_enumclass_h
#define G3D_enumclass_h
#include "G3D/HashTrait.h"
#include "G3D/BinaryInput.h"
#include "G3D/BinaryOutput.h"
/**
\def G3D_DECLARE_ENUM_CLASS_METHODS
\brief Creates a series of methods that turn a class into a scoped enumeration.
Uses the "Intelligent Enum" design pattern
http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4001/
Enum classes are initialized to their zero value by default.
You must implement the following method before calling G3D_DECLARE_ENUM_CLASS_METHODS, as either:
<pre>
static const char* toString(int i, Value& v) {
static const char* str[] = {"FUEL", "FOOD", "WATER", NULL}; // Whatever your enum values are
static const Value val[] = {FUEL, FOOD, WATER}; // Whatever your enum values are
const char* s = str[i];
if (s) {
v = val[i];
}
return s;
}
</pre>
See GLG3D/GKey.h for an example.
\sa G3D_DECLARE_ENUM_CLASS_HASHCODE
*/
#define G3D_DECLARE_ENUM_CLASS_METHODS(Classname)\
private: \
void fromString(const std::string& x) {\
Value v;\
const char* s;\
int i = 0;\
\
do {\
s = toString(i, v);\
if (x == s) {\
value = v;\
return;\
}\
++i;\
} while (s);\
}\
\
public:\
\
const char* toString() const {\
const char* s;\
int i = 0;\
Value v = (Value)0;\
while (true) {\
s = toString(i, v);\
if ((s == NULL) || (v == value)) {\
return s;\
}\
++i;\
}\
return NULL;\
}\
\
explicit Classname(const std::string& x) : value((Value)0) {\
fromString(x);\
}\
\
Classname(const Any& a) : value((Value)0) {\
fromString(a.string());\
}\
\
operator Any() const {\
return Any(toString());\
}\
\
Classname(char v) : value((Value)v) {}\
\
Classname() : value((Value)0) {}\
\
Classname(const Value v) : value(v) {}\
\
explicit Classname(int v) : value((Value)v) {}\
\
/** Support cast back to the Value type, which is needed to allow implicit assignment inside unions. */\
/*inline operator Value() const {
return value;
}*/\
\
operator int() const {\
return (int)value;\
}\
\
bool operator== (const Classname other) const {\
return value == other.value;\
}\
\
bool operator== (const Classname::Value other) const {\
return value == other;\
}\
\
bool operator!= (const Classname other) const {\
return value != other.value;\
}\
\
bool operator!= (const Classname::Value other) const {\
return value != other;\
}\
\
bool operator< (const Classname other) const {\
return value < other.value;\
}\
\
bool operator> (const Classname other) const {\
return value > other.value;\
}\
\
bool operator>= (const Classname other) const {\
return value >= other.value;\
}\
\
bool operator<= (const Classname other) const {\
return value <= other.value;\
}\
\
bool operator< (const Value other) const {\
return value < other;\
}\
\
bool operator> (const Value other) const {\
return value > other;\
}\
\
bool operator<= (const Value other) const {\
return value <= other;\
}\
\
bool operator>= (const Value other) const {\
return value >= other;\
}\
\
Classname& operator-- () {\
value = (Value)((int)value - 1);\
return *this;\
}\
\
Classname& operator++ () {\
value = (Value)((int)value + 1);\
return *this;\
}\
\
Classname& operator+= (const int x) {\
value = (Value)((int)value + x);\
return *this;\
}\
\
Classname& operator-= (const int x) {\
value = (Value)((int)value - x);\
return *this;\
}\
\
Classname operator+ (const int x) const {\
return Classname((int)value + x);\
}\
\
Classname operator- (const int x) const {\
return Classname((int)value - x);\
}\
\
unsigned int hashCode() const {\
return (unsigned int)value;\
}\
\
void serialize(BinaryOutput& b) const {\
b.writeInt32(value);\
}\
\
void deserialize(BinaryInput& b) {\
value = (Value)b.readInt32();\
}
/** \def G3D_DECLARE_ENUM_CLASS_HASHCODE
*/
#define G3D_DECLARE_ENUM_CLASS_HASHCODE(Classname)\
template <> struct HashTrait<Classname::Value> \
{ \
static size_t hashCode(Classname::Value key) { return static_cast<size_t>(key); } \
}; \
\
template <> struct HashTrait<Classname> \
{ \
static size_t hashCode(Classname key) { return static_cast<size_t>(key.hashCode()); } \
};
#endif
|