47 #ifndef _MIRA_HUMANREADABLEENUM_H_ 48 #define _MIRA_HUMANREADABLEENUM_H_ 50 #include <boost/preprocessor/tuple/to_seq.hpp> 51 #include <boost/preprocessor/seq/for_each.hpp> 54 #include <boost/bimap.hpp> 55 #include <boost/assign.hpp> 86 #define MIRA_HUMANREADABLE_ENUM_VALUE_EXPAND(VALUE) #VALUE 88 #define MIRA_HUMANREADABLE_ENUM_MAP_ITEM(R, NAME, VALUE) \ 89 (NAME::VALUE, MIRA_HUMANREADABLE_ENUM_VALUE_EXPAND(VALUE)) 91 #define MIRA_HUMANREADABLE_ENUM_DECLARE(NAME, VALUES...) \ 92 namespace human_readable_enum { \ 93 enum class NAME { VALUES }; \ 94 std::string toString(const NAME& v); \ 95 std::ostream& operator<<(std::ostream& os, const NAME& js);\ 97 human_readable_enum::NAME to##NAME(const std::string& v); \ 98 using human_readable_enum::NAME 101 #define MIRA_HUMANREADABLE_ENUM_DEFINE(NAME, VALUES...) \ 102 namespace human_readable_enum { \ 103 const boost::bimap<NAME, std::string> NAME##Map \ 104 = boost::assign::list_of<boost::bimap<NAME, std::string>::relation> \ 105 BOOST_PP_SEQ_FOR_EACH( \ 106 MIRA_HUMANREADABLE_ENUM_MAP_ITEM, \ 108 BOOST_PP_TUPLE_TO_SEQ(MIRA_VARIADIC_SIZE(VALUES), (VALUES))); \ 109 std::string toString(const NAME& v) { \ 110 auto it = NAME##Map.left.find(v); \ 111 if ( it == NAME##Map.left.end() ) \ 112 MIRA_THROW(mira::XInvalidParameter, int(v) << \ 113 " is not a valid value for enumeration "#NAME) \ 116 std::ostream& operator<<(std::ostream& os, const NAME& js) { \ 117 os << #NAME << "::" << toString(js); \ 121 human_readable_enum::NAME to##NAME(const std::string& v) { \ 122 auto it = human_readable_enum::NAME##Map.right.find(v); \ 123 if ( it == human_readable_enum::NAME##Map.right.end() ) \ 124 MIRA_THROW(mira::XInvalidParameter, v << \ 125 " is not a valid value for enumeration "#NAME) \ Tools for handling variadic macros.
Contains toString and fromString functions for converting data types to strings and the other way rou...