TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
enumclass.h
Go to the documentation of this file.
1 
8 #ifndef G3D_enumclass_h
9 #define G3D_enumclass_h
10 
11 #include "G3D/platform.h"
12 #include "G3D/HashTrait.h"
13 #include "G3D/BinaryInput.h"
14 #include "G3D/BinaryOutput.h"
15 #include "G3D/TextOutput.h"
16 #include "G3D/Any.h"
17 
18 namespace G3D {
19  namespace _internal {
20  const char** smartEnumParseNames(const char* enumValList);
21  }
22 }
23 
60 #define G3D_DECLARE_ENUM_CLASS_METHODS(Classname)\
61 private: \
62  void fromString(const std::string& x) {\
63  Value v = (Value)0;\
64  const char* s;\
65  int i = 0;\
66 \
67  do {\
68  s = toString(i, v);\
69  if (s == NULL) { return; } \
70  if (x == s) {\
71  value = v;\
72  return;\
73  }\
74  ++i;\
75  } while (true);\
76  }\
77 \
78 public:\
79  static const char* classname() {\
80  return #Classname;\
81  }\
82 \
83  const char* toString() const {\
84  const char* s;\
85  int i = 0;\
86  Value v = (Value)0;\
87  while (true) {\
88  s = toString(i, v);\
89  if ((s == NULL) || (v == value)) {\
90  return s;\
91  }\
92  ++i;\
93  }\
94  }\
95 \
96  explicit Classname(const std::string& x) : value((Value)0) {\
97  fromString(x);\
98  }\
99 \
100  explicit Classname(const G3D::Any& a) : value((Value)0) { \
101  fromString(a.string());\
102  }\
103 \
104  G3D::Any toAny() const { \
105  return G3D::Any(toString()); \
106  }\
107 \
108  explicit Classname(char v) : value((Value)v) {}\
109 \
110  Classname() : value((Value)0) {}\
111 \
112  Classname(const Value v) : value(v) {}\
113 \
114  explicit Classname(int v) : value((Value)v) {}\
115 \
116  operator int() const {\
117  return (int)value;\
118  }\
119 \
120  Classname& operator=(const Any& a) {\
121  value = Classname(a).value;\
122  return *this;\
123  }\
124 \
125  bool operator== (const Classname other) const {\
126  return value == other.value;\
127  }\
128 \
129  bool operator== (const Classname::Value other) const {\
130  return value == other;\
131  }\
132 \
133  bool operator!= (const Classname other) const {\
134  return value != other.value;\
135  }\
136 \
137  bool operator!= (const Classname::Value other) const {\
138  return value != other;\
139  }\
140 \
141  bool operator< (const Classname other) const {\
142  return value < other.value;\
143  }\
144 \
145  bool operator> (const Classname other) const {\
146  return value > other.value;\
147  }\
148 \
149  bool operator>= (const Classname other) const {\
150  return value >= other.value;\
151  }\
152 \
153  bool operator<= (const Classname other) const {\
154  return value <= other.value;\
155  }\
156 \
157  bool operator< (const Value other) const {\
158  return value < other;\
159  }\
160 \
161  bool operator> (const Value other) const {\
162  return value > other;\
163  }\
164 \
165  bool operator<= (const Value other) const {\
166  return value <= other;\
167  }\
168 \
169  bool operator>= (const Value other) const {\
170  return value >= other;\
171  }\
172 \
173  Classname& operator-- () {\
174  value = (Value)((int)value - 1);\
175  return *this;\
176  }\
177 \
178  Classname& operator++ () {\
179  value = (Value)((int)value + 1);\
180  return *this;\
181  }\
182 \
183  Classname& operator+= (const int x) {\
184  value = (Value)((int)value + x);\
185  return *this;\
186  }\
187 \
188  Classname& operator-= (const int x) {\
189  value = (Value)((int)value - x);\
190  return *this;\
191  }\
192 \
193  Classname operator+ (const int x) const {\
194  return Classname((int)value + x);\
195  }\
196 \
197  Classname operator- (const int x) const {\
198  return Classname((int)value - x);\
199  }\
200 \
201  unsigned int hashCode() const {\
202  return (unsigned int)value;\
203  }\
204 \
205  void serialize(G3D::BinaryOutput& b) const { \
206  b.writeInt32(value);\
207  }\
208 \
209  void deserialize(G3D::BinaryInput& b) { \
210  value = (Value)b.readInt32();\
211  }
212 
216 #define G3D_DECLARE_ENUM_CLASS_HASHCODE(Classname)\
217 template <> struct HashTrait<Classname::Value> \
218 { \
219  static size_t hashCode(Classname::Value key) { return static_cast<size_t>(key); } \
220 }; \
221  \
222 template <> struct HashTrait<Classname> \
223 { \
224  static size_t hashCode(Classname key) { return static_cast<size_t>(key.hashCode()); } \
225 };
226 
250 #define G3D_DECLARE_ENUM_CLASS(ClassName, ...)\
251  G3D_BEGIN_ENUM_CLASS_DECLARATION(ClassName, __VA_ARGS__);\
252  G3D_END_ENUM_CLASS_DECLARATION();
253 
267 #define G3D_BEGIN_ENUM_CLASS_DECLARATION(ClassName, ...)\
268 class ClassName {\
269 public:\
270  enum Value {\
271  __VA_ARGS__\
272  } value;\
273 \
274  /* The static variables here may be duplicated in different shared object binaries (DLLs), but that's ok--we only depend on their values, not their uniqueness. See also http://stackoverflow.com/questions/11962918/local-static-variable-is-instantiated-multiple-times-why */\
275  static const char* toString(int i, Value& v) {\
276  static const char** str = G3D::_internal::smartEnumParseNames(#__VA_ARGS__);\
277  static const Value val[] = {__VA_ARGS__};\
278  const char* s = str[i];\
279  if (s) { v = val[i]; }\
280  return s;\
281  }\
282 \
283  G3D_DECLARE_ENUM_CLASS_METHODS(ClassName);
284 
287 #define G3D_END_ENUM_CLASS_DECLARATION() }
288 
289 
290 namespace G3D {
291 
302 template<class EnumClass, class EnumClassValue>
304  t.printf("/* BEGIN GENERATED CODE. The following was automatically generated by G3D::enumToJavaScriptDeclaration(). Do not edit it manually. */\n\n");
305  t.printf("var %s = (function (propList) {", EnumClass::classname()); t.writeNewline();
306  t.pushIndent(); {
307  t.printf("// Define immutable properties"); t.writeNewline();
308  t.printf("var en = {};"); t.writeNewline();
309  t.printf("for (var i = 0; i < propList.length; i += 2)"); t.writeNewline();
310  t.pushIndent(); {
311  t.printf("Object.defineProperty(en, propList[i], {enumerable: true, value: propList[i + 1]});"); t.writeNewline();
312  } t.popIndent();
313  t.printf("return en;");
314  t.writeNewline();
315  } t.popIndent();
316  t.printf("})([");
317  int i = 0;
318  EnumClassValue m;
319  const char* s = EnumClass::toString(i, m);
320  while (notNull(s)) {
321  t.printf("\"%s\", %d, ", s, int(m));
322  ++i;
323  s = EnumClass::toString(i, m);
324  }
325  // JavaScript allows a trailing comma
326  t.printf("]);"); t.writeNewline();
327  t.printf("/* END GENERATED CODE */"); t.writeNewline();
328 }
329 
330 }
331 #endif
const char ** smartEnumParseNames(const char *enumValList)
bool notNull(const Pointer< T > &p)
Definition: Pointer.h:350
void popIndent()
Definition: TextOutput.cpp:72
void pushIndent()
Definition: TextOutput.cpp:67
Definition: AABox.h:25
std::string toString() const
Definition: TextOutput.h:60
void writeNewline()
Definition: TextOutput.cpp:218
void __cdecl printf(const char *fmt,...) G3D_CHECK_PRINTF_METHOD_ARGS
Definition: TextOutput.cpp:174
void enumToJavaScriptDeclaration(TextOutput &t)
Generates JavaScript source code defining an enum equivalent to EnumClass.
Definition: enumclass.h:303