add default values as param to property_create funcs

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
Folling 2024-01-15 03:37:26 +01:00 committed by Folling
parent 8f82b0e3da
commit 70fca82425
Signed by: folling
SSH key fingerprint: SHA256:S9qEx5WCFFLK49tE/LKnKuJYM5sw+++Dn6qJbbyxnCY
8 changed files with 24 additions and 8 deletions

View file

@ -24,12 +24,15 @@ struct IkarusNumberProperty;
/// \param property_source The property source to create the property for. /// \param property_source The property source to create the property for.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \pre \li Must exist. /// \pre \li Must exist.
/// \param default_value The default value for the property.
/// \pre \li Must not be null.
/// \param error_out \see errors.h /// \param error_out \see errors.h
/// \return The created property or null if an error occurs. /// \return The created property or null if an error occurs.
IKA_API IkarusNumberProperty * ikarus_number_property_create( IKA_API IkarusNumberProperty * ikarus_number_property_create(
struct IkarusProject * project, struct IkarusProject * project,
char const * name, char const * name,
struct IkarusPropertySource * property_source, struct IkarusPropertySource * property_source,
struct IkarusNumberValue * default_value,
IkarusErrorData * error_out IkarusErrorData * error_out
); );

View file

@ -24,12 +24,15 @@ struct IkarusTextProperty;
/// \param property_source The property source to create the property for. /// \param property_source The property source to create the property for.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \pre \li Must exist. /// \pre \li Must exist.
/// \param default_value The default value for the property.
/// \pre \li Must not be null.
/// \param error_out \see errors.h /// \param error_out \see errors.h
/// \return The created property or null if an error occurs. /// \return The created property or null if an error occurs.
IKA_API IkarusTextProperty * ikarus_text_property_create( IKA_API IkarusTextProperty * ikarus_text_property_create(
struct IkarusProject * project, struct IkarusProject * project,
char const * name, char const * name,
struct IkarusPropertySource * property_source, struct IkarusPropertySource * property_source,
struct IkarusTextValue* default_value,
IkarusErrorData * error_out IkarusErrorData * error_out
); );

View file

@ -24,12 +24,15 @@ struct IkarusToggleProperty;
/// \param property_source The property source to create the property for. /// \param property_source The property source to create the property for.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \pre \li Must exist. /// \pre \li Must exist.
/// \param default_value The default value for the property.
/// \pre \li Must not be null.
/// \param error_out \see errors.h /// \param error_out \see errors.h
/// \return The created property or null if an error occurs. /// \return The created property or null if an error occurs.
IKA_API IkarusToggleProperty * ikarus_toggle_property_create( IKA_API IkarusToggleProperty * ikarus_toggle_property_create(
struct IkarusProject * project, struct IkarusProject * project,
char const * name, char const * name,
struct IkarusPropertySource * property_source, struct IkarusPropertySource * property_source,
struct IkarusToggleValue * default_value,
IkarusErrorData * error_out IkarusErrorData * error_out
); );

View file

