further serializer refactoring

This commit is contained in:
2024-02-15 19:06:56 +01:00
parent f6e55851cc
commit 73d1d65142
8 changed files with 78 additions and 101 deletions

View File

@@ -6,32 +6,24 @@
#include <nlohmann/json_fwd.hpp>
#include "./fragment_store_i.hpp"
template<typename EntityType = entt::entity>
struct SerializerCallbacks {
using Registry = entt::basic_registry<EntityType>;
using Handle = entt::basic_handle<Registry>;
// nlohmann
// json/msgpack
using serialize_json_fn = bool(*)(void* comp, nlohmann::json& out);
using serialize_json_fn = bool(*)(const Handle h, nlohmann::json& out);
entt::dense_map<entt::id_type, serialize_json_fn> _serl_json;
using deserialize_json_fn = bool(*)(FragmentHandle fh, const nlohmann::json& in);
using deserialize_json_fn = bool(*)(Handle h, const nlohmann::json& in);
entt::dense_map<entt::id_type, deserialize_json_fn> _deserl_json;
template<typename T>
static bool component_emplace_or_replace_json(FragmentHandle fh, const nlohmann::json& j) {
if constexpr (std::is_empty_v<T>) {
fh.emplace_or_replace<T>(); // assert empty json?
} else {
fh.emplace_or_replace<T>(static_cast<T>(j));
}
return true;
}
template<typename T>
static bool component_get_json(const FragmentHandle fh, nlohmann::json& j) {
if (fh.all_of<T>()) {
static bool component_get_json(const Handle h, nlohmann::json& j) {
if (h.template all_of<T>()) {
if constexpr (!std::is_empty_v<T>) {
j = fh.get<T>();
j = h.template get<T>();
}
return true;
}
@@ -39,12 +31,31 @@ struct SerializerCallbacks {
return false;
}
void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info);
template<typename T>
static bool component_emplace_or_replace_json(Handle h, const nlohmann::json& j) {
if constexpr (std::is_empty_v<T>) {
h.template emplace_or_replace<T>(); // assert empty json?
} else {
h.template emplace_or_replace<T>(static_cast<T>(j));
}
return true;
}
void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info) {
_serl_json[type_info.hash()] = fn;
}
template<typename CompType>
void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info = entt::type_id<CompType>()) { registerSerializerJson(fn, type_info); }
void registerSerializerJson(
serialize_json_fn fn = component_get_json<CompType>,
const entt::type_info& type_info = entt::type_id<CompType>()
) {
registerSerializerJson(fn, type_info);
}
void registerDeSerializerJson(deserialize_json_fn fn, const entt::type_info& type_info);
void registerDeSerializerJson(deserialize_json_fn fn, const entt::type_info& type_info) {
_deserl_json[type_info.hash()] = fn;
}
template<typename CompType>
void registerDeSerializerJson(