diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 09d10aa..ffd0531 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,15 +10,16 @@ add_library(fragment_store ./fragment_store/types.hpp ./fragment_store/meta_components.hpp ./fragment_store/meta_components_id.inl - ./fragment_store/serializer.hpp ./fragment_store/file2_stack.hpp ./fragment_store/file2_stack.cpp #old + ./fragment_store/serializer.hpp ./fragment_store/fragment_store_i.hpp ./fragment_store/fragment_store_i.cpp ./fragment_store/fragment_store.hpp ./fragment_store/fragment_store.cpp #new + ./fragment_store/serializer_json.hpp ./fragment_store/object_store.hpp ./fragment_store/object_store.cpp ./fragment_store/backends/filesystem_storage.hpp diff --git a/src/fragment_store/backends/filesystem_storage.cpp b/src/fragment_store/backends/filesystem_storage.cpp index 222a971..b5ae420 100644 --- a/src/fragment_store/backends/filesystem_storage.cpp +++ b/src/fragment_store/backends/filesystem_storage.cpp @@ -1,6 +1,7 @@ #include "./filesystem_storage.hpp" #include "../meta_components.hpp" +#include "../serializer_json.hpp" #include @@ -38,60 +39,10 @@ static ByteSpan spanFromRead(const std::variant>& } } -// TODO: move somewhere else -static bool serl_json_data_enc_type(const ObjectHandle oh, nlohmann::json& out) { - if (!oh.all_of()) { - return false; - } - - out = static_cast>( - oh.get().enc - ); - return true; -} - -static bool deserl_json_data_enc_type(ObjectHandle oh, const nlohmann::json& in) { - oh.emplace_or_replace( - static_cast( - static_cast>(in) - ) - ); - return true; -} - -static bool serl_json_data_comp_type(const ObjectHandle oh, nlohmann::json& out) { - if (!oh.all_of()) { - return false; - } - - out = static_cast>( - oh.get().comp - ); - return true; -} - -static bool deserl_json_data_comp_type(ObjectHandle oh, const nlohmann::json& in) { - oh.emplace_or_replace( - static_cast( - static_cast>(in) - ) - ); - return true; -} namespace backend { FilesystemStorage::FilesystemStorage(ObjectStore2& os, std::string_view storage_path) : StorageBackendI::StorageBackendI(os), _storage_path(storage_path) { - _sc.registerSerializerJson(serl_json_data_enc_type); - _sc.registerDeSerializerJson(deserl_json_data_enc_type); - _sc.registerSerializerJson(serl_json_data_comp_type); - _sc.registerDeSerializerJson(deserl_json_data_comp_type); - - // old stuff - _sc.registerSerializerJson(serl_json_data_enc_type); - _sc.registerDeSerializerJson(deserl_json_data_enc_type); - _sc.registerSerializerJson(serl_json_data_comp_type); - _sc.registerDeSerializerJson(deserl_json_data_comp_type); } FilesystemStorage::~FilesystemStorage(void) { @@ -227,6 +178,8 @@ bool FilesystemStorage::write(Object o, std::function>(); + // TODO: refactor extract to OS for (const auto& [type_id, storage] : reg.storage()) { if (!storage.contains(o)) { @@ -236,8 +189,8 @@ bool FilesystemStorage::write(Object o, std::function>(); + std::vector scanned_objs; // step 3: parse meta and insert into reg of non preexising // main thread @@ -637,8 +592,8 @@ size_t FilesystemStorage::scanPath(std::string_view path) { for (const auto& [k, v] : j.items()) { // type id from string hash const auto type_id = entt::hashed_string(k.data(), k.size()); - const auto deserl_fn_it = _sc._deserl_json.find(type_id); - if (deserl_fn_it != _sc._deserl_json.cend()) { + const auto deserl_fn_it = sjc._deserl.find(type_id); + if (deserl_fn_it != sjc._deserl.cend()) { // TODO: check return value deserl_fn_it->second(oh, v); } else { diff --git a/src/fragment_store/backends/filesystem_storage.hpp b/src/fragment_store/backends/filesystem_storage.hpp index ef5c111..626f000 100644 --- a/src/fragment_store/backends/filesystem_storage.hpp +++ b/src/fragment_store/backends/filesystem_storage.hpp @@ -26,11 +26,6 @@ struct FilesystemStorage : public StorageBackendI { private: size_t scanPath(std::string_view path); void scanPathAsync(std::string path); - - public: // TODO: private? - // this thing needs to change and be facilitated over the OS - // but the json serializer are specific to the backend - SerializerCallbacks _sc; }; } // backend diff --git a/src/fragment_store/convert_frag_to_obj.cpp b/src/fragment_store/convert_frag_to_obj.cpp index 0329212..7d36bfa 100644 --- a/src/fragment_store/convert_frag_to_obj.cpp +++ b/src/fragment_store/convert_frag_to_obj.cpp @@ -1,6 +1,7 @@ #include "./object_store.hpp" #include "./backends/filesystem_storage.hpp" #include "./meta_components.hpp" +#include "./serializer_json.hpp" #include @@ -97,7 +98,7 @@ int main(int argc, const char** argv) { oh.emplace_or_replace(e.e.get_or_emplace()); // serializable - for (const auto& [type, fn] : _fsb_src._sc._serl_json) { + for (const auto& [type, fn] : _os_src.registry().ctx().get>()._serl) { //if (!e.e.registry()->storage(type)->contains(e.e)) { //continue; //} @@ -106,7 +107,7 @@ int main(int argc, const char** argv) { // raw copy might be better in the future nlohmann::json tmp_j; if (fn(e.e, tmp_j)) { - _fsb_dst._sc._deserl_json.at(type)(oh, tmp_j); + _os_dst.registry().ctx().get>()._deserl.at(type)(oh, tmp_j); } } } diff --git a/src/fragment_store/object_store.cpp b/src/fragment_store/object_store.cpp index 663d1f9..d5e2dae 100644 --- a/src/fragment_store/object_store.cpp +++ b/src/fragment_store/object_store.cpp @@ -2,10 +2,53 @@ #include "./meta_components.hpp" +#include "./serializer_json.hpp" + #include // this sucks #include +// TODO: move somewhere else +static bool serl_json_data_enc_type(const ObjectHandle oh, nlohmann::json& out) { + if (!oh.all_of()) { + return false; + } + + out = static_cast>( + oh.get().enc + ); + return true; +} + +static bool deserl_json_data_enc_type(ObjectHandle oh, const nlohmann::json& in) { + oh.emplace_or_replace( + static_cast( + static_cast>(in) + ) + ); + return true; +} + +static bool serl_json_data_comp_type(const ObjectHandle oh, nlohmann::json& out) { + if (!oh.all_of()) { + return false; + } + + out = static_cast>( + oh.get().comp + ); + return true; +} + +static bool deserl_json_data_comp_type(ObjectHandle oh, const nlohmann::json& in) { + oh.emplace_or_replace( + static_cast( + static_cast>(in) + ) + ); + return true; +} + StorageBackendI::StorageBackendI(ObjectStore2& os) : _os(os) { } @@ -23,6 +66,18 @@ bool StorageBackendI::write(Object o, const ByteSpan data) { } ObjectStore2::ObjectStore2(void) { + // HACK: set them up independently + auto& sjc = _reg.ctx().emplace>(); + sjc.registerSerializer(serl_json_data_enc_type); + sjc.registerDeSerializer(deserl_json_data_enc_type); + sjc.registerSerializer(serl_json_data_comp_type); + sjc.registerDeSerializer(deserl_json_data_comp_type); + + // old stuff + sjc.registerSerializer(serl_json_data_enc_type); + sjc.registerDeSerializer(deserl_json_data_enc_type); + sjc.registerSerializer(serl_json_data_comp_type); + sjc.registerDeSerializer(deserl_json_data_comp_type); } ObjectStore2::~ObjectStore2(void) { diff --git a/src/fragment_store/object_store.hpp b/src/fragment_store/object_store.hpp index fcc4456..03bac9b 100644 --- a/src/fragment_store/object_store.hpp +++ b/src/fragment_store/object_store.hpp @@ -3,8 +3,6 @@ #include #include -#include "./serializer.hpp" // TODO: get rid of the tight nljson integration - #include #include diff --git a/src/fragment_store/serializer_json.hpp b/src/fragment_store/serializer_json.hpp new file mode 100644 index 0000000..cd74540 --- /dev/null +++ b/src/fragment_store/serializer_json.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include + +#include + +// nlohmann +template +struct SerializerJsonCallbacks { + using Registry = entt::basic_registry; + using Handle = entt::basic_handle; + + using serialize_fn = bool(*)(const Handle h, nlohmann::json& out); + entt::dense_map _serl; + + using deserialize_fn = bool(*)(Handle h, const nlohmann::json& in); + entt::dense_map _deserl; + + template + static bool component_get_json(const Handle h, nlohmann::json& j) { + if (h.template all_of()) { + if constexpr (!std::is_empty_v) { + j = h.template get(); + } + return true; + } + + return false; + } + + template + static bool component_emplace_or_replace_json(Handle h, const nlohmann::json& j) { + if constexpr (std::is_empty_v) { + h.template emplace_or_replace(); // assert empty json? + } else { + h.template emplace_or_replace(static_cast(j)); + } + return true; + } + + void registerSerializer(serialize_fn fn, const entt::type_info& type_info) { + _serl[type_info.hash()] = fn; + } + + template + void registerSerializer( + serialize_fn fn = component_get_json, + const entt::type_info& type_info = entt::type_id() + ) { + registerSerializer(fn, type_info); + } + + void registerDeSerializer(deserialize_fn fn, const entt::type_info& type_info) { + _deserl[type_info.hash()] = fn; + } + + template + void registerDeSerializer( + deserialize_fn fn = component_emplace_or_replace_json, + const entt::type_info& type_info = entt::type_id() + ) { + registerDeSerializer(fn, type_info); + } +}; +