Enum.h
Go to the documentation of this file.
00001 #ifndef ENUM_1E2YM5S5
00002 #define ENUM_1E2YM5S5
00003 
00004 /*
00005 * @file Tools/Enum.h
00006 * Defines a macro that declares an enum and provides
00007 * a function to access the names of its elements.
00008 *
00009 * @author Thomas Röfer
00010 * @author Samuel Barrett
00011 */
00012 
00013 #include <string>
00014 #include <vector>
00015 #include <string.h>
00016 #include <bwi_tools/common/Params.h>
00017 
00024 class EnumName {
00025 private:
00026   std::vector<std::string> names; 
00034   static std::string trim(const std::string& s);
00035   
00036 public:
00049   EnumName(const std::string& enums, size_t numOfEnums);
00050 
00056   const char* getName(size_t e) {return e >= names.size() ? 0 : names[e].c_str();}
00057 };
00058 
00065 #define ENUM(Enum, ...) \
00066   namespace Enum { \
00067     enum type {__VA_ARGS__, NUM}; \
00068   } \
00069   typedef Enum::type Enum##_t; \
00070   inline static const char* getName(Enum::type e) {static EnumName en(#__VA_ARGS__, (size_t) Enum::NUM); return en.getName((size_t) e);} \
00071   namespace Enum { \
00072     inline type fromName(const char* name, bool ignoreUnknown = false) { \
00073       for (size_t i = 0; i < NUM; i++) { \
00074         if (strcmp(getName((type)i),name) == 0) \
00075           return (type)i; \
00076       } \
00077       if (ignoreUnknown) \
00078         return NUM; \
00079       std::cerr << "Problem converting " << name << " to type " << #Enum << std::endl; \
00080       exit(13); \
00081     } \
00082     inline type fromName(const std::string &name, bool ignoreUnknown = false) {return fromName(name.c_str(),ignoreUnknown);} \
00083   } \
00084   SET_FROM_JSON_ENUM(Enum)
00085 
00086 #endif /* end of include guard: ENUM_1E2YM5S5 */


bwi_tools
Author(s): Piyush Khandelwal
autogenerated on Thu Jun 6 2019 17:57:26