85 #include <boost/preprocessor/cat.hpp>
86 #include <boost/preprocessor/seq/for_each.hpp>
87 #include <boost/preprocessor.hpp>
96 :
message(
"Failed to convert string \"" + str +
"\" to type " + enumname)
103 const char *
what()
const throw()
108 const char *
type()
const throw()
128 #define ADD_PAREN_1( A, B ) ((A, B)) ADD_PAREN_2
129 #define ADD_PAREN_2( A, B ) ((A, B)) ADD_PAREN_1
130 #define ADD_PAREN_1_END
131 #define ADD_PAREN_2_END
132 #define MAKEPAIRS( INPUT ) BOOST_PP_CAT(ADD_PAREN_1 INPUT,_END)
133 #define PP_SEQ_FOR_EACH_I_PAIR(macro, data, pairs) BOOST_PP_SEQ_FOR_EACH_I(macro, data, MAKEPAIRS(pairs))
136 #define CAT2( A, B ) A ## B
137 #define CAT3( A, B, C ) A ## B ## C
140 #define EXPAND_ENUMVALUE_NORMAL(r, data, i, record) \
141 BOOST_PP_TUPLE_ELEM(2, 0, record) = i,
144 #define EXPAND_ENUMFUNC_NORMAL(r, data, i, record) \
145 if(data == BOOST_PP_TUPLE_ELEM(2, 1, record)) return BOOST_PP_TUPLE_ELEM(2, 0, record);
146 #define EXPAND_ENUMPARSE_NORMAL(r, data, i, record) \
147 if(data == BOOST_PP_TUPLE_ELEM(2, 1, record)) { *this = BOOST_PP_TUPLE_ELEM(2, 0, record); return true; }
148 #define EXPAND_ENUMFUNCREV_NORMAL(r, data, i, record) \
149 if(data == BOOST_PP_TUPLE_ELEM(2, 0, record)) return BOOST_PP_TUPLE_ELEM(2, 1, record);
151 #define EXPAND_ENUMFUNCTIONCOUNT(r, data, i, record) \
157 #define MAKE_ENUM(NAME, CONTENT) \
158 struct NAME : public enum_tag \
162 PP_SEQ_FOR_EACH_I_PAIR(EXPAND_ENUMVALUE_NORMAL, ,CONTENT) \
165 NAME(type v) : v(v) {} \
169 static NAME string_to_enum (const std::string& str, NAME def) \
171 PP_SEQ_FOR_EACH_I_PAIR(EXPAND_ENUMFUNC_NORMAL, str , CONTENT) \
174 static NAME string_to_enum (const std::string& str) \
176 PP_SEQ_FOR_EACH_I_PAIR(EXPAND_ENUMFUNC_NORMAL, str , CONTENT) \
177 throw bad_enum_cast( #NAME , str); \
182 template<typename TStr> \
183 bool parse (const TStr& str) \
185 PP_SEQ_FOR_EACH_I_PAIR(EXPAND_ENUMPARSE_NORMAL, str , CONTENT) \
189 bool parse (const char* str) \
191 return parse(std::string(str)); \
193 static std::string name() \
197 static std::string enum_to_string (NAME val) \
199 PP_SEQ_FOR_EACH_I_PAIR(EXPAND_ENUMFUNCREV_NORMAL, val.v , CONTENT) \
200 assert(false && "Corrupted enum found with identifier NAME"); \
201 throw "assertion ignored"; \
203 static const char* enum_to_cstring (NAME val) \
205 PP_SEQ_FOR_EACH_I_PAIR(EXPAND_ENUMFUNCREV_NORMAL, val.v , CONTENT) \
206 assert(false && "Corrupted enum found with identifier NAME"); \
207 throw "assertion ignored"; \
209 std::string to_string () const \
211 return enum_to_string(*this); \
213 const char* to_cstring () const \
215 return enum_to_cstring(*this); \
217 friend std::ostream& operator<< (std::ostream & os, NAME val) \
219 os << enum_to_string(val); \
222 friend std::ostream& operator<< (std::ostream & os, NAME::type val) \
224 return (os << NAME(val)); \
226 friend std::istream& operator>> (std::istream & is, NAME& val) \
228 std::istream::sentry s(is, true); \
233 val = string_to_enum(temp); \
234 } catch (const bad_enum_cast & ) {\
235 is.setstate(std::ios::failbit); \
240 friend std::istream& operator>> (std::istream & os, NAME::type& val) \
242 return (os >> reinterpret_cast< NAME &>(val)); \
244 friend bool operator==(NAME v1, NAME v2) \
246 return v1.v == v2.v; \
248 friend bool operator==(NAME::type v1, NAME v2) \
252 friend bool operator==(NAME v1, NAME::type v2) \
256 friend bool operator!=(NAME v1, NAME v2) \
258 return v1.v != v2.v; \
260 friend bool operator!=(NAME::type v1, NAME v2) \
264 friend bool operator!=(NAME v1, NAME::type v2) \
269 friend bool operator<(NAME v1, NAME v2) \
271 return v1.v < v2.v; \
273 template<typename T> \
276 return static_cast<T>(v); \
278 static NAME from_int(int i) \
280 return static_cast<type>(i); \
282 static const size_t count = 0 PP_SEQ_FOR_EACH_I_PAIR(EXPAND_ENUMFUNCTIONCOUNT, , CONTENT);\
285 return cast<size_t>() < count; \
const char * type() const
const char * what() const
const char * value() const
GLuint const GLchar * name
GLsizei GLenum GLuint GLuint GLsizei char * message
void debug_conversion_error(const std::string &temp, const bad_enum_cast &e)
GLsizei const GLcharARB ** string
bad_enum_cast(const std::string &enumname, const std::string &str)