enum.h
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5*
6* ArmarX is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* ArmarX is distributed in the hope that it will be useful, but
11* WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17*
18* @package ArmarXCore::core
19* @author Raphael Grimm (raphael dot grimm at kit dot edu)
20* @date 2019
21* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22* GNU General Public License
23*/
24
25#pragma once
26
27#include "Preprocessor.h"
28#include "trace.h"
29
30#define _detail_ARMARX_MAKE_ENUM_CONVERTERS(...) _detail_ARMARX_MAKE_ENUM_CONVERTERS_i(__VA_ARGS__)
31#define _detail_ARMARX_MAKE_ENUM_CONVERTERS_i(r, data, elem) \
32 case data::elem: \
33 return #data "::" #elem;
34
35#define ARMARX_MAKE_ENUM_CONVERTERS(type, ...) \
36 static_assert(std::is_enum_v<type>, \
37 "Parameters to ARMARX_MAKE_ENUM_CONVERTERS must be enum types"); \
38 inline std::string to_string(type v) \
39 { \
40 using base = std::underlying_type_t<type>; \
41 switch (v) \
42 { \
43 ARMARX_VARIADIC_FOR_EACH(_detail_ARMARX_MAKE_ENUM_CONVERTERS, type, __VA_ARGS__) \
44 } \
45 throw std::invalid_argument{"Unknown enum value " + std::to_string(static_cast<base>(v))}; \
46 } \
47 inline std::ostream& operator<<(std::ostream& out, type v) \
48 { \
49 return out << to_string(v); \
50 } \
51 inline type operator++(type& x) \
52 { \
53 return x = (type)(((int)(x) + 1)); \
54 } \
55 inline type operator++(type& x, int) \
56 { \
57 type y = x; \
58 x = (type)(((int)(x) + 1)); \
59 return y; \
60 }
61
62#define ARMARX_MAKE_ENUM_AND_CONVERTERS(name, ...) \
63 enum class name \
64 { \
65 __VA_ARGS__ \
66 }; \
67 ARMARX_MAKE_ENUM_CONVERTERS(name, __VA_ARGS__)