MIRA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Macros
HumanReadableEnum.h File Reference

Macros for easy creation of enumerations with conversion functions between enumeration values and human readable strings. More...

#include <boost/preprocessor/tuple/to_seq.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/bimap.hpp>
#include <boost/assign.hpp>
#include <utils/VariadicMacro.h>
#include <utils/ToString.h>
Include dependency graph for HumanReadableEnum.h:

Go to the source code of this file.

Macros

#define MIRA_HUMANREADABLE_ENUM_VALUE_EXPAND(VALUE)   #VALUE
 
#define MIRA_HUMANREADABLE_ENUM_MAP_ITEM(R, NAME, VALUE)   (NAME::VALUE, MIRA_HUMANREADABLE_ENUM_VALUE_EXPAND(VALUE))
 
#define MIRA_HUMANREADABLE_ENUM_DECLARE(NAME, VALUES...)
 
#define MIRA_HUMANREADABLE_ENUM_DEFINE(NAME, VALUES...)
 

Detailed Description

Macros for easy creation of enumerations with conversion functions between enumeration values and human readable strings.

Author
Tim van der Grinten, Ronny Stricker
Date
2018/03/06

Macro Definition Documentation

#define MIRA_HUMANREADABLE_ENUM_VALUE_EXPAND (   VALUE)    #VALUE
#define MIRA_HUMANREADABLE_ENUM_MAP_ITEM (   R,
  NAME,
  VALUE 
)    (NAME::VALUE, MIRA_HUMANREADABLE_ENUM_VALUE_EXPAND(VALUE))
#define MIRA_HUMANREADABLE_ENUM_DECLARE (   NAME,
  VALUES... 
)
Value:
enum class NAME { VALUES }; \
std::string toString(const NAME& v); \
NAME to##NAME(const std::string& v);
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream &lt;&lt; operator).
Definition: ToString.h:256
#define MIRA_HUMANREADABLE_ENUM_DEFINE (   NAME,
  VALUES... 
)
Value:
const boost::bimap<NAME, std::string> NAME##Map \
= boost::assign::list_of<boost::bimap<NAME, std::string>::relation> \
BOOST_PP_SEQ_FOR_EACH( \
NAME, \
BOOST_PP_TUPLE_TO_SEQ(MIRA_VARIADIC_SIZE(VALUES), (VALUES))); \
std::string toString(const NAME& v) { \
auto it = NAME##Map.left.find(v); \
if ( it == NAME##Map.left.end() ) \
MIRA_THROW(mira::XInvalidParameter, mira::toString(int(v)) + \
" is not a valid value for enumeration "#NAME) \
return it->second; \
} \
NAME to##NAME(const std::string& v) { \
auto it = NAME##Map.right.find(v); \
if ( it == NAME##Map.right.end() ) \
MIRA_THROW(mira::XInvalidParameter, v + \
" is not a valid value for enumeration "#NAME) \
return it->second; \
}
#define MIRA_HUMANREADABLE_ENUM_MAP_ITEM(R, NAME, VALUE)
Definition: HumanReadableEnum.h:88
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:91
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream &lt;&lt; operator).
Definition: ToString.h:256
#define MIRA_VARIADIC_SIZE(...)
Definition: VariadicMacro.h:96