1 /* This file is part of libDAI - http://www.libdai.org/
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
10 /// \brief Defines the DAI_ENUM macro, which can be used to define an \c enum with additional functionality.
13 #ifndef __defined_libdai_enum_h
14 #define __defined_libdai_enum_h
19 #include <dai/exceptions.h>
22 /// Extends the C++ \c enum type by supporting input/output streaming and conversion to and from <tt>const char*</tt> and \c size_t
23 /** For more details see the source code.
27 * DAI_ENUM(colors,RED,GREEN,BLUE)
29 * defines a class \a colors encapsulating an
31 * enum {RED, GREEN, BLUE};
33 * which offers additional functionality over the plain \c enum keyword.
35 #define DAI_ENUM(x,val0,...) class x {\
37 enum value {val0,__VA_ARGS__};\
44 static char const* labelstring = #val0 "," #__VA_ARGS__;\
45 size_t pos_begin = 0;\
47 for( size_t pos_end = 0; ; pos_end++ ) {\
48 if( (labelstring[pos_end] == ',') || (labelstring[pos_end] == '\0') ) {\
49 if( (strlen( w ) == pos_end - pos_begin) && (strncmp( labelstring + pos_begin, w, pos_end - pos_begin ) == 0) ) {\
54 pos_begin = pos_end + 1;\
57 if( labelstring[pos_end] == '\0' )\
60 DAI_THROWE(UNKNOWN_ENUM_VALUE,"'" + std::string(w) + "' is not in [" + std::string(labelstring) + "]");\
63 operator value() const { return v; }\
65 operator size_t() const { return (size_t)v; }\
67 operator char const*() const {\
68 static char labelstring[] = #val0 "," #__VA_ARGS__;\
69 size_t pos_begin = 0;\
71 for( size_t pos_end = 0; ; pos_end++ )\
72 if( (labelstring[pos_end] == ',') || (labelstring[pos_end] == '\0') ) {\
73 if( (size_t)v == i ) {\
74 labelstring[pos_end] = '\0';\
75 return labelstring + pos_begin;\
78 pos_begin = pos_end + 1;\
83 operator wchar_t const*() const {\
84 static wchar_t labelstring[] = #val0 L"," #__VA_ARGS__;\
85 size_t pos_begin = 0;\
87 for( size_t pos_end = 0; ; pos_end++ )\
88 if( (labelstring[pos_end] == L',') || (labelstring[pos_end] == L'\0') ) {\
89 if( (size_t)v == i ) {\
90 labelstring[pos_end] = L'\0';\
91 return labelstring + pos_begin;\
94 pos_begin = pos_end + 1;\
99 friend std::istream& operator >> (std::istream& is, x& y) {\
106 friend std::wistream& operator >> (std::wistream& wis, x& y) {\
109 y = x((const char *)s.c_str());\
113 friend std::ostream& operator << (std::ostream& os, const x& y) {\
114 os << (const char *)y;\
118 friend std::wostream& operator << (std::wostream& wos, const x& y) {\
119 wos << (const wchar_t *)y;\