version 0.4.1
makeenum.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10// https://www.scs.stanford.edu/~dm/blog/va-opt.html
11
12#define PARENS ()
13#define ENUM_CASE(name) \
14 case name: \
15 return #name;
16// Limits to 256 items
17#define EXPAND(arg) EXPAND1(EXPAND1(EXPAND1(EXPAND1(arg))))
18#define EXPAND1(arg) EXPAND2(EXPAND2(EXPAND2(EXPAND2(arg))))
19#define EXPAND2(arg) EXPAND3(EXPAND3(EXPAND3(EXPAND3(arg))))
20#define EXPAND3(arg) EXPAND4(EXPAND4(EXPAND4(EXPAND4(arg))))
21#define EXPAND4(arg) arg
22
23#define FOR_EACH(macro, ...) __VA_OPT__(EXPAND(FOR_EACH_HELPER(macro, __VA_ARGS__)))
24#define FOR_EACH_HELPER(macro, a1, ...) macro(a1) __VA_OPT__(FOR_EACH_AGAIN PARENS(macro, __VA_ARGS__))
25#define FOR_EACH_AGAIN() FOR_EACH_HELPER
26
27#define ENUM_CASE(name) \
28 case name: \
29 return #name;
30
40#define MAKE_ENUM(type, ...) \
41 enum class type \
42 { \
43 BEGIN, \
44 __VA_ARGS__, \
45 END \
46 }; \
47 constexpr std::string toString(type _e) { \
48 using enum type; \
49 switch (_e) { \
50 ENUM_CASE(BEGIN) \
51 FOR_EACH(ENUM_CASE, __VA_ARGS__) \
52 ENUM_CASE(END) \
53 } \
54 __builtin_unreachable(); \
55 }
56
57#include <dune/common/exceptions.hh>
58namespace Ikarus {
67template <typename MessageType>
68MessageType& increment(MessageType& e) {
69 if (e == MessageType::END) {
70 DUNE_THROW(Dune::RangeError, "for MessageType& operator ++ (MessageType&)");
71 }
72 e = MessageType(static_cast<typename std::underlying_type<MessageType>::type>(e) + 1);
73 return e;
74}
75} // namespace Ikarus
MessageType & increment(MessageType &e)
Increments the given enum value.
Definition: makeenum.hh:68
Definition: assemblermanipulatorbuildingblocks.hh:22