Go to the source code of this file.
Classes | |
class | EnumName |
Defines | |
#define | ENUM(Enum,...) |
namespace Enum { \ enum type {__VA_ARGS__, NUM}; \ } \ typedef Enum::type Enum##_t; \ inline static const char* getName(Enum::type e) {static EnumName en(#__VA_ARGS__, (size_t) Enum::NUM); return en.getName((size_t) e);} \ namespace Enum { \ inline type fromName(const char* name, bool ignoreUnknown = false) { \ for (size_t i = 0; i < NUM; i++) { \ if (strcmp(getName((type)i),name) == 0) \ return (type)i; \ } \ if (ignoreUnknown) \ return NUM; \ std::cerr << "Problem converting " << name << " to type " << #Enum << std::endl; \ exit(13); \ } \ inline type fromName(const std::string &name, bool ignoreUnknown = false) {return fromName(name.c_str(),ignoreUnknown);} \ } \ SET_FROM_JSON_ENUM(Enum)
Defining an enum and a function getName(<Enum>) that can return the name of each enum element. The enum will automatically contain an element NUM that reflects the number of elements defined.