View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

enum.h
Go to the documentation of this file.
1#ifndef ENUM_H
2#define ENUM_H
3
4// cf. http://stackoverflow.com/questions/5093460/how-to-convert-an-enum-type-variable-to-a-string
5// define an enum like this:
6// DEFINE_ENUM_WITH_STRING_CONVERSIONS(OS_type, (Linux)(Apple)(Windows))
7
8#include <vector>
9#include <string>
10#include <boost/preprocessor.hpp>
11
12#ifdef ENUMS_FOR_PYTHON
13#include "pybind.h"
14#endif
15
16#define X_DEFINE_ENUM_WITH_STRING_CONVERSIONS_TOSTRING_CASE(r, data, elem) \
17 case elem: \
18 return BOOST_PP_STRINGIZE(elem);
19
20#define X_DEFINE_ENUM_FOR_PYTHON_VALUE(r, data, elem) .value(BOOST_PP_STRINGIZE(elem), elem)
21
22#define X_DEFINE_ENUM_ADD_VALUE(r, data, elem) values.emplace_back(BOOST_PP_STRINGIZE(elem));
23
24#ifdef ENUMS_FOR_PYTHON
25#define DEFINE_ENUM_WITH_STRING_CONVERSIONS(name, enumerators) \
26 enum name { BOOST_PP_SEQ_ENUM(enumerators) }; \
27\
28 static inline const char *toString(name v) \
29 { \
30 switch (v) { \
31 BOOST_PP_SEQ_FOR_EACH(X_DEFINE_ENUM_WITH_STRING_CONVERSIONS_TOSTRING_CASE, name, enumerators) \
32 default: \
33 return "[Unknown " BOOST_PP_STRINGIZE(name) "]"; \
34 } \
35 } \
36\
37 static inline std::vector<std::string> valueList(name) \
38 { \
39 std::vector<std::string> values; \
40 BOOST_PP_SEQ_FOR_EACH(X_DEFINE_ENUM_ADD_VALUE, name, enumerators) \
41 return values; \
42 } \
43\
44 template<class Scope> \
45 static inline void enumForPython_##name(const Scope &scope, const char *pythonName) \
46 { \
47 namespace py = pybind11; \
48 py::enum_<name>(scope, pythonName) BOOST_PP_SEQ_FOR_EACH(X_DEFINE_ENUM_FOR_PYTHON_VALUE, name, enumerators) \
49 .export_values(); \
50 }
51#else
52#define DEFINE_ENUM_WITH_STRING_CONVERSIONS(name, enumerators) \
53 enum name { BOOST_PP_SEQ_ENUM(enumerators) }; \
54\
55 static inline const char *toString(name v) \
56 { \
57 switch (v) { \
58 BOOST_PP_SEQ_FOR_EACH(X_DEFINE_ENUM_WITH_STRING_CONVERSIONS_TOSTRING_CASE, name, enumerators) \
59 default: \
60 return "[Unknown " BOOST_PP_STRINGIZE(name) "]"; \
61 } \
62 } \
63\
64 static inline std::vector<std::string> valueList(name) \
65 { \
66 std::vector<std::string> values; \
67 BOOST_PP_SEQ_FOR_EACH(X_DEFINE_ENUM_ADD_VALUE, name, enumerators) \
68 return values; \
69 }
70#endif
71
72#define V_ENUM_OUTPUT_OP(name, scope) \
73 inline std::ostream &operator<<(std::ostream &s, scope::name v) \
74 { \
75 s << scope::toString(v) << " (" << (int)v << ")"; \
76 return s; \
77 }
78
79#define V_ENUM_SET_CHOICES_SCOPE(param, name, scope) setParameterChoices(param, scope::valueList((scope::name)0))
80
81#define V_ENUM_SET_CHOICES(param, name) setParameterChoices(param, valueList((name)0))
82#endif