implement property (base) logic

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
folling 2023-11-27 13:37:42 +01:00 committed by Folling
parent f9de016dfe
commit 08ad2c5c66
Signed by: folling
SSH key fingerprint: SHA256:S9qEx5WCFFLK49tE/LKnKuJYM5sw+++Dn6qJbbyxnCY
3 changed files with 122 additions and 11 deletions

View file

@ -4,13 +4,23 @@
#include <ikarus/objects/properties/property_type.h>
#include <objects/properties/property_source.hpp>
#include <persistence/function_context.hpp>
#include <persistence/project.hpp>
#include <sys/stat.h>
IkarusProperty::IkarusProperty(IkarusProject * project, IkarusId id, Data data):
IkarusObject{project, id},
_data{data} {}
IkarusProperty::Data& IkarusProperty::get_data() {
return _data;
}
IkarusProperty::Data const& IkarusProperty::get_data() const {
return _data;
}
cppbase::Result<IkarusPropertyType, sqlitecpp::SingleQueryError> IkarusProperty::get_property_type(
IkarusProject * project, IkarusId id
) {
@ -18,7 +28,21 @@ cppbase::Result<IkarusPropertyType, sqlitecpp::SingleQueryError> IkarusProperty:
LOG_VERBOSE("project={};property={}", project->get_path().c_str(), id);
VTRY(auto const type, project->get_db()->query_one<int>("SELECT `type` FROM `objects` WHERE `id` = ?", id));
auto * ctx = project->get_function_context();
VTRY(
auto const type,
project->get_db()
->query_one<int>("SELECT `type` FROM `properties` WHERE `id` = ?", id)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch unboxed property type: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
return cppbase::ok(static_cast<IkarusPropertyType>(type));
}
@ -53,5 +77,88 @@ IKA_API void ikarus_property_delete(IkarusProperty * property) {
}
IkarusPropertyType ikarus_property_get_type(IkarusProperty const * property) {
LOG_DEBUG("fetching property type");
LOG_VERBOSE("fetching property type");
return IkarusProperty::get_property_type(property->get_project(), property->get_id())
.unwrap_value_or(IkarusPropertyType_Toggle);
}
IkarusPropertySource const * ikarus_property_get_source(IkarusProperty const * property) {
LOG_VERBOSE("fetching property source");
LOG_VERBOSE("project={};property={}", property->get_project()->get_path().c_str(), property->get_id());
auto * ctx = property->get_project()->get_function_context();
VTRY(
auto const source,
property->get_project()
->get_db()
->query_one<int>("SELECT `source` FROM `properties` WHERE `id` = ?", property->get_id())
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch property's source: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
switch (ikarus_id_get_object_type(source)) {
case IkarusObjectType_Blueprint: return new IkarusPropertySource{property->get_project()->get_blueprint(source)};
case IkarusObjectType_Entity: return new IkarusPropertySource{property->get_project()->get_entity(source)};
default: {
ctx->set_error(
fmt::format("PropertySource is neither blueprint nor entity"),
true,
IkarusErrorInfo_Source_LibIkarus,
IkarusErrorInfo_Type_LibIkarus_InvalidState
);
return nullptr;
}
}
}
void ikarus_property_visit(
IkarusProperty * property,
void (*toggle_property_visitor)(struct IkarusToggleProperty *, void *),
void (*number_property_visitor)(struct IkarusNumberProperty *, void *),
void (*text_property_visitor)(struct IkarusTextProperty *, void *),
void * data
) {
std::visit(
cppbase::overloaded{
[toggle_property_visitor, data](IkarusToggleProperty * property) { toggle_property_visitor(property, data); },
[number_property_visitor, data](IkarusNumberProperty * property) { number_property_visitor(property, data); },
[text_property_visitor, data](IkarusTextProperty * property) { text_property_visitor(property, data); }
},
property->get_data()
);
}
void ikarus_property_visit_const(
IkarusProperty const * property,
void (*toggle_property_visitor)(struct IkarusToggleProperty const *, void *),
void (*number_property_visitor)(struct IkarusNumberProperty const *, void *),
void (*text_property_visitor)(struct IkarusTextProperty const *, void *),
void * data
) {
std::visit(
cppbase::overloaded{
[toggle_property_visitor, data](IkarusToggleProperty const * property) { toggle_property_visitor(property, data); },
[number_property_visitor, data](IkarusNumberProperty const * property) { number_property_visitor(property, data); },
[text_property_visitor, data](IkarusTextProperty const * property) { text_property_visitor(property, data); }
},
property->get_data()
);
}
IkarusObject * ikarus_property_to_object(IkarusProperty * property) {
return static_cast<IkarusObject *>(property);
}
IkarusObject const * ikarus_property_to_object_const(IkarusProperty const * property) {
return static_cast<IkarusObject const *>(property);
}

View file

@ -31,6 +31,10 @@ public:
~IkarusProperty() override = default;
public:
[[nodiscard]] Data& get_data();
[[nodiscard]] Data const& get_data() const;
private:
Data _data;
};