tdns
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
nenum.hh
Go to the documentation of this file.
1 #pragma once
2 #include <cstdint>
3 #include <iostream>
4 #include <stdexcept>
5 #include <algorithm>
6 #include <array>
7 #include <string.h>
8 
9 #define SMARTENUMSTART(x) static constexpr std::pair<x, const char*> enumtypemap##x[]= {
10 #define SENUM(x,a1) { x::a1, #a1},
11 #define SENUM2(x, a1, ...) SENUM(x,a1) SENUM(x, __VA_ARGS__)
12 #define SENUM3(x, a1, ...) SENUM(x,a1) SENUM2(x, __VA_ARGS__)
13 #define SENUM4(x, a1, ...) SENUM(x,a1) SENUM3(x, __VA_ARGS__)
14 #define SENUM5(x, a1, ...) SENUM(x,a1) SENUM4(x, __VA_ARGS__)
15 #define SENUM6(x, a1, ...) SENUM(x,a1) SENUM5(x, __VA_ARGS__)
16 #define SENUM7(x, a1, ...) SENUM(x,a1) SENUM6(x, __VA_ARGS__)
17 #define SENUM8(x, a1, ...) SENUM(x,a1) SENUM7(x, __VA_ARGS__)
18 #define SENUM9(x, a1, ...) SENUM(x,a1) SENUM8(x, __VA_ARGS__)
19 #define SENUM10(x, a1, ...) SENUM(x,a1) SENUM9(x, __VA_ARGS__)
20 #define SENUM11(x, a1, ...) SENUM(x,a1) SENUM10(x, __VA_ARGS__)
21 #define SENUM12(x, a1, ...) SENUM(x,a1) SENUM11(x, __VA_ARGS__)
22 #define SENUM13(x, a1, ...) SENUM(x,a1) SENUM12(x, __VA_ARGS__)
23 
24 #define SMARTENUMEND(x) }; \
25 inline const char* toString(const x& t) \
26 { \
27  for(const auto &a : enumtypemap##x) \
28  if(a.first == t) \
29  return a.second; \
30  return "?"; \
31 } \
32 inline x make##x(const char* from) { \
33 for(const auto& a : enumtypemap##x) \
34  if(!strcmp(a.second, from)) \
35  return a.first; \
36  throw std::runtime_error("Unknown value '" + std::string(from) + "' for enum "#x); \
37  } \
38 inline std::ostream& operator<<(std::ostream &os, const x& s) { \
39  os << toString(s); return os; } \
40 
41 #define COMBOENUM4(x, a1,b1,a2,b2,a3,b3,a4,b4) enum class x : uint16_t { \
42  a1=b1, a2=b2, a3=b3, a4=b4 }; SMARTENUMSTART(x) SENUM4(x, a1, a2, a3,a4) \
43  SMARTENUMEND(x)