@ -1,9 +1,10 @@
#include "number_property.hpp" #include "ikarus/objects/properties/number_property.h"
#include <cppbase/result.hpp> #include <cppbase/result.hpp>
#include <ikarus/objects/properties/property.h> #include <ikarus/objects/properties/property.h>
#include <objects/properties/number_property.hpp>
#include <objects/properties/property_source.hpp> #include <objects/properties/property_source.hpp>
#include <objects/properties/util.hpp> #include <objects/properties/util.hpp>
#include <values/value.hpp> #include <values/value.hpp>
@ -15,9 +16,10 @@ IkarusNumberProperty * ikarus_number_property_create(
struct IkarusProject * project, struct IkarusProject * project,
char const * name, char const * name,
struct IkarusPropertySource * property_source, struct IkarusPropertySource * property_source,
struct IkarusNumberValue * default_value,
IkarusErrorData * error_out IkarusErrorData * error_out
) { ) {
return ikarus::util::create_property<IkarusNumberProperty>(project, name, property_source, error_out); return ikarus::util::create_property<IkarusNumberProperty>(project, name, property_source, default_value, error_out);
} }
IkarusNumberValue * ikarus_number_property_get_default_value(IkarusNumberProperty * property, IkarusErrorData * error_out) { IkarusNumberValue * ikarus_number_property_get_default_value(IkarusNumberProperty * property, IkarusErrorData * error_out) {

View file

@ -15,9 +15,10 @@ IkarusTextProperty * ikarus_text_property_create(
struct IkarusProject * project, struct IkarusProject * project,
char const * name, char const * name,
struct IkarusPropertySource * property_source, struct IkarusPropertySource * property_source,
struct IkarusTextValue * default_value,
IkarusErrorData * error_out IkarusErrorData * error_out
) { ) {
return ikarus::util::create_property<IkarusTextProperty>(project, name, property_source, error_out); return ikarus::util::create_property<IkarusTextProperty>(project, name, property_source, default_value, error_out);
} }
IkarusTextValue * ikarus_text_property_get_default_value(IkarusTextProperty * property, IkarusErrorData * error_out) { IkarusTextValue * ikarus_text_property_get_default_value(IkarusTextProperty * property, IkarusErrorData * error_out) {

View file

@ -15,9 +15,10 @@ IkarusToggleProperty * ikarus_toggle_property_create(
struct IkarusProject * project, struct IkarusProject * project,
char const * name, char const * name,
struct IkarusPropertySource * property_source, struct IkarusPropertySource * property_source,
struct IkarusToggleValue * default_value,
IkarusErrorData * error_out IkarusErrorData * error_out
) { ) {
return ikarus::util::create_property<IkarusToggleProperty>(project, name, property_source, error_out); return ikarus::util::create_property<IkarusToggleProperty>(project, name, property_source, default_value, error_out);
} }
IkarusToggleValue * ikarus_toggle_property_get_default_value(struct IkarusToggleProperty * property, IkarusErrorData * error_out) { IkarusToggleValue * ikarus_toggle_property_get_default_value(struct IkarusToggleProperty * property, IkarusErrorData * error_out) {

View file

@ -18,6 +18,7 @@ T * create_property(
struct IkarusProject * project, struct IkarusProject * project,
char const * name, char const * name,
struct IkarusPropertySource * property_source, struct IkarusPropertySource * property_source,
typename T::value_type * default_value,
IkarusErrorData * error_out IkarusErrorData * error_out
) { ) {
IKARUS_FAIL_IF_NULL(project, nullptr); IKARUS_FAIL_IF_NULL(project, nullptr);
@ -29,20 +30,22 @@ T * create_property(
fmt::format("{} name cannot be empty or blank", boost::typeindex::type_id<T>().pretty_name()), fmt::format("{} name cannot be empty or blank", boost::typeindex::type_id<T>().pretty_name()),
IkarusErrorInfo_Client_InvalidInput IkarusErrorInfo_Client_InvalidInput
) )
IKARUS_FAIL_IF_NULL(default_value, nullptr);
IKARUS_VTRYRV_OR_FAIL( IKARUS_VTRYRV_OR_FAIL(
IkarusId const id, IkarusId const id,
nullptr, nullptr,
"failed to create property: {}", "failed to create property: {}",
IkarusErrorInfo_Database_QueryFailed, IkarusErrorInfo_Database_QueryFailed,
project->db->transact([name, property_source](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> { project->db->transact([name, property_source, default_value](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
TRY(db->execute("INSERT INTO `objects`(`type`, `name`, `information`) VALUES(?, ?, ?)", IkarusObjectType_Property, name, "")); TRY(db->execute("INSERT INTO `objects`(`type`, `name`, `information`) VALUES(?, ?, ?)", IkarusObjectType_Property, name, ""));
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Property); auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Property);
TRY(db->execute( TRY(db->execute(
"INSERT INTO `properties`(`id`, `type`, `source`) VALUES(?, ?, ?)", "INSERT INTO `properties`(`id`, `type`, `source`, `default_value`) VALUES(?, ?, ?, ?)",
id, id,
T::PropertyType, T::PropertyType,
property_source->get_id() property_source->get_id(),
boost::json::serialize(default_value->to_json())
)); ));
return cppbase::ok(id); return cppbase::ok(id);
}) })

2
vendor/sqlitecpp vendored

@ -1 +1 @@
Subproject commit eadf3237bfa853763b332777e9dc0f16df8cca71 Subproject commit 77e726036b52fff6f82ff2d44081f05aee7408a8