diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4643d2b7..f83d2861 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,16 +7,20 @@ add_library(fragment_store ./fragment_store/uuid_generator.hpp ./fragment_store/uuid_generator.cpp - ./fragment_store/fragment_store_i.hpp - ./fragment_store/fragment_store_i.cpp ./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/fragment_store_i.hpp + ./fragment_store/fragment_store_i.cpp ./fragment_store/fragment_store.hpp ./fragment_store/fragment_store.cpp + #new + ./fragment_store/object_store.hpp + ./fragment_store/object_store.cpp ./json/message_components.hpp # TODO: move ./json/tox_message_components.hpp # TODO: move diff --git a/src/fragment_store/object_store.cpp b/src/fragment_store/object_store.cpp new file mode 100644 index 00000000..0806c618 --- /dev/null +++ b/src/fragment_store/object_store.cpp @@ -0,0 +1,88 @@ +#include "./object_store.hpp" + +#include "./meta_components.hpp" + +#include // this sucks + +#include + +static bool serl_json_data_enc_type(const ObjectHandle oh, nlohmann::json& out) { + 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) { + 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; +} + +ObjectStore2::ObjectStore2(void) { + _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); +} + +ObjectStore2::~ObjectStore2(void) { +} + +ObjectRegistry& ObjectStore2::registry(void) { + return _reg; +} + +ObjectHandle ObjectStore2::objectHandle(const Object o) { + return {_reg, o}; +} + +void ObjectStore2::throwEventConstruct(const Object o) { + std::cout << "OS debug: event construct " << entt::to_integral(o) << "\n"; + dispatch( + ObjectStore_Event::object_construct, + ObjectStore::Events::ObjectConstruct{ + ObjectHandle{_reg, o} + } + ); +} + +void ObjectStore2::throwEventUpdate(const Object o) { + std::cout << "OS debug: event update " << entt::to_integral(o) << "\n"; + dispatch( + ObjectStore_Event::object_update, + ObjectStore::Events::ObjectUpdate{ + ObjectHandle{_reg, o} + } + ); +} + +void ObjectStore2::throwEventDestroy(const Object o) { + std::cout << "OS debug: event destroy " << entt::to_integral(o) << "\n"; + dispatch( + ObjectStore_Event::object_destroy, + ObjectStore::Events::ObjectUpdate{ + ObjectHandle{_reg, o} + } + ); +} + diff --git a/src/fragment_store/object_store.hpp b/src/fragment_store/object_store.hpp new file mode 100644 index 00000000..764a9794 --- /dev/null +++ b/src/fragment_store/object_store.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include + +#include "./serializer.hpp" // TODO: get rid of the tight nljson integration + +#include +#include + +#include + +// internal id +enum class Object : uint32_t {}; +using ObjectRegistry = entt::basic_registry; +using ObjectHandle = entt::basic_handle; + +namespace ObjectStore::Events { + + struct ObjectConstruct { + const ObjectHandle e; + }; + struct ObjectUpdate { + const ObjectHandle e; + }; + struct ObjectDestory { + const ObjectHandle e; + }; + +} // ObjectStore::Events + +enum class ObjectStore_Event : uint16_t { + object_construct, + object_update, + object_destroy, + + MAX +}; + +struct ObjectStoreEventI { + using enumType = ObjectStore_Event; + + virtual ~ObjectStoreEventI(void) {} + + virtual bool onEvent(const ObjectStore::Events::ObjectConstruct&) { return false; } + virtual bool onEvent(const ObjectStore::Events::ObjectUpdate&) { return false; } + virtual bool onEvent(const ObjectStore::Events::ObjectDestory&) { return false; } +}; +using ObjectStoreEventProviderI = EventProviderI; + +struct ObjectStore2 : public ObjectStoreEventProviderI { + static constexpr const char* version {"2"}; + + ObjectRegistry _reg; + + SerializerCallbacks _sc; + + ObjectStore2(void); + virtual ~ObjectStore2(void); + + ObjectRegistry& registry(void); + ObjectHandle objectHandle(const Object o); + + void throwEventConstruct(const Object o); + void throwEventUpdate(const Object o); + void throwEventDestroy(const Object o); +}; +