add EnumSerializer

This commit is contained in:
2022-09-19 10:04:30 +09:00
parent c5590092fa
commit dcbd3594cf
2 changed files with 30 additions and 0 deletions

29
common/yas_enum.hh Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <string_view>
#include <yas/serialize.hpp>
#include <yas/types/std/string.hpp>
namespace nf7 {
template <
typename T,
const char* (*Stringify)(T),
T (*Parse)(std::string_view)>
struct EnumSerializer {
public:
static auto& save(auto& ar, auto t) {
const std::string v = Stringify(t);
ar(v);
return ar;
}
static auto& load(auto& ar, auto& t) {
std::string v;
ar(v);
t = Parse(v);
return ar;
}
};
} // namespace nf7