TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
enumclass.h File Reference
#include "G3D/platform.h"
#include "G3D/HashTrait.h"
#include "G3D/BinaryInput.h"
#include "G3D/BinaryOutput.h"
#include "G3D/TextOutput.h"
#include "G3D/Any.h"
+ Include dependency graph for enumclass.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

 G3D
 
 G3D::_internal
 

Macros

#define G3D_DECLARE_ENUM_CLASS_METHODS(Classname)
 Creates a series of methods that turn a class into a scoped enumeration. More...
 
#define G3D_DECLARE_ENUM_CLASS_HASHCODE(Classname)
 
#define G3D_DECLARE_ENUM_CLASS(ClassName,...)
 
#define G3D_BEGIN_ENUM_CLASS_DECLARATION(ClassName,...)
 
#define G3D_END_ENUM_CLASS_DECLARATION()   }
 

Functions

const char ** G3D::_internal::smartEnumParseNames (const char *enumValList)
 
template<class EnumClass , class EnumClassValue >
void G3D::enumToJavaScriptDeclaration (TextOutput &t)
 Generates JavaScript source code defining an enum equivalent to EnumClass. More...
 

Detailed Description

Morgan McGuire, http://graphics.cs.williams.edu 2007-01-27 2013-04-09

Macro Definition Documentation

#define G3D_BEGIN_ENUM_CLASS_DECLARATION (   ClassName,
  ... 
)
Value:
class ClassName {\
public:\
enum Value {\
__VA_ARGS__\
} value;\
\
/* 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 */\
static const char* toString(int i, Value& v) {\
static const char** str = G3D::_internal::smartEnumParseNames(#__VA_ARGS__);\
static const Value val[] = {__VA_ARGS__};\
const char* s = str[i];\
if (s) { v = val[i]; }\
return s;\
}\
const char ** smartEnumParseNames(const char *enumValList)
std::string toString() const
#define G3D_DECLARE_ENUM_CLASS_METHODS(Classname)
Creates a series of methods that turn a class into a scoped enumeration.
Definition: enumclass.h:60
string ClassName(const Descriptor *descriptor, bool qualified)
const FieldDescriptor value
Definition: descriptor.h:1522
#define const
Definition: zconf.h:217
1 G3D_BEGIN_ENUM_CLASS_DECLARATION(Day, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY);
2  // Put extra methods here, e.g.,
3  Value nextValue() { ... }
4 G3D_END_ENUM_CLASS_DECLARATION();
5 }
6 G3D_DECLARE_ENUM_CLASS_HASHCODE(Foo::Day);
See also
G3D_DECLARE_ENUM_CLASS, G3D_END_ENUM_CLASS_DECLARATION
#define G3D_DECLARE_ENUM_CLASS (   ClassName,
  ... 
)
Value:
#define G3D_BEGIN_ENUM_CLASS_DECLARATION(ClassName,...)
Definition: enumclass.h:267
#define G3D_END_ENUM_CLASS_DECLARATION()
Definition: enumclass.h:287
string ClassName(const Descriptor *descriptor, bool qualified)
1 // Arguments may not have initializer expressions. Arguments may contain comments.
2 // Namespaces aren't *required*, this example just shows how to use them.
3 namespace Foo {
4  G3D_DECLARE_ENUM_CLASS(Day, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY);
5 }
6 G3D_DECLARE_ENUM_CLASS_HASHCODE(Foo::Day);
7 ...
8 
9 using namespace Foo;
10 // Example use of the smart enum
11 Day d = Day::TUESDAY;
12 Day d2("SATURDAY");
13 Any a(d);
14 d = a;
15 printf("%s = %d\n", d.toString(), d.value);
See also
G3D_DECLARE_ENUM_CLASS_METHODS, G3D_DECLARE_ENUM_CLASS_HASHCODE, G3D::enumToJavaScriptDeclaration, G3D_BEGIN_ENUM_CLASS_DECLARATION
#define G3D_DECLARE_ENUM_CLASS_HASHCODE (   Classname)
Value:
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()); } \
};
size_t hashCode() const
Definition: Vector2unorm16.h:76
Definition: HashTrait.h:105
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:1706

Must be used at top level (i.e., not inside a class or namespace), with a fully qualified class name.

#define G3D_DECLARE_ENUM_CLASS_METHODS (   Classname)

Creates a series of methods that turn a class into a scoped enumeration.

Example of use:

1 class Resource {
2 public:
3  enum Value {FUEL, FOOD, WATER} value;
4 
5  // i is the position the enum value in Value (not the enum value itself)
6  static const char* toString(int i, Value& v) {
7  static const char* str[] = {"FUEL", "FOOD", "WATER", NULL}; // Whatever your enum values are
8  static const Value val[] = {FUEL, FOOD, WATER}; // Whatever your enum values are
9  const char* s = str[i];
10  if (s) {
11  v = val[i];
12  }
13  return s;
14  }
15 
16  G3D_DECLARE_ENUM_CLASS_METHODS(Resource);
17 };
18 G3D_DECLARE_ENUM_CLASS_HASHCODE(Resource);

Extends 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.

See GLG3D/GKey.h and G3D/WrapMode for an example.

See also
G3D_DECLARE_ENUM_CLASS_HASHCODE
#define G3D_END_ENUM_CLASS_DECLARATION ( )    }