From 73d1d6514255103d019c795dabc8f0895ffd3833 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Thu, 15 Feb 2024 19:06:56 +0100 Subject: [PATCH] further serializer refactoring --- src/CMakeLists.txt | 1 - src/fragment_store/fragment_store.cpp | 60 ++++++++----------- src/fragment_store/fragment_store.hpp | 5 +- src/fragment_store/message_fragment_store.cpp | 30 +++++----- src/fragment_store/message_fragment_store.hpp | 2 +- src/fragment_store/serializer.cpp | 10 ---- src/fragment_store/serializer.hpp | 51 +++++++++------- src/fragment_store/test_fragstore.cpp | 20 ++----- 8 files changed, 78 insertions(+), 101 deletions(-) delete mode 100644 src/fragment_store/serializer.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9acd1e3..6e3e87e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,7 +6,6 @@ add_library(fragment_store ./fragment_store/meta_components.hpp ./fragment_store/meta_components_id.inl ./fragment_store/serializer.hpp - ./fragment_store/serializer.cpp ./fragment_store/fragment_store.hpp ./fragment_store/fragment_store.cpp ) diff --git a/src/fragment_store/fragment_store.cpp b/src/fragment_store/fragment_store.cpp index 97dd67a..c0aa02c 100644 --- a/src/fragment_store/fragment_store.cpp +++ b/src/fragment_store/fragment_store.cpp @@ -291,7 +291,7 @@ bool FragmentStore::syncToStorage(FragmentID fid, std::functionsecond(storage.value(fid), meta_data[storage.type().hash()]); //} else if (meta_type == MetaFileType::TEXT_JSON) { - s_cb_it->second(storage.value(fid), meta_data[storage.type().name()]); + s_cb_it->second({_reg, fid}, meta_data[storage.type().name()]); //} } @@ -521,52 +521,44 @@ void FragmentStore::scanStoragePathAsync(std::string path) { scanStoragePath(path); // TODO: make async and post result } -static bool serl_json_data_enc_type(void* comp, nlohmann::json& out) { - if (comp == nullptr) { - return false; - } - - auto& r_comp = *reinterpret_cast(comp); - - out = static_cast>(r_comp.enc); - +static bool serl_json_data_enc_type(const FragmentHandle fh, nlohmann::json& out) { + out = static_cast>( + fh.get().enc + ); return true; } static bool deserl_json_data_enc_type(FragmentHandle fh, const nlohmann::json& in) { - // TODO: this is ugly in multiple places - try { - fh.emplace_or_replace( - static_cast( - static_cast>(in) - ) - ); - } catch(...) { - return false; - } + fh.emplace_or_replace( + static_cast( + static_cast>(in) + ) + ); return true; } -static bool serl_json_data_comp_type(void* comp, nlohmann::json& out) { - if (comp == nullptr) { - return false; - } - - auto& r_comp = *reinterpret_cast(comp); - - out = static_cast>(r_comp.comp); - +static bool serl_json_data_comp_type(const FragmentHandle fh, nlohmann::json& out) { + out = static_cast>( + fh.get().comp + ); return true; } +static bool deserl_json_data_comp_type(FragmentHandle fh, const nlohmann::json& in) { + fh.emplace_or_replace( + static_cast( + static_cast>(in) + ) + ); + return true; +} + +// TODO: dserl comp type + void FragmentStore::registerSerializers(void) { _sc.registerSerializerJson(serl_json_data_enc_type); _sc.registerDeSerializerJson(deserl_json_data_enc_type); _sc.registerSerializerJson(serl_json_data_comp_type); - - std::cout << "registered serl text json cbs:\n"; - for (const auto& [type_id, _] : _sc._serl_json) { - std::cout << " " << type_id << "\n"; - } + _sc.registerDeSerializerJson(deserl_json_data_comp_type); } diff --git a/src/fragment_store/fragment_store.hpp b/src/fragment_store/fragment_store.hpp index 1025f9e..101a933 100644 --- a/src/fragment_store/fragment_store.hpp +++ b/src/fragment_store/fragment_store.hpp @@ -17,9 +17,6 @@ #include #include -// fwd -struct SerializerCallbacks; - struct FragmentStore : public FragmentStoreI { FragmentRegistry _reg; @@ -31,7 +28,7 @@ struct FragmentStore : public FragmentStoreI { uint64_t _memory_budget {10u*1024u*1024u}; uint64_t _memory_usage {0u}; - SerializerCallbacks _sc; + SerializerCallbacks _sc; FragmentStore(void); FragmentStore(std::array session_uuid_namespace); diff --git a/src/fragment_store/message_fragment_store.cpp b/src/fragment_store/message_fragment_store.cpp index 07383a7..bc7804b 100644 --- a/src/fragment_store/message_fragment_store.cpp +++ b/src/fragment_store/message_fragment_store.cpp @@ -214,37 +214,37 @@ MessageFragmentStore::MessageFragmentStore( _rmm.subscribe(this, RegistryMessageModel_Event::message_updated); _rmm.subscribe(this, RegistryMessageModel_Event::message_destroy); - _fs._sc.registerSerializerJson(serl_json_msg_ts_range); + _fs._sc.registerSerializerJson(); _fs._sc.registerDeSerializerJson(); - _fs._sc.registerSerializerJson(serl_json_msg_c_id); + _fs._sc.registerSerializerJson(); _fs._sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); // files //_sc.registerSerializerJson() - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); - _sc.registerSerializerJson(serl_json_default); + _sc.registerSerializerJson(); _sc.registerDeSerializerJson(); _fs.scanStoragePath("test_message_store/"); @@ -292,7 +292,7 @@ float MessageFragmentStore::tick(float time_delta) { continue; } - s_cb_it->second(storage.value(m), j_entry[storage.type().name()]); + s_cb_it->second({*reg, m}, j_entry[storage.type().name()]); } } diff --git a/src/fragment_store/message_fragment_store.hpp b/src/fragment_store/message_fragment_store.hpp index 58ded02..8039470 100644 --- a/src/fragment_store/message_fragment_store.hpp +++ b/src/fragment_store/message_fragment_store.hpp @@ -36,7 +36,7 @@ namespace Fragment::Components { }; } // Fragment::Components -struct MessageSerializerCallbacks : public SerializerCallbacks { +struct MessageSerializerCallbacks : public SerializerCallbacks { // TODO: add contact and message reg, so entities can be looked up and be converted to fragment uids OR persistent ids }; diff --git a/src/fragment_store/serializer.cpp b/src/fragment_store/serializer.cpp deleted file mode 100644 index 1f3bfe4..0000000 --- a/src/fragment_store/serializer.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "./serializer.hpp" - -void SerializerCallbacks::registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info) { - _serl_json[type_info.hash()] = fn; -} - -void SerializerCallbacks::registerDeSerializerJson(deserialize_json_fn fn, const entt::type_info& type_info) { - _deserl_json[type_info.hash()] = fn; -} - diff --git a/src/fragment_store/serializer.hpp b/src/fragment_store/serializer.hpp index 1f94f0d..a1daa98 100644 --- a/src/fragment_store/serializer.hpp +++ b/src/fragment_store/serializer.hpp @@ -6,32 +6,24 @@ #include -#include "./fragment_store_i.hpp" - +template struct SerializerCallbacks { + using Registry = entt::basic_registry; + using Handle = entt::basic_handle; + // 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 _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 _deserl_json; template - static bool component_emplace_or_replace_json(FragmentHandle fh, const nlohmann::json& j) { - if constexpr (std::is_empty_v) { - fh.emplace_or_replace(); // assert empty json? - } else { - fh.emplace_or_replace(static_cast(j)); - } - return true; - } - - template - static bool component_get_json(const FragmentHandle fh, nlohmann::json& j) { - if (fh.all_of()) { + static bool component_get_json(const Handle h, nlohmann::json& j) { + if (h.template all_of()) { if constexpr (!std::is_empty_v) { - j = fh.get(); + j = h.template get(); } return true; } @@ -39,12 +31,31 @@ struct SerializerCallbacks { return false; } - void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info); + 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 registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info) { + _serl_json[type_info.hash()] = fn; + } template - void registerSerializerJson(serialize_json_fn fn, const entt::type_info& type_info = entt::type_id()) { registerSerializerJson(fn, type_info); } + void registerSerializerJson( + serialize_json_fn fn = component_get_json, + const entt::type_info& type_info = entt::type_id() + ) { + 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 void registerDeSerializerJson( diff --git a/src/fragment_store/test_fragstore.cpp b/src/fragment_store/test_fragstore.cpp index 581f64a..7136643 100644 --- a/src/fragment_store/test_fragstore.cpp +++ b/src/fragment_store/test_fragstore.cpp @@ -11,28 +11,16 @@ namespace Components { uint64_t begin {0}; uint64_t end {1000}; }; + + NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MessagesTimestampRange, begin, end) } // Components -static bool serl_json_msg_ts_range(void* comp, nlohmann::json& out) { - if (comp == nullptr) { - return false; - } - - out = nlohmann::json::object(); - - auto& r_comp = *reinterpret_cast(comp); - - out["begin"] = r_comp.begin; - out["end"] = r_comp.end; - - return true; -} - int main(void) { FragmentStore fs; fs._default_store_path = "test_store/"; - fs._sc.registerSerializerJson(serl_json_msg_ts_range); + fs._sc.registerSerializerJson(); + fs._sc.registerDeSerializerJson(); const auto frag0 = fs.newFragmentFile("", MetaFileType::TEXT_JSON, {0xff, 0xf1, 0xf2, 0xf0, 0xff, 0xff, 0xff, 0xf9});