Oops, correct previous partial commit.
[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 /// \file
24 /// \brief Defines the DAI_ENUM macro
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_enum_h
29 #define __defined_libdai_enum_h
30
31
32 #include <cstring>
33 #include <iostream>
34 #include <dai/exceptions.h>
35
36
37 /// Extends the C++ enum type by supporting input/output streaming and conversion to and from const char*
38 /** Example of usage:
39 * \code
40 * DAI_ENUM(colors,RED,GREEN,BLUE)
41 * \endcode
42 * defines a class encapsulating an
43 * \code
44 * enum colors {RED, GREEN, BLUE};
45 * \endcode
46 * It offers additional functionality over the plain "enum" keyword.
47 */
48 #define DAI_ENUM(x,val0,...) class x {\
49 public:\
50 enum value {val0,__VA_ARGS__};\
51 \
52 x() : v(val0) {}\
53 \
54 x(value w) : v(w) {}\
55 \
56 x(char const *w) {\
57 static char const* labelstring = #val0 "," #__VA_ARGS__ ",";\
58 size_t pos_begin = 0;\
59 size_t i = 0;\
60 for( size_t pos_end = 0; labelstring[pos_end] != '\0'; pos_end++ )\
61 if( (labelstring[pos_end] == ',') ) {\
62 if( (strlen( w ) == pos_end - pos_begin) && (strncmp( labelstring + pos_begin, w, pos_end - pos_begin ) == 0) ) {\
63 v = (value)i;\
64 return;\
65 } else {\
66 i++;\
67 pos_begin = pos_end + 1;\
68 }\
69 }\
70 DAI_THROW(UNKNOWN_ENUM_VALUE);\
71 }\
72 \
73 operator value() const { return v; }\
74 \
75 operator size_t() const { return (size_t)v; }\
76 \
77 operator char const*() const {\
78 static char labelstring[] = #val0 "," #__VA_ARGS__;\
79 size_t pos_begin = 0;\
80 size_t i = 0;\
81 for( size_t pos_end = 0; ; pos_end++ )\
82 if( (labelstring[pos_end] == ',') || (labelstring[pos_end] == '\0') ) {\
83 if( (size_t)v == i ) {\
84 labelstring[pos_end] = '\0';\
85 return labelstring + pos_begin;\
86 } else {\
87 i++;\
88 pos_begin = pos_end + 1;\
89 }\
90 }\
91 }\
92 \
93 friend std::istream& operator >> (std::istream& is, x& y) {\
94 std::string s;\
95 is >> s;\
96 y = x(s.c_str());\
97 return is;\
98 }\
99 \
100 friend std::ostream& operator << (std::ostream& os, const x& y) {\
101 os << (const char *)y;\
102 return os;\
103 }\
104 \
105 private:\
106 value v;\
107 };
108
109
110 #endif