make object fields public and fixup compile errors

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
Folling 2023-12-26 13:09:41 +01:00 committed by Folling
parent 8a76573983
commit 5da995b47e
No known key found for this signature in database
13 changed files with 258 additions and 216 deletions

View file

@ -1,6 +1,6 @@
Checks: >- Checks: >-
-*, -*,
bugprone-*, bugprone-*, -bugprone-lambda-function-name,
cppcoreguidelines-*, -cppcoreguidelines-owning-memory, cppcoreguidelines-*, -cppcoreguidelines-owning-memory,
clang-analyzer-*, clang-analyzer-*,
google-*, -google-readability-todo, google-*, -google-readability-todo,

View file

@ -35,13 +35,6 @@ IKA_API IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project
/// \remark The blueprint must not be accessed after deletion. /// \remark The blueprint must not be accessed after deletion.
IKA_API void ikarus_blueprint_delete(IkarusBlueprint * blueprint); IKA_API void ikarus_blueprint_delete(IkarusBlueprint * blueprint);
/// \brief Gets the number of properties of a blueprint.
/// \param blueprint The blueprint to get the number of properties of.
/// \pre \li Must not be null.
/// \pre \li Must exist.
/// \return The number of properties or undefined if an error occurs.
IKA_API size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint);
/// \brief Gets the properties of a blueprint. /// \brief Gets the properties of a blueprint.
/// \param blueprint The blueprint to get the properties of. /// \param blueprint The blueprint to get the properties of.
/// \pre \li Must not be null. /// \pre \li Must not be null.
@ -49,16 +42,17 @@ IKA_API size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * bluep
/// \param properties_out The buffer to write the properties to. /// \param properties_out The buffer to write the properties to.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \param properties_out_size The size of the buffer. /// \param properties_out_size The size of the buffer.
/// \see ikarus_blueprint_get_property_count
IKA_API void ikarus_blueprint_get_properties( IKA_API void ikarus_blueprint_get_properties(
IkarusBlueprint const * blueprint, struct IkarusProperty ** properties_out, size_t properties_out_size IkarusBlueprint const * blueprint, struct IkarusProperty ** properties_out, size_t properties_out_size
); );
/// \brief Gets the number of entities linked to a blueprint. /// \brief Gets the number of properties of a blueprint.
/// \param blueprint The blueprint to get the number of linked entities of. /// \param blueprint The blueprint to get the number of properties of.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \pre \li Must exist. /// \pre \li Must exist.
/// \return The number of linked entities or undefined if an error occurs. /// \return The number of properties or undefined if an error occurs.
IKA_API size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint); IKA_API size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint);
/// \brief Gets the entities linked to a blueprint. /// \brief Gets the entities linked to a blueprint.
/// \param blueprint The blueprint to get the linked entities of. /// \param blueprint The blueprint to get the linked entities of.
@ -67,10 +61,18 @@ IKA_API size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const *
/// \param entities_out The buffer to write the entities to. /// \param entities_out The buffer to write the entities to.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \param entities_out_size The size of the buffer. /// \param entities_out_size The size of the buffer.
/// \see ikarus_blueprint_get_linked_entity_count
IKA_API void ikarus_blueprint_get_linked_entities( IKA_API void ikarus_blueprint_get_linked_entities(
IkarusBlueprint const * blueprint, struct IkarusEntity ** entities_out, size_t entities_out_size IkarusBlueprint const * blueprint, struct IkarusEntity ** entities_out, size_t entities_out_size
); );
/// \brief Gets the number of entities linked to a blueprint.
/// \param blueprint The blueprint to get the number of linked entities of.
/// \pre \li Must not be null.
/// \pre \li Must exist.
/// \return The number of linked entities or undefined if an error occurs.
IKA_API size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint);
/// \brief Casts a blueprint to an object. /// \brief Casts a blueprint to an object.
/// \param blueprint The blueprint to cast. /// \param blueprint The blueprint to cast.
/// \pre \li Must not be null. /// \pre \li Must not be null.

View file

@ -45,13 +45,9 @@ struct IkarusEntity;
/// \param name The name of the entity. /// \param name The name of the entity.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \pre \li Must not be empty. /// \pre \li Must not be empty.
/// \param blueprints Blueprints to link the entity to (0..n). Null is treated as an empty array.
/// \param blueprints_count The number of blueprints.
/// \return The created entity or null if an error occurs. /// \return The created entity or null if an error occurs.
/// \remark Must be freed using #ikarus_free. /// \remark Must be freed using #ikarus_free.
IKA_API IkarusEntity * ikarus_entity_create( IKA_API IkarusEntity * ikarus_entity_create(struct IkarusProject * project, char const * name);
struct IkarusProject * project, char const * name, struct IkarusBlueprint ** blueprints, size_t blueprints_count
);
/// \brief Deletes an entity. /// \brief Deletes an entity.
/// \param entity The entity to delete. /// \param entity The entity to delete.

View file

@ -22,6 +22,12 @@ enum IkarusObjectType {
IkarusObjectType_Property = 0b00000011, IkarusObjectType_Property = 0b00000011,
}; };
/// \brief Converts an IkarusObjectType to a string.
/// \param type The type to convert.
/// \return The string representation of the type.
/// \remark The returned string must not be freed.
char const * ikarus_object_type_to_string(IkarusObjectType type);
IKARUS_END_HEADER IKARUS_END_HEADER
/// @} /// @}

View file

@ -1,13 +1,16 @@
#include "ikarus/objects/blueprint.h"
#include "objects/blueprint.hpp"
#include <iterator> #include <iterator>
#include <cppbase/logger.hpp> #include <cppbase/logger.hpp>
#include <cppbase/result.hpp> #include <cppbase/result.hpp>
#include <cppbase/strings.hpp> #include <cppbase/strings.hpp>
#include <ikarus/objects/blueprint.h>
#include <objects/blueprint.hpp>
#include <objects/entity.hpp> #include <objects/entity.hpp>
#include <objects/object_helper.hpp>
#include <objects/properties/property.hpp>
#include <persistence/function_context.hpp> #include <persistence/function_context.hpp>
#include <persistence/project.hpp> #include <persistence/project.hpp>
@ -26,40 +29,9 @@ IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char c
return nullptr; return nullptr;
} }
VTRYRV( VTRYRV(auto const id, nullptr, insert_object(project, ctx, IkarusObjectType_Blueprint, name, [](auto * db, IkarusId id) {
auto const id, return db->execute("INSERT INTO `blueprints`(`id`) VALUES(?)", id);
nullptr, }));
project->get_db()
->transact([name](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
LOG_VERBOSE("creating blueprint in objects table");
TRY(db->execute(
"INSERT INTO `objects` (`object_type`, `name`) VALUES(?, ?);",
static_cast<int>(IkarusObjectType_Blueprint),
name
));
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Blueprint);
LOG_DEBUG("blueprint is {}", id);
LOG_VERBOSE("inserting blueprint into blueprints table");
TRY(db->execute("INSERT INTO `blueprints`(`id`) VALUES(?);", id));
return cppbase::ok(id);
})
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("unable to insert blueprint into database: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_VERBOSE("successfully created blueprint");
return project->get_blueprint(id); return project->get_blueprint(id);
} }
@ -67,60 +39,11 @@ IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char c
void ikarus_blueprint_delete(IkarusBlueprint * blueprint) { void ikarus_blueprint_delete(IkarusBlueprint * blueprint) {
LOG_INFO("deleting blueprint"); LOG_INFO("deleting blueprint");
LOG_DEBUG("project={};blueprint={}", blueprint->get_project()->get_path().c_str(), blueprint->get_id()); LOG_DEBUG("project={}; blueprint={}", blueprint->project->get_path().c_str(), blueprint->id);
auto * ctx = blueprint->get_project()->get_function_context(); auto * ctx = blueprint->project->get_function_context();
TRYRV( delete_object(blueprint->project, ctx, blueprint);
,
blueprint->get_project()
->get_db()
->execute("DELETE FROM `objects` WHERE `id` = ?", blueprint->get_id())
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to delete blueprint from objects table: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_VERBOSE("blueprint was successfully deleted from database, freeing");
blueprint->get_project()->uncache_blueprint(blueprint);
LOG_VERBOSE("successfully deleted blueprint");
}
size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
LOG_VERBOSE("fetching blueprint property count");
auto * ctx = blueprint->get_project()->get_function_context();
LOG_DEBUG("blueprint={}", blueprint->get_id());
VTRYRV(
auto count,
0,
blueprint->get_project()
->get_db()
->query_one<int>("SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint` = ?;", blueprint->get_id())
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint property count: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint property count: {}", count);
LOG_VERBOSE("successfully fetched blueprint property count");
return static_cast<size_t>(count);
} }
void ikarus_blueprint_get_properties( void ikarus_blueprint_get_properties(
@ -129,25 +52,25 @@ void ikarus_blueprint_get_properties(
LOG_VERBOSE("fetching blueprint properties"); LOG_VERBOSE("fetching blueprint properties");
LOG_DEBUG( LOG_DEBUG(
"project={};blueprint={};properties_out_size={}", "project={}; blueprint={}; properties_out_size={}",
blueprint->get_project()->get_path().c_str(), blueprint->project->get_path().c_str(),
blueprint->get_id(), blueprint->id,
properties_out_size properties_out_size
); );
auto * ctx = blueprint->get_project()->get_function_context(); auto * ctx = blueprint->project->get_function_context();
IkarusId ids[properties_out_size]; IkarusId ids[properties_out_size];
TRYRV( TRYRV(
, ,
blueprint->get_project() blueprint->project
->get_db() ->get_db()
->query_many_buffered<IkarusId>( ->query_many_buffered<IkarusId>(
"SELECT `id` FROM `properties` WHERE `source` = ?", "SELECT `id` FROM `properties` WHERE `source` = ?",
static_cast<IkarusId *>(ids), static_cast<IkarusId *>(ids),
properties_out_size, properties_out_size,
blueprint->get_id() blueprint->id
) )
.on_error([ctx](auto const& err) { .on_error([ctx](auto const& err) {
ctx->set_error( ctx->set_error(
@ -167,7 +90,7 @@ void ikarus_blueprint_get_properties(
VTRYRV( VTRYRV(
auto const type, auto const type,
, ,
IkarusProperty::get_property_type(blueprint->get_project(), id).on_error([ctx, id](auto const& err) { IkarusProperty::get_property_type(blueprint->project, id).on_error([ctx, id](auto const& err) {
ctx->set_error( ctx->set_error(
fmt::format("failed to fetch property {}'s type: {}", id, err), fmt::format("failed to fetch property {}'s type: {}", id, err),
true, true,
@ -178,25 +101,101 @@ void ikarus_blueprint_get_properties(
); );
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) /// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
properties_out[i] = blueprint->get_project()->get_property(id, type); properties_out[i] = blueprint->project->get_property(id, type);
} }
LOG_VERBOSE("successfully fetched blueprint properties"); LOG_VERBOSE("successfully fetched blueprint properties");
} }
size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint) { size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
LOG_VERBOSE("fetching blueprint linked entity count"); LOG_VERBOSE("fetching blueprint property count");
LOG_DEBUG("project={};blueprint={}", blueprint->get_project()->get_path().c_str(), blueprint->get_id()); auto * ctx = blueprint->project->get_function_context();
auto * ctx = blueprint->get_project()->get_function_context(); LOG_DEBUG("blueprint={}", blueprint->id);
VTRYRV( VTRYRV(
auto count, auto count,
0, 0,
blueprint->get_project() blueprint->project
->get_db() ->get_db()
->query_one<int>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?;", blueprint->get_id()) ->query_one<int>("SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint` = ?;", blueprint->id)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint property count: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint property count: {}", count);
LOG_VERBOSE("successfully fetched blueprint property count");
return static_cast<size_t>(count);
}
void ikarus_blueprint_get_linked_entities(
IkarusBlueprint const * blueprint, struct IkarusEntity ** entities_out, size_t entities_out_size
) {
LOG_VERBOSE("fetching blueprint linked entities");
LOG_DEBUG(
"project={}; blueprint={}; entities_out_size={}",
blueprint->project->get_path().c_str(),
blueprint->id,
entities_out_size
);
auto * ctx = blueprint->project->get_function_context();
IkarusId ids[entities_out_size];
TRYRV(
,
blueprint->project
->get_db()
->query_many_buffered<IkarusId>(
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
static_cast<IkarusId *>(ids),
entities_out_size,
blueprint->id
)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint linked entity ids: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint linked entities: [{}]", fmt::join(ids, ids + entities_out_size, ", "));
for (size_t i = 0; i < entities_out_size; ++i) {
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
entities_out[i] = blueprint->project->get_entity(ids[i]);
}
LOG_VERBOSE("successfully fetched blueprint linked entities");
}
size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint) {
LOG_VERBOSE("fetching blueprint linked entity count");
LOG_DEBUG("project={}; blueprint={}", blueprint->project->get_path().c_str(), blueprint->id);
auto * ctx = blueprint->project->get_function_context();
VTRYRV(
auto count,
0,
blueprint->project
->get_db()
->query_one<int>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?;", blueprint->id)
.on_error([ctx](auto const& err) { .on_error([ctx](auto const& err) {
ctx->set_error( ctx->set_error(
fmt::format("failed to fetch blueprint linked entity count: {}", err), fmt::format("failed to fetch blueprint linked entity count: {}", err),
@ -214,52 +213,6 @@ size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprin
return static_cast<size_t>(count); return static_cast<size_t>(count);
} }
void ikarus_blueprint_get_linked_entities(
IkarusBlueprint const * blueprint, struct IkarusEntity ** entities_out, size_t entities_out_size
) {
LOG_VERBOSE("fetching blueprint linked entities");
LOG_DEBUG(
"project={};blueprint={};entities_out_size={}",
blueprint->get_project()->get_path().c_str(),
blueprint->get_id(),
entities_out_size
);
auto * ctx = blueprint->get_project()->get_function_context();
IkarusId ids[entities_out_size];
TRYRV(
,
blueprint->get_project()
->get_db()
->query_many_buffered<IkarusId>(
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
static_cast<IkarusId *>(ids),
entities_out_size,
blueprint->get_id()
)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint linked entity ids: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint linked entities: [{}]", fmt::join(ids, ids + entities_out_size, ", "));
for (size_t i = 0; i < entities_out_size; ++i) {
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
entities_out[i] = blueprint->get_project()->get_entity(ids[i]);
}
LOG_VERBOSE("successfully fetched blueprint linked entities");
}
IkarusObject * ikarus_blueprint_to_object(IkarusBlueprint * blueprint) { IkarusObject * ikarus_blueprint_to_object(IkarusBlueprint * blueprint) {
return static_cast<IkarusObject *>(blueprint); return static_cast<IkarusObject *>(blueprint);
} }

View file

@ -0,0 +1,21 @@
#include "entity.hpp"
#include <cppbase/strings.hpp>
#include <persistence/function_context.hpp>
#include <persistence/project.hpp>
IkarusEntity * ikarus_entity_create(struct IkarusProject * project, char const * name) {
LOG_INFO("creating new entity");
LOG_DEBUG("project={}; name={}", project->get_path().c_str(), name);
auto * ctx = project->get_function_context();
if (cppbase::is_empty_or_blank(name)) {
ctx->set_error("name is empty or blank", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
// TODO
}

View file

@ -1,17 +1,5 @@
#include "object.hpp" #include "object.hpp"
IkarusObject::IkarusObject(IkarusProject * project, IkarusId id): IkarusObject::IkarusObject(IkarusProject * project, IkarusId id):
_project{project}, project{project},
_id{id} {} id{id} {}
IkarusProject * IkarusObject::get_project() {
return _project;
}
IkarusProject * IkarusObject::get_project() const {
return _project;
}
IkarusId IkarusObject::get_id() const {
return _id;
}

View file

@ -1,5 +1,9 @@
#pragma once #pragma once
#include <cppbase/result.hpp>
#include <sqlitecpp/errors.hpp>
#include <ikarus/id.h> #include <ikarus/id.h>
struct IkarusObject { struct IkarusObject {
@ -15,13 +19,6 @@ public:
virtual ~IkarusObject() = default; virtual ~IkarusObject() = default;
public: public:
[[nodiscard]] struct IkarusProject * get_project(); struct IkarusProject * project;
IkarusId id;
[[nodiscard]] struct IkarusProject * get_project() const;
[[nodiscard]] IkarusId get_id() const;
private:
struct IkarusProject mutable * _project;
IkarusId _id;
}; };

View file

@ -0,0 +1,68 @@
#pragma once
#include <concepts>
#include <cppbase/result.hpp>
#include <ikarus/id.h>
#include <ikarus/objects/object_type.h>
#include <persistence/function_context.hpp>
template<std::invocable<sqlitecpp::Connection *, IkarusId> F>
[[nodiscard]] cppbase::Result<IkarusId, sqlitecpp::TransactionError> insert_object(
IkarusProject * project, FunctionContext * ctx, IkarusObjectType type, std::string_view name, F insert_function
) {
char const * object_type_str = ikarus_object_type_to_string(type);
VTRY(
auto const id,
project->get_db()
->transact([&](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
LOG_VERBOSE("creating {} in objects table", object_type_str);
TRY(db->execute("INSERT INTO `objects` (`object_type`, `name`) VALUES(?, ?);", static_cast<int>(type), name));
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Blueprint);
LOG_DEBUG("{} is {}", object_type_str, id);
TRY(insert_function(db, id));
return cppbase::ok(id);
})
.on_error([&](auto const& err) {
ctx->set_error(
fmt::format("unable to insert {} into database: {}", object_type_str, err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_VERBOSE("successfully created blueprint");
return cppbase::ok(id);
}
template<typename T>
void delete_object(IkarusProject * project, FunctionContext * ctx, T * object) {
auto id = object->id;
auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(id));
TRYRV(, project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", id).on_error([&](auto const& err) {
ctx->set_error(
fmt::format("failed to delete {} from objects table: {}", object_type_str, err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
}));
LOG_VERBOSE("{} was successfully deleted from database, freeing", object_type_str);
project->uncache(object);
LOG_VERBOSE("successfully deleted {}", object_type_str);
}

View file

@ -0,0 +1,12 @@
#include "ikarus/objects/object_type.h"
char const * ikarus_object_type_to_string(IkarusObjectType type) {
switch (type) {
case IkarusObjectType_None: return "none";
case IkarusObjectType_Entity: return "entity";
case IkarusObjectType_Blueprint: return "blueprint";
case IkarusObjectType_Property: return "property";
}
return "unknown";
}

View file

@ -51,15 +51,15 @@ cppbase::Result<IkarusPropertyType, sqlitecpp::SingleQueryError> IkarusProperty:
IKA_API void ikarus_property_delete(IkarusProperty * property) { IKA_API void ikarus_property_delete(IkarusProperty * property) {
LOG_INFO("deleting property"); LOG_INFO("deleting property");
LOG_VERBOSE("project={};property={}", property->get_project()->get_path().c_str(), property->get_id()); LOG_VERBOSE("project={};property={}", property->project->get_path().c_str(), property->id);
auto * ctx = property->get_project()->get_function_context(); auto * ctx = property->project->get_function_context();
TRYRV( TRYRV(
, ,
property->get_project() property->project
->get_db() ->get_db()
->execute("DELETE FROM `objects` WHERE `id` = ?", property->get_id()) ->execute("DELETE FROM `objects` WHERE `id` = ?", property->id)
.on_error([ctx](auto const& err) { .on_error([ctx](auto const& err) {
ctx->set_error( ctx->set_error(
fmt::format("failed to delete property from objects table: {}", err), fmt::format("failed to delete property from objects table: {}", err),
@ -72,7 +72,7 @@ IKA_API void ikarus_property_delete(IkarusProperty * property) {
LOG_VERBOSE("property was successfully deleted from database, freeing"); LOG_VERBOSE("property was successfully deleted from database, freeing");
property->get_project()->uncache_property(property); property->project->uncache(property);
LOG_VERBOSE("successfully deleted property"); LOG_VERBOSE("successfully deleted property");
} }
@ -80,23 +80,23 @@ IKA_API void ikarus_property_delete(IkarusProperty * property) {
IkarusPropertyType ikarus_property_get_type(IkarusProperty const * property) { IkarusPropertyType ikarus_property_get_type(IkarusProperty const * property) {
LOG_VERBOSE("fetching property type"); LOG_VERBOSE("fetching property type");
return IkarusProperty::get_property_type(property->get_project(), property->get_id()) return IkarusProperty::get_property_type(property->project, property->id)
.unwrap_value_or(IkarusPropertyType_Toggle); .unwrap_value_or(IkarusPropertyType_Toggle);
} }
IkarusPropertySource const * ikarus_property_get_source(IkarusProperty const * property) { IkarusPropertySource const * ikarus_property_get_source(IkarusProperty const * property) {
LOG_VERBOSE("fetching property source"); LOG_VERBOSE("fetching property source");
LOG_VERBOSE("project={};property={}", property->get_project()->get_path().c_str(), property->get_id()); LOG_VERBOSE("project={};property={}", property->project->get_path().c_str(), property->id);
auto * ctx = property->get_project()->get_function_context(); auto * ctx = property->project->get_function_context();
VTRYRV( VTRYRV(
auto const source, auto const source,
nullptr, nullptr,
property->get_project() property->project
->get_db() ->get_db()
->query_one<int>("SELECT `source` FROM `properties` WHERE `id` = ?", property->get_id()) ->query_one<int>("SELECT `source` FROM `properties` WHERE `id` = ?", property->id)
.on_error([ctx](auto const& err) { .on_error([ctx](auto const& err) {
ctx->set_error( ctx->set_error(
fmt::format("failed to fetch property's source: {}", err), fmt::format("failed to fetch property's source: {}", err),
@ -108,8 +108,8 @@ IkarusPropertySource const * ikarus_property_get_source(IkarusProperty const * p
); );
switch (ikarus_id_get_object_type(source)) { switch (ikarus_id_get_object_type(source)) {
case IkarusObjectType_Blueprint: return new IkarusPropertySource{property->get_project()->get_blueprint(source)}; case IkarusObjectType_Blueprint: return new IkarusPropertySource{property->project->get_blueprint(source)};
case IkarusObjectType_Entity: return new IkarusPropertySource{property->get_project()->get_entity(source)}; case IkarusObjectType_Entity: return new IkarusPropertySource{property->project->get_entity(source)};
default: { default: {
ctx->set_error( ctx->set_error(
fmt::format("PropertySource is neither blueprint nor entity"), fmt::format("PropertySource is neither blueprint nor entity"),
@ -126,16 +126,16 @@ IkarusPropertySource const * ikarus_property_get_source(IkarusProperty const * p
IkarusValue * ikarus_property_get_default_value(IkarusProperty const * property) { IkarusValue * ikarus_property_get_default_value(IkarusProperty const * property) {
LOG_VERBOSE("fetching property default value"); LOG_VERBOSE("fetching property default value");
LOG_VERBOSE("project={};property={}", property->get_project()->get_path().c_str(), property->get_id()); LOG_VERBOSE("project={};property={}", property->project->get_path().c_str(), property->id);
auto * ctx = property->get_project()->get_function_context(); auto * ctx = property->project->get_function_context();
VTRYRV( VTRYRV(
auto const value, auto const value,
nullptr, nullptr,
property->get_project() property->project
->get_db() ->get_db()
->query_one<int>("SELECT `default_value` FROM `properties` WHERE `id` = ?", property->get_id()) ->query_one<int>("SELECT `default_value` FROM `properties` WHERE `id` = ?", property->id)
.on_error([ctx](auto const& err) { .on_error([ctx](auto const& err) {
ctx->set_error( ctx->set_error(
fmt::format("failed to fetch property's default value: {}", err), fmt::format("failed to fetch property's default value: {}", err),

View file

@ -2,7 +2,10 @@
#include "ikarus/persistence/project.h" #include "ikarus/persistence/project.h"
#include <objects/blueprint.hpp>
#include <objects/entity.hpp>
#include <objects/properties/number_property.hpp> #include <objects/properties/number_property.hpp>
#include <objects/properties/property.hpp>
#include <objects/properties/text_property.hpp> #include <objects/properties/text_property.hpp>
#include <objects/properties/toggle_property.hpp> #include <objects/properties/toggle_property.hpp>
#include <persistence/function_context.hpp> #include <persistence/function_context.hpp>
@ -31,7 +34,7 @@ IkarusBlueprint * IkarusProject::get_blueprint(IkarusId id) {
return get_cached_object<IkarusBlueprint>(id, this->_blueprints); return get_cached_object<IkarusBlueprint>(id, this->_blueprints);
} }
auto IkarusProject::uncache_blueprint(IkarusBlueprint * blueprint) -> void { auto IkarusProject::uncache(IkarusBlueprint * blueprint) -> void {
remove_cached_object(blueprint, _blueprints); remove_cached_object(blueprint, _blueprints);
} }
@ -39,7 +42,7 @@ auto IkarusProject::get_entity(IkarusId id) -> IkarusEntity * {
return get_cached_object<IkarusEntity>(id, this->_entities); return get_cached_object<IkarusEntity>(id, this->_entities);
} }
auto IkarusProject::uncache_entity(IkarusEntity * entity) -> void { auto IkarusProject::uncache(IkarusEntity * entity) -> void {
remove_cached_object(entity, _entities); remove_cached_object(entity, _entities);
} }
@ -60,6 +63,6 @@ auto IkarusProject::get_property(IkarusId id, IkarusPropertyType type) -> Ikarus
return iter->second.get(); return iter->second.get();
} }
auto IkarusProject::uncache_property(IkarusProperty * property) -> void { auto IkarusProject::uncache(IkarusProperty * property) -> void {
remove_cached_object(property, _properties); remove_cached_object(property, _properties);
} }

View file

@ -11,10 +11,6 @@
#include <ikarus/id.h> #include <ikarus/id.h>
#include <ikarus/objects/properties/property_type.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; constexpr inline auto MAXIMUM_ERROR_INFOS = 8;
/// \private /// \private
@ -32,13 +28,13 @@ public:
public: public:
[[nodiscard]] auto get_blueprint(IkarusId id) -> struct IkarusBlueprint *; [[nodiscard]] auto get_blueprint(IkarusId id) -> struct IkarusBlueprint *;
auto uncache_blueprint(struct IkarusBlueprint * blueprint) -> void; auto uncache(struct IkarusBlueprint * blueprint) -> void;
[[nodiscard]] auto get_entity(IkarusId id) -> struct IkarusEntity *; [[nodiscard]] auto get_entity(IkarusId id) -> struct IkarusEntity *;
auto uncache_entity(struct IkarusEntity * entity) -> void; auto uncache(struct IkarusEntity * entity) -> void;
[[nodiscard]] auto get_property(IkarusId id, IkarusPropertyType type) -> struct IkarusProperty *; [[nodiscard]] auto get_property(IkarusId id, IkarusPropertyType type) -> struct IkarusProperty *;
auto uncache_property(struct IkarusProperty * property) -> void; auto uncache(struct IkarusProperty * property) -> void;
private: private:
template<typename T> template<typename T>
@ -54,7 +50,7 @@ private:
template<typename T> template<typename T>
void remove_cached_object(T * object, std::unordered_map<IkarusId, std::unique_ptr<T>>& cache) { void remove_cached_object(T * object, std::unordered_map<IkarusId, std::unique_ptr<T>>& cache) {
cache.erase(object->get_id()); cache.erase(object->id);
} }
private: private: