MIRA
HumanReadableEnum.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_HUMANREADABLEENUM_H_
48 #define _MIRA_HUMANREADABLEENUM_H_
49 
50 #include <boost/preprocessor/tuple/to_seq.hpp>
51 #include <boost/preprocessor/seq/for_each.hpp>
52 
53 #ifndef Q_MOC_RUN
54 #include <boost/bimap.hpp>
55 #include <boost/assign.hpp>
56 #endif
57 
58 #include <utils/VariadicMacro.h>
59 #include <utils/ToString.h>
60 
62 
63 /*
64  * Usage:
65  *
66  * desired enumeration:
67  * enum class Gender {
68  * UNKNOWN,
69  * FEMALE,
70  * MALE
71  * }
72  *
73  * use in header file:
74  * MIRA_HUMANREADABLE_ENUM_DECLARE(Gender, UNKNOWN, FEMALE, MALE);
75  *
76  * use in code file:
77  * MIRA_HUMANREADABLE_ENUM_DEFINE(Gender, UNKNOWN, FEMALE, MALE);
78  *
79  * now you can use following functions:
80  * std::string toString(const Gender& v); -> converts Gender::UNKNOWN to "UNKNOWN"
81  * Gender toGender(const std::string& v); -> converts "FEMALE" to Gender::FEMALE
82  */
83 
85 
86 #define MIRA_HUMANREADABLE_ENUM_VALUE_EXPAND(VALUE) #VALUE
87 
88 #define MIRA_HUMANREADABLE_ENUM_MAP_ITEM(R, NAME, VALUE) \
89  (NAME::VALUE, MIRA_HUMANREADABLE_ENUM_VALUE_EXPAND(VALUE))
90 
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);\
96 } /* namespace human_readable_enum */ \
97 human_readable_enum::NAME to##NAME(const std::string& v); \
98 using human_readable_enum::NAME
99 
100 
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, \
107  NAME, \
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) \
114  return it->second; \
115 } \
116 std::ostream& operator<<(std::ostream& os, const NAME& js) { \
117  os << #NAME << "::" << toString(js); \
118  return os; \
119 } \
120 } /* namespace human_readable_enum */ \
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) \
126  return it->second; \
127 } \
128 
129 
131 #endif
Tools for handling variadic macros.
Contains toString and fromString functions for converting data types to strings and the other way rou...