start refactoring in the name of object store

This commit is contained in:
Green Sky 2024-04-06 22:27:53 +02:00
parent fd0b210bbb
commit 26d07b06db
No known key found for this signature in database
3 changed files with 161 additions and 2 deletions

View File

@ -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

View File

@ -0,0 +1,88 @@
#include "./object_store.hpp"
#include "./meta_components.hpp"
#include <nlohmann/json.hpp> // this sucks
#include <iostream>
static bool serl_json_data_enc_type(const ObjectHandle oh, nlohmann::json& out) {
out = static_cast<std::underlying_type_t<Encryption>>(
oh.get<FragComp::DataEncryptionType>().enc
);
return true;
}
static bool deserl_json_data_enc_type(ObjectHandle oh, const nlohmann::json& in) {
oh.emplace_or_replace<FragComp::DataEncryptionType>(
static_cast<Encryption>(
static_cast<std::underlying_type_t<Encryption>>(in)
)
);
return true;
}
static bool serl_json_data_comp_type(const ObjectHandle oh, nlohmann::json& out) {
out = static_cast<std::underlying_type_t<Compression>>(
oh.get<FragComp::DataCompressionType>().comp
);
return true;
}
static bool deserl_json_data_comp_type(ObjectHandle oh, const nlohmann::json& in) {
oh.emplace_or_replace<FragComp::DataCompressionType>(
static_cast<Compression>(
static_cast<std::underlying_type_t<Compression>>(in)
)
);
return true;
}
ObjectStore2::ObjectStore2(void) {
_sc.registerSerializerJson<FragComp::DataEncryptionType>(serl_json_data_enc_type);
_sc.registerDeSerializerJson<FragComp::DataEncryptionType>(deserl_json_data_enc_type);
_sc.registerSerializerJson<FragComp::DataCompressionType>(serl_json_data_comp_type);
_sc.registerDeSerializerJson<FragComp::DataCompressionType>(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}
}
);
}

View File

@ -0,0 +1,67 @@
#pragma once
#include <solanaceae/util/event_provider.hpp>
#include "./serializer.hpp" // TODO: get rid of the tight nljson integration
#include <entt/entity/registry.hpp>
#include <entt/entity/handle.hpp>
#include <cstdint>
// internal id
enum class Object : uint32_t {};
using ObjectRegistry = entt::basic_registry<Object>;
using ObjectHandle = entt::basic_handle<ObjectRegistry>;
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<ObjectStoreEventI>;
struct ObjectStore2 : public ObjectStoreEventProviderI {
static constexpr const char* version {"2"};
ObjectRegistry _reg;
SerializerCallbacks<Object> _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);
};