ECSTASY
All in the name
Loading...
Searching...
No Matches
SerializableEnum.hpp
Go to the documentation of this file.
1
11
12#ifndef UTIL_SERIALIZATION_SERIALIZABLE_ENUM_HPP_
13#define UTIL_SERIALIZATION_SERIALIZABLE_ENUM_HPP_
14
15#include "foreach.hpp"
16
17#define _ENUM_TO_STRING_ENTRY(name) {Enum::name, #name},
18#define _STRING_TO_ENUM_ENTRY(name) {#name, Enum::name},
19#define _GET_FIRST_ARG(arg1, ...) arg1
20#define _REMOVE_FIRST_ARG(arg1, ...) __VA_ARGS__
21
28#define SERIALIZABLE_ENUM(NAME, STARTING_AT, ...) \
29 struct __##NAME { \
30 public: \
31 enum class Enum { _GET_FIRST_ARG(__VA_ARGS__) = STARTING_AT, _REMOVE_FIRST_ARG(__VA_ARGS__) }; \
32 \
33 friend inline std::ostream &operator<<(std::ostream &stream, const Enum &e) \
34 { \
35 static const std::unordered_map<Enum, std::string> map = {FOR_EACH(_ENUM_TO_STRING_ENTRY, __VA_ARGS__)}; \
36 return stream << map.at(e); \
37 } \
38 friend inline std::istream &operator>>(std::istream &stream, Enum &e) \
39 { \
40 static const std::unordered_map<std::string, Enum> map = {FOR_EACH(_STRING_TO_ENUM_ENTRY, __VA_ARGS__)}; \
41 std::string buffer; \
42 \
43 stream >> buffer; \
44 e = map.at(buffer); \
45 return stream; \
46 } \
47 }; \
48 using NAME = __##NAME::Enum;
49
50#endif /* !UTIL_SERIALIZATION_SERIALIZABLE_ENUM_HPP_ */