e6b926aaf52a764f0187b8cdd45dbcb579e64e66
[libdai.git] / include / dai / enum.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #ifndef __defined_libdai_enum_h
24 #define __defined_libdai_enum_h
25
26
27 #include <cstring>
28 #include <iostream>
29 #include <dai/exceptions.h>
30
31
32 /// Extends the C++ enum type by supporting io streaming and conversion to and from const char* (using anonymous variadic macros)
33
34
35 #define DAI_ENUM(x,val0,...) class x {\
36 public:\
37 enum value {val0,__VA_ARGS__};\
38 \
39 x() : v(val0) {}\
40 \
41 x(value w) : v(w) {}\
42 \
43 x(char const *w) {\
44 static char const* labelstring = #val0 "," #__VA_ARGS__ ",";\
45 size_t pos_begin = 0;\
46 size_t i = 0;\
47 for( size_t pos_end = 0; labelstring[pos_end] != '\0'; pos_end++ )\
48 if( (labelstring[pos_end] == ',') ) {\
49 if( (strlen( w ) == pos_end - pos_begin) && (strncmp( labelstring + pos_begin, w, pos_end - pos_begin ) == 0) ) {\
50 v = (value)i;\
51 return;\
52 } else {\
53 i++;\
54 pos_begin = pos_end + 1;\
55 }\
56 }\
57 DAI_THROW(UNKNOWN_ENUM_VALUE);\
58 }\
59 \
60 operator value() const { return v; }\
61 \
62 operator size_t() const { return (size_t)v; }\
63 \
64 operator char const*() const {\
65 static char labelstring[] = #val0 "," #__VA_ARGS__;\
66 size_t pos_begin = 0;\
67 size_t i = 0;\
68 for( size_t pos_end = 0; ; pos_end++ )\
69 if( (labelstring[pos_end] == ',') || (labelstring[pos_end] == '\0') ) {\
70 if( (size_t)v == i ) {\
71 labelstring[pos_end] = '\0';\
72 return labelstring + pos_begin;\
73 } else {\
74 i++;\
75 pos_begin = pos_end + 1;\
76 }\
77 }\
78 }\
79 \
80 friend std::istream& operator >> (std::istream& is, x& y) {\
81 std::string s;\
82 is >> s;\
83 y = x(s.c_str());\
84 return is;\
85 }\
86 \
87 friend std::ostream& operator << (std::ostream& os, const x& y) {\
88 os << (const char *)y;\
89 return os;\
90 }\
91 \
92 private:\
93 value v;\
94 };
95
96
97 #endif