00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00014
00015
00016 #ifndef __defined_libdai_enum_h
00017 #define __defined_libdai_enum_h
00018
00019
00020 #include <cstring>
00021 #include <iostream>
00022 #include <dai/exceptions.h>
00023
00024
00026
00038 #define DAI_ENUM(x,val0,...) class x {\
00039 public:\
00040 enum value {val0,__VA_ARGS__};\
00041 \
00042 x() : v(val0) {}\
00043 \
00044 x(value w) : v(w) {}\
00045 \
00046 x(char const *w) {\
00047 static char const* labelstring = #val0 "," #__VA_ARGS__;\
00048 size_t pos_begin = 0;\
00049 size_t i = 0;\
00050 for( size_t pos_end = 0; ; pos_end++ ) {\
00051 if( (labelstring[pos_end] == ',') || (labelstring[pos_end] == '\0') ) {\
00052 if( (strlen( w ) == pos_end - pos_begin) && (strncmp( labelstring + pos_begin, w, pos_end - pos_begin ) == 0) ) {\
00053 v = (value)i;\
00054 return;\
00055 } else {\
00056 i++;\
00057 pos_begin = pos_end + 1;\
00058 }\
00059 }\
00060 if( labelstring[pos_end] == '\0' )\
00061 break;\
00062 }\
00063 DAI_THROWE(UNKNOWN_ENUM_VALUE,"'" + std::string(w) + "' is not in [" + std::string(labelstring) + "]");\
00064 }\
00065 \
00066 operator value() const { return v; }\
00067 \
00068 operator size_t() const { return (size_t)v; }\
00069 \
00070 operator char const*() const {\
00071 static char labelstring[] = #val0 "," #__VA_ARGS__;\
00072 size_t pos_begin = 0;\
00073 size_t i = 0;\
00074 for( size_t pos_end = 0; ; pos_end++ )\
00075 if( (labelstring[pos_end] == ',') || (labelstring[pos_end] == '\0') ) {\
00076 if( (size_t)v == i ) {\
00077 labelstring[pos_end] = '\0';\
00078 return labelstring + pos_begin;\
00079 } else {\
00080 i++;\
00081 pos_begin = pos_end + 1;\
00082 }\
00083 }\
00084 }\
00085 \
00086 friend std::istream& operator >> (std::istream& is, x& y) {\
00087 std::string s;\
00088 is >> s;\
00089 y = x(s.c_str());\
00090 return is;\
00091 }\
00092 \
00093 friend std::ostream& operator << (std::ostream& os, const x& y) {\
00094 os << (const char *)y;\
00095 return os;\
00096 }\
00097 \
00098 protected:\
00099 value v;\
00100 };
00101
00102
00103 #endif