add EnumSerializer

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

View File

@ -116,6 +116,7 @@ target_sources(nf7
common/util_string.hh
common/value.hh
common/yas_audio.hh
common/yas_enum.hh
common/yas_imgui.hh
common/yas_imnodes.hh
common/yas_nf7.hh

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