add new thirdparty library, magic_enum

this is awesome X)
This commit is contained in:
2022-09-20 22:26:36 +09:00
parent dcbd3594cf
commit 6e820daef8
3 changed files with 21 additions and 7 deletions

View File

@@ -2,26 +2,31 @@
#include <string_view>
#include <magic_enum.hpp>
#include <yas/serialize.hpp>
#include <yas/types/std/string.hpp>
#include "nf7.hh"
namespace nf7 {
template <
typename T,
const char* (*Stringify)(T),
T (*Parse)(std::string_view)>
template <typename T>
struct EnumSerializer {
public:
static auto& save(auto& ar, auto t) {
const std::string v = Stringify(t);
ar(v);
ar(magic_enum::enum_name(t));
return ar;
}
static auto& load(auto& ar, auto& t) {
std::string v;
ar(v);
t = Parse(v);
if (auto ot = magic_enum::enum_cast<T>(v)) {
t = *ot;
} else {
throw nf7::DeserializeException {"unknown enum: "+v};
}
return ar;
}
};