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
33 // C++ enums are too limited for my purposes. This defines wrapper classes
34 // that provide much more functionality than a simple enum. The only
35 // disadvantage is that one wrapper class needs to be written for each
36 // number of values an enum can take... a better solution is needed.
39 #define ENUM2(x,a,b) class x {\
48 static char const* labels[] = {#a, #b};\
50 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
51 if( strcmp( w, labels[i] ) == 0 ) {\
55 if( i == sizeof(labels) / sizeof(char const *) )\
56 throw "Unknown " #x " value";\
59 operator value () const { return v; }\
61 operator size_t () const { return (size_t)v; }\
63 operator char const* () const {\
64 static char const* labels[] = {#a, #b};\
68 friend std::istream& operator >> (std::istream& is, x& y) {\
75 friend std::ostream& operator << (std::ostream& os, const x& y) {\
76 os << (const char *)y;\
85 #define ENUM3(x,a,b,c) class x {\
87 enum value {a, b, c};\
94 static char const* labels[] = {#a, #b, #c};\
96 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
97 if( strcmp( w, labels[i] ) == 0 ) {\
101 if( i == sizeof(labels) / sizeof(char const *) )\
102 throw "Unknown " #x " value";\
105 operator value () const { return v; }\
107 operator size_t () const { return (size_t)v; }\
109 operator char const* () const {\
110 static char const* labels[] = {#a, #b, #c};\
114 friend std::istream& operator >> (std::istream& is, x& y) {\
121 friend std::ostream& operator << (std::ostream& os, const x& y) {\
122 os << (const char *)y;\
131 #define ENUM4(x,a,b,c,d) class x {\
133 enum value {a, b, c, d};\
137 x(value w) : v(w) {}\
140 static char const* labels[] = {#a, #b, #c, #d};\
142 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
143 if( strcmp( w, labels[i] ) == 0 ) {\
147 if( i == sizeof(labels) / sizeof(char const *) )\
148 throw "Unknown " #x " value";\
151 operator value () const { return v; }\
153 operator size_t () const { return (size_t)v; }\
155 operator char const* () const {\
156 static char const* labels[] = {#a, #b, #c, #d};\
160 friend std::istream& operator >> (std::istream& is, x& y) {\
167 friend std::ostream& operator << (std::ostream& os, const x& y) {\
168 os << (const char *)y;\
177 #define ENUM5(x,a,b,c,d,e) class x {\
179 enum value {a, b, c, d, e};\
183 x(value w) : v(w) {}\
186 static char const* labels[] = {#a, #b, #c, #d, #e};\
188 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
189 if( strcmp( w, labels[i] ) == 0 ) {\
193 if( i == sizeof(labels) / sizeof(char const *) )\
194 throw "Unknown " #x " value";\
197 operator value () const { return v; }\
199 operator size_t () const { return (size_t)v; }\
201 operator char const* () const {\
202 static char const* labels[] = {#a, #b, #c, #d, #e};\
206 friend std::istream& operator >> (std::istream& is, x& y) {\
213 friend std::ostream& operator << (std::ostream& os, const x& y) {\
214 os << (const char *)y;\
223 #define ENUM6(x,a,b,c,d,e,f) class x {\
225 enum value {a, b, c, d, e, f};\
229 x(value w) : v(w) {}\
232 static char const* labels[] = {#a, #b, #c, #d, #e, #f};\
234 for( ; i < sizeof(labels) / sizeof(char const *); i++ )\
235 if( strcmp( w, labels[i] ) == 0 ) {\
239 if( i == sizeof(labels) / sizeof(char const *) )\
240 throw "Unknown " #x " value";\
243 operator value () const { return v; }\
245 operator size_t () const { return (size_t)v; }\
247 operator char const* () const {\
248 static char const* labels[] = {#a, #b, #c, #d, #e, #f};\
252 friend std::istream& operator >> (std::istream& is, x& y) {\
259 friend std::ostream& operator << (std::ostream& os, const x& y) {\
260 os << (const char *)y;\
269 } // end of namespace dai