-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum_io.hpp
35 lines (32 loc) · 1.82 KB
/
enum_io.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once
// std
#include <iostream>
#include <string_view>
#include <tuple>
/// must be called with:
/// - first argument is enum type
/// - subsequent arguments are a tuple literal of enum values to literal string
///
/// will allocate static constexpr storage of a c-style array of tuples; this
/// is searched linearly for enums when callint to_string
#define DECLARE_ENUM_IO( E, ... ) \
static constexpr std::tuple<E, const char*> E##__enum_io_storage[] = { \
__VA_ARGS__ \
}; \
\
[[maybe_unused]] static constexpr std::string_view to_string(E e) \
{ \
for (size_t i=0; i<std::size(E##__enum_io_storage); ++i) \
{ \
const auto& [enum_value, enum_string] = E##__enum_io_storage[i]; \
if (enum_value == e) \
return enum_string; \
} \
\
return to_string(E::UNKNOWN); \
} \
\
[[maybe_unused]] static std::ostream& operator<<(std::ostream& os, E e) \
{ \
return os << to_string(e); \
}