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 enum class NAME { VALUES }; \
93 std::string toString(const NAME& v); \
94 NAME to##NAME(const std::string& v);
95 
96 #define MIRA_HUMANREADABLE_ENUM_DEFINE(NAME, VALUES...) \
97 const boost::bimap<NAME, std::string> NAME##Map \
98  = boost::assign::list_of<boost::bimap<NAME, std::string>::relation> \
99 BOOST_PP_SEQ_FOR_EACH( \
100  MIRA_HUMANREADABLE_ENUM_MAP_ITEM, \
101  NAME, \
102  BOOST_PP_TUPLE_TO_SEQ(MIRA_VARIADIC_SIZE(VALUES), (VALUES))); \
103 std::string toString(const NAME& v) { \
104  auto it = NAME##Map.left.find(v); \
105  if ( it == NAME##Map.left.end() ) \
106  MIRA_THROW(mira::XInvalidParameter, mira::toString(int(v)) + \
107  " is not a valid value for enumeration "#NAME) \
108  return it->second; \
109 } \
110 NAME to##NAME(const std::string& v) { \
111  auto it = NAME##Map.right.find(v); \
112  if ( it == NAME##Map.right.end() ) \
113  MIRA_THROW(mira::XInvalidParameter, v + \
114  " is not a valid value for enumeration "#NAME) \
115  return it->second; \
116 }
117 
119 
120 #endif
Tools for handling variadic macros.
Contains toString and fromString functions for converting data types to strings and the other way rou...