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

@ -82,24 +82,24 @@ IKA_API struct IkarusPropertySource const * ikarus_property_get_source(IkarusPro
/// \param property The property to visit. /// \param property The property to visit.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \pre \li Must exist. /// \pre \li Must exist.
/// \param toggle_property The function to call if the property is a toggle property. Skipped if null. /// \param toggle_property_visitor The function to call if the property is a toggle property. Skipped if null.
/// \param number_property The function to call if the property is a number property. Skipped if null. /// \param number_property_visitor The function to call if the property is a number property. Skipped if null.
/// \param text_property The function to call if the property is a text property. Skipped if null. /// \param text_property_visitor The function to call if the property is a text property. Skipped if null.
/// \param data The data to pass to the functions. /// \param data The data to pass to the functions.
IKA_API void ikarus_property_visit( IKA_API void ikarus_property_visit(
IkarusProperty * property, IkarusProperty * property,
void (*toggle_property)(struct IkarusToggleProperty *, void *), void (*toggle_property_visitor)(struct IkarusToggleProperty *, void *),
void (*number_property)(struct IkarusNumberProperty *, void *), void (*number_property_visitor)(struct IkarusNumberProperty *, void *),
void (*text_property)(struct IkarusTextProperty *, void *), void (*text_property_visitor)(struct IkarusTextProperty *, void *),
void * data void * data
); );
/// \see ikarus_property_visit /// \see ikarus_property_visit
IKA_API void ikarus_property_visit_const( IKA_API void ikarus_property_visit_const(
IkarusProperty const * property, IkarusProperty const * property,
void (*toggle_property)(struct IkarusToggleProperty const *, void *), void (*toggle_property_visitor)(struct IkarusToggleProperty const *, void *),
void (*number_property)(struct IkarusNumberProperty const *, void *), void (*number_property_visitor)(struct IkarusNumberProperty const *, void *),
void (*text_property)(struct IkarusTextProperty const *, void *), void (*text_property_visitor)(struct IkarusTextProperty const *, void *),
void * data void * data
); );

View file

@ -4,13 +4,23 @@
#include <ikarus/objects/properties/property_type.h> #include <ikarus/objects/properties/property_type.h>
#include <objects/properties/property_source.hpp>
#include <persistence/function_context.hpp> #include <persistence/function_context.hpp>
#include <persistence/project.hpp> #include <persistence/project.hpp>
#include <sys/stat.h>
IkarusProperty::IkarusProperty(IkarusProject * project, IkarusId id, Data data): IkarusProperty::IkarusProperty(IkarusProject * project, IkarusId id, Data data):
IkarusObject{project, id}, IkarusObject{project, id},
_data{data} {} _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( cppbase::Result<IkarusPropertyType, sqlitecpp::SingleQueryError> IkarusProperty::get_property_type(
IkarusProject * project, IkarusId id IkarusProject * project, IkarusId id
) { ) {
@ -18,7 +28,21 @@ cppbase::Result<IkarusPropertyType, sqlitecpp::SingleQueryError> IkarusProperty:
LOG_VERBOSE("project={};property={}", project->get_path().c_str(), id); 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)); 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) { 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; ~IkarusProperty() override = default;
public:
[[nodiscard]] Data& get_data();
[[nodiscard]] Data const& get_data() const;
private: private:
Data _data; Data _data;
}; };