fixup compile errors and allow fetching property properly from cache

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
Folling 2023-11-27 13:13:54 +01:00 committed by Folling
parent 549bad7aac
commit cc73cf000d
No known key found for this signature in database
21 changed files with 213 additions and 122 deletions

View file

@ -14,5 +14,5 @@ FunctionContext::~FunctionContext() {
_project->error_infos = {};
}
_project->_function_contexts.pop();
_project->_function_contexts.pop_back();
}

View file

@ -2,6 +2,9 @@
#include "ikarus/persistence/project.h"
#include <objects/properties/number_property.hpp>
#include <objects/properties/text_property.hpp>
#include <objects/properties/toggle_property.hpp>
#include <persistence/function_context.hpp>
auto IkarusProject::get_name() const -> std::string_view {
@ -21,11 +24,11 @@ auto IkarusProject::get_db() const -> sqlitecpp::Connection const * {
}
auto IkarusProject::get_function_context() -> FunctionContext * {
return &_function_contexts.emplace(this);
return &_function_contexts.emplace_back(this);
}
IkarusBlueprint * IkarusProject::get_blueprint(IkarusId id) {
return get_cached_object(id, this->_blueprints);
return get_cached_object<IkarusBlueprint>(id, this->_blueprints);
}
auto IkarusProject::uncache_blueprint(IkarusBlueprint * blueprint) -> void {
@ -33,15 +36,28 @@ auto IkarusProject::uncache_blueprint(IkarusBlueprint * blueprint) -> void {
}
auto IkarusProject::get_entity(IkarusId id) -> IkarusEntity * {
return get_cached_object(id, this->_entities);
return get_cached_object<IkarusEntity>(id, this->_entities);
}
auto IkarusProject::uncache_entity(IkarusEntity * entity) -> void {
remove_cached_object(entity, _entities);
}
auto IkarusProject::get_property(IkarusId id) -> IkarusProperty * {
return get_cached_object(id, this->_properties);
auto IkarusProject::get_property(IkarusId id, IkarusPropertyType type) -> IkarusProperty * {
auto const iter = _properties.find(id);
if (iter == _properties.cend()) {
switch (type) {
case IkarusPropertyType_Toggle:
return _properties.emplace(id, std::make_unique<IkarusToggleProperty>(this, id)).first->second.get();
case IkarusPropertyType_Number:
return _properties.emplace(id, std::make_unique<IkarusNumberProperty>(this, id)).first->second.get();
case IkarusPropertyType_Text:
return _properties.emplace(id, std::make_unique<IkarusTextProperty>(this, id)).first->second.get();
}
}
return iter->second.get();
}
auto IkarusProject::uncache_property(IkarusProperty * property) -> void {

View file

@ -9,6 +9,11 @@
#include <ikarus/errors.h>
#include <ikarus/id.h>
#include <ikarus/objects/properties/property_type.h>
#include <objects/blueprint.hpp>
#include <objects/entity.hpp>
#include <objects/properties/property.hpp>
constexpr inline auto MAXIMUM_ERROR_INFOS = 8;
@ -32,18 +37,16 @@ public:
[[nodiscard]] auto get_entity(IkarusId id) -> struct IkarusEntity *;
auto uncache_entity(struct IkarusEntity * entity) -> void;
[[nodiscard]] auto get_property(IkarusId id) -> struct IkarusProperty *;
[[nodiscard]] auto get_property(IkarusId id, IkarusPropertyType type) -> struct IkarusProperty *;
auto uncache_property(struct IkarusProperty * property) -> void;
private:
template<typename T>
[[nodiscard]] T * get_cached_object(IkarusId id, std::unordered_map<IkarusId, std::unique_ptr<T>>& cache) {
[[nodiscard]] T * get_cached_object(IkarusId id, auto& cache) {
auto const iter = cache.find(id);
if (iter == cache.cend()) {
auto [ret_iter, _] = cache.emplace(id, std::make_unique<T>(this, id));
return ret_iter->second.get();
return cache.emplace(id, std::make_unique<T>(this, id)).first->second.get();
}
return iter->second.get();
@ -51,7 +54,7 @@ private:
template<typename T>
void remove_cached_object(T * object, std::unordered_map<IkarusId, std::unique_ptr<T>>& cache) {
cache.erase(object->id);
cache.erase(object->get_id());
}
private:
@ -68,5 +71,5 @@ private:
std::unordered_map<IkarusId, std::unique_ptr<IkarusProperty>> _properties;
std::unordered_map<IkarusId, std::unique_ptr<IkarusEntity>> _entities;
std::stack<FunctionContext> _function_contexts;
std::vector<FunctionContext> _function_contexts;
};