Support Forum       G3D Web Page     
Namespaces | Macros | Functions
enumclass.h File Reference

G3D Innovation Engine http://casual-effects.com/g3d Copyright 2000-2019, Morgan McGuire All rights reserved Available under the BSD License. More...

Namespaces

 G3D
 
 G3D::_internal
 

Macros

#define G3D_BEGIN_ENUM_CLASS_DECLARATION(ClassName, ...)
 
#define G3D_DECLARE_ENUM_CLASS(ClassName, ...)
 
More...
 
#define G3D_DECLARE_ENUM_CLASS_HASHCODE(Classname)
 Must be used at top level (i.e., not inside a class or namespace), with a fully qualified class name. More...
 
#define G3D_DECLARE_ENUM_CLASS_METHODS(Classname)
 
Creates a series of methods that turn a class into a scoped enumeration. More...
 
#define G3D_END_ENUM_CLASS_DECLARATION()   }
 

Functions

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

Detailed Description

G3D Innovation Engine http://casual-effects.com/g3d Copyright 2000-2019, Morgan McGuire All rights reserved Available under the BSD License.

Macro Definition Documentation

◆ G3D_BEGIN_ENUM_CLASS_DECLARATION

#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)
#define G3D_DECLARE_ENUM_CLASS_METHODS(Classname)
Creates a series of methods that turn a class into a scoped enumeration.
Definition: enumclass.h:63
G3D_BEGIN_ENUM_CLASS_DECLARATION(Day, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY);
// Put extra methods here, e.g.,
Value nextValue() { ... }
}
See also
G3D_DECLARE_ENUM_CLASS, G3D_END_ENUM_CLASS_DECLARATION

◆ G3D_DECLARE_ENUM_CLASS

#define G3D_DECLARE_ENUM_CLASS (   ClassName,
  ... 
)
Value:
G3D_BEGIN_ENUM_CLASS_DECLARATION(ClassName, __VA_ARGS__);\
G3D_END_ENUM_CLASS_DECLARATION();
#define G3D_BEGIN_ENUM_CLASS_DECLARATION(ClassName,...)
Definition: enumclass.h:298


// Arguments may not have initializer expressions. Arguments may contain comments.
// Namespaces aren't *required*, this example just shows how to use them.
namespace Foo {
G3D_DECLARE_ENUM_CLASS(Day, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY);
}
...
using namespace Foo;
// Example use of the smart enum
Day d = Day::TUESDAY;
Day d2("SATURDAY");
Any a(d);
d = a;
printf("%s = %d\n", d.toString(), d.value);

Provides these methods:

const char* toString()
arithmetic operators
static int count()
static Value nthValue(n)
See also
G3D_DECLARE_ENUM_CLASS_METHODS, G3D_DECLARE_ENUM_CLASS_HASHCODE, G3D::enumToJavaScriptDeclaration, G3D_BEGIN_ENUM_CLASS_DECLARATION

◆ G3D_DECLARE_ENUM_CLASS_HASHCODE

#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()); } \
};
Must be specialized for custom types.
Definition: HashTrait.h:105

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

◆ G3D_DECLARE_ENUM_CLASS_METHODS

#define G3D_DECLARE_ENUM_CLASS_METHODS (   Classname)


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

Example of use:

class Resource {
public:
enum Value {FUEL, FOOD, WATER} value;
// i is the position the enum value in Value (not the enum value itself)
static const char* toString(int i, Value& v) {
static const char* str[] = {"FUEL", "FOOD", "WATER", nullptr}; // 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;
}
};

Supports enumerations with initializer values.

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 G3D-app/GKey.h and G3D-base/WrapMode for an example.

See also
G3D_DECLARE_ENUM_CLASS_HASHCODE

◆ G3D_END_ENUM_CLASS_DECLARATION

#define G3D_END_ENUM_CLASS_DECLARATION ( )    }

documentation generated on Wed Nov 24 2021 08:01:51 using doxygen 1.8.15