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) case data::elem: return #data "::" #elem;
32 
33 #define ARMARX_MAKE_ENUM_CONVERTERS(type, ...) \
34  static_assert(std::is_enum_v<type>, "Parameters to ARMARX_MAKE_ENUM_CONVERTERS must be enum types"); \
35  inline std::string to_string(type v) \
36  { \
37  using base = std::underlying_type_t<type>; \
38  switch(v) \
39  { \
40  ARMARX_VARIADIC_FOR_EACH(_detail_ARMARX_MAKE_ENUM_CONVERTERS, type, __VA_ARGS__) \
41  } \
42  throw std::invalid_argument{"Unknown enum value " + std::to_string(static_cast<base>(v))}; \
43  } \
44  inline std::ostream& operator << (std::ostream& out, type v) \
45  { \
46  return out << to_string(v); \
47  } \
48  inline type operator++(type& x) {return x = (type)(((int)(x)+1));} \
49  inline type operator++(type& x, int) {type y = x; x = (type)(((int)(x)+1)); return y;}
50 
51 #define ARMARX_MAKE_ENUM_AND_CONVERTERS(name, ...) \
52  enum class name { __VA_ARGS__ }; \
53  ARMARX_MAKE_ENUM_CONVERTERS(name, __VA_ARGS__)
trace.h
Preprocessor.h