1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef __defined_libdai_enum_h
23 #define __defined_libdai_enum_h
28 #include <dai/exceptions.h>
34 // C++ enums are too limited for my purposes. This defines wrapper classes
35 // that provide much more functionality than a simple enum. The only
36 // disadvantage is that one wrapper class needs to be written for each
37 // number of values an enum can take... a better solution is needed.
40 #define ENUM2(x,a,b) class x {\
49 static char const* labels[] = {#a, #b};\
51 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
52 if( strcmp( w, labels[i] ) == 0 ) {\
56 if( i == sizeof(labels) / sizeof(char const *) )\
57 DAI_THROW(UNKNOWN_ENUM_VALUE);\
60 operator value () const { return v; }\
62 operator size_t () const { return (size_t)v; }\
64 operator char const* () const {\
65 static char const* labels[] = {#a, #b};\
69 friend std::istream& operator >> (std::istream& is, x& y) {\
76 friend std::ostream& operator << (std::ostream& os, const x& y) {\
77 os << (const char *)y;\
86 #define ENUM3(x,a,b,c) class x {\
88 enum value {a, b, c};\
95 static char const* labels[] = {#a, #b, #c};\
97 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
98 if( strcmp( w, labels[i] ) == 0 ) {\
102 if( i == sizeof(labels) / sizeof(char const *) )\
103 DAI_THROW(UNKNOWN_ENUM_VALUE);\
106 operator value () const { return v; }\
108 operator size_t () const { return (size_t)v; }\
110 operator char const* () const {\
111 static char const* labels[] = {#a, #b, #c};\
115 friend std::istream& operator >> (std::istream& is, x& y) {\
122 friend std::ostream& operator << (std::ostream& os, const x& y) {\
123 os << (const char *)y;\
132 #define ENUM4(x,a,b,c,d) class x {\
134 enum value {a, b, c, d};\
138 x(value w) : v(w) {}\
141 static char const* labels[] = {#a, #b, #c, #d};\
143 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
144 if( strcmp( w, labels[i] ) == 0 ) {\
148 if( i == sizeof(labels) / sizeof(char const *) )\
149 DAI_THROW(UNKNOWN_ENUM_VALUE);\
152 operator value () const { return v; }\
154 operator size_t () const { return (size_t)v; }\
156 operator char const* () const {\
157 static char const* labels[] = {#a, #b, #c, #d};\
161 friend std::istream& operator >> (std::istream& is, x& y) {\
168 friend std::ostream& operator << (std::ostream& os, const x& y) {\
169 os << (const char *)y;\
178 #define ENUM5(x,a,b,c,d,e) class x {\
180 enum value {a, b, c, d, e};\
184 x(value w) : v(w) {}\
187 static char const* labels[] = {#a, #b, #c, #d, #e};\
189 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
190 if( strcmp( w, labels[i] ) == 0 ) {\
194 if( i == sizeof(labels) / sizeof(char const *) )\
195 DAI_THROW(UNKNOWN_ENUM_VALUE);\
198 operator value () const { return v; }\
200 operator size_t () const { return (size_t)v; }\
202 operator char const* () const {\
203 static char const* labels[] = {#a, #b, #c, #d, #e};\
207 friend std::istream& operator >> (std::istream& is, x& y) {\
214 friend std::ostream& operator << (std::ostream& os, const x& y) {\
215 os << (const char *)y;\
224 #define ENUM6(x,a,b,c,d,e,f) class x {\
226 enum value {a, b, c, d, e, f};\
230 x(value w) : v(w) {}\
233 static char const* labels[] = {#a, #b, #c, #d, #e, #f};\
235 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
236 if( strcmp( w, labels[i] ) == 0 ) {\
240 if( i == sizeof(labels) / sizeof(char const *) )\
241 DAI_THROW(UNKNOWN_ENUM_VALUE);\
244 operator value () const { return v; }\
246 operator size_t () const { return (size_t)v; }\
248 operator char const* () const {\
249 static char const* labels[] = {#a, #b, #c, #d, #e, #f};\
253 friend std::istream& operator >> (std::istream& is, x& y) {\
260 friend std::ostream& operator << (std::ostream& os, const x& y) {\
261 os << (const char *)y;\
270 } // end of namespace dai