diff --git a/include/ikarus/errors.h b/include/ikarus/errors.h index ce2f31d..9dc876d 100644 --- a/include/ikarus/errors.h +++ b/include/ikarus/errors.h @@ -57,7 +57,7 @@ enum IkarusErrorInfo { /// Example: A query takes longer than the timeout. IkarusErrorInfo_Type_LibIkarus_Timeout = 0x0002000300000003, /// \brief The type of error is unknown. - IkarusErrorInfo_Type_Unknown = 0xFFFFFFFF, + IkarusErrorInfo_Type_Unknown = 0xFFFFFFFFFFFFFFFF, }; /// \brief Gets the name of an error info. diff --git a/include/ikarus/global.h b/include/ikarus/global.h index 1572b24..bf9e5d5 100644 --- a/include/ikarus/global.h +++ b/include/ikarus/global.h @@ -5,7 +5,7 @@ #include -/// \addtogroup global Global +/// \defgroup global Global /// \brief Information relevant to the entire library. /// @{ diff --git a/src/id.hpp b/include/ikarus/id.h similarity index 100% rename from src/id.hpp rename to include/ikarus/id.h diff --git a/include/ikarus/objects/object.h b/include/ikarus/objects/object.h index a2cce6a..abaa580 100644 --- a/include/ikarus/objects/object.h +++ b/include/ikarus/objects/object.h @@ -8,12 +8,9 @@ /// \defgroup object Objects /// \brief Objects are a compound type of all types of objects in the database. /// \details The following objects currently exist: -/// - blueprints -/// - properties -/// - entities -/// - blueprint folders -/// - property folders -/// - entity folders +/// - \ref blueprint.h "Blueprints" +/// - \ref property.h "Properties" +/// - \ref entity.h "Entities" /// @{ IKARUS_BEGIN_HEADER @@ -21,6 +18,16 @@ IKARUS_BEGIN_HEADER /// \brief A generic object. Wraps all types of objects, including folders. struct IkarusObject; +/// \brief Compares two objects for equality. +/// \details This neither compares the pointers nor does a deep copy. Instead it figures out if the objects _are_ the same +/// object. +/// \param lhs The left hand side object. +/// \pre \li Must not be null. +/// \param rhs The right hand side object. +/// \pre \li Must not be null. +/// \return True if the objects are equal, false otherwise. +IKA_API bool ikarus_object_is_equal(IkarusObject const * lhs, IkarusObject const * rhs); + /// \brief Visits an object. Calling the appropriate function for the object's type. /// \param object The object to visit. /// \param blueprint_visitor The function to call if the object is a blueprint. Skipped if null. @@ -55,4 +62,4 @@ IKA_API void ikarus_object_visit_const( IKARUS_END_HEADER -// @} +/// @} diff --git a/include/ikarus/objects/properties/number_property.h b/include/ikarus/objects/properties/number_property.h index 9045140..7787cc6 100644 --- a/include/ikarus/objects/properties/number_property.h +++ b/include/ikarus/objects/properties/number_property.h @@ -14,10 +14,27 @@ IKARUS_BEGIN_HEADER struct IkarusNumberProperty; IKA_API IkarusNumberProperty * ikarus_number_property_create( - struct IkarusProject * project, - char const * name, - struct IkarusPropertySource * property_source, - struct IkarusNumberSettings * settings + struct IkarusProject * project, char const * name, struct IkarusPropertySource * property_source +); + +/// \brief Sets the default value for a number property. +/// \param property The number property. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \remark The settings take ownership of the value, the caller must not free it. +IKA_API struct IkarusNumberValue * ikarus_number_property_get_default_value(struct IkarusNumberProperty * property); + +/// \brief Sets the default value for a number property. +/// \param property The number property. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \param default_value The default value. +/// \pre \li Must not be null. +/// \pre \li Must be a valid value for the property. +/// \remark Please see \ref property.h "the property documentation" for more information on the interplay between default values +/// and other settings. +IKA_API void ikarus_number_property_set_default_value( + struct IkarusNumberProperty * property, struct IkarusNumberValue * default_value ); IKARUS_END_HEADER diff --git a/include/ikarus/objects/properties/property.h b/include/ikarus/objects/properties/property.h index f1ebf49..1da35e6 100644 --- a/include/ikarus/objects/properties/property.h +++ b/include/ikarus/objects/properties/property.h @@ -29,8 +29,12 @@ IKARUS_BEGIN_HEADER /// /// Every property has settings which can be used to customise the property further. /// Two settings that are shared among all properties are the following: -/// - Multiple -/// - Allow undefined +/// - List +/// - May be undefined +/// +/// Additionally, each property has a default value. If no default value is provided, a sensible default is chosen. +/// Setting a default value that isn't valid for the property is an error. Changing settings so that the current default value +/// becomes invalid is valid but unsets the custom default value. /// /// The former transforms a property into a list. Instead of one number, you could then specify a series of numbers. /// The latter allows you to specify an "unknown" value for a property. @@ -43,6 +47,7 @@ IKARUS_BEGIN_HEADER /// /// We call properties within entities "Entity Properties" and properties within blueprints "Blueprint Properties". /// +/// /// \remark Properties are scoped to the blueprint or entity they are associated with. /// \remark Values for properties are lazily created as space saving measure. /// Fetching the value for some property of some entity will return the property's default value if none is specified. @@ -65,17 +70,6 @@ IKA_API void ikarus_property_delete(IkarusProperty * property); /// \remark Must be freed using #ikarus_free. IKA_API IkarusPropertyType ikarus_property_get_type(IkarusProperty const * property); -/// \brief Gets the settings of a property. -/// \param property The property to get the settings of. -/// \pre \li Must not be null. -/// \pre \li Must exist. -/// \return The settings of the property or null if an error occurs. -/// \remark Must be freed using #ikarus_free. -IKA_API struct IkarusPropertySettings * ikarus_property_get_settings(IkarusProperty * property); - -/// \see ikarus_property_get_settings -IKA_API struct IkarusPropertySettings const * ikarus_property_get_settings_const(IkarusProperty const * property); - /// \brief Gets the source of a property. /// \param property The property to get the source of. /// \pre \li Must not be null. diff --git a/include/ikarus/objects/properties/property_type.h b/include/ikarus/objects/properties/property_type.h index 89e5fa1..ee6ec19 100644 --- a/include/ikarus/objects/properties/property_type.h +++ b/include/ikarus/objects/properties/property_type.h @@ -22,12 +22,6 @@ enum IkarusPropertyType { IkarusPropertyType_Text, }; -/// \brief Fetches the default value for a property type. -/// \remark Not to be confused with the default value of a property. See ikarus_property_get_default_value -/// \param type The property type. -/// \return The default value for the property type or null if an error occurs. -IKA_API struct IkarusValue * ikarus_property_type_get_default_default_value(IkarusPropertyType type); - IKARUS_END_HEADER /// @} diff --git a/include/ikarus/objects/properties/text_property.h b/include/ikarus/objects/properties/text_property.h index f7975b3..ad90bf6 100644 --- a/include/ikarus/objects/properties/text_property.h +++ b/include/ikarus/objects/properties/text_property.h @@ -14,10 +14,27 @@ IKARUS_BEGIN_HEADER struct IkarusTextProperty; IKA_API IkarusTextProperty * ikarus_text_property_create( - struct IkarusProject * project, - char const * name, - struct IkarusPropertySource * property_source, - struct IkarusTextSettings * settings + struct IkarusProject * project, char const * name, struct IkarusPropertySource * property_source +); + +/// \brief Sets the default value for a text property. +/// \param property The text property. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \remark The settings take ownership of the value, the caller must not free it. +IKA_API struct IkarusTextValue * ikarus_text_property_get_default_value(struct IkarusTextProperty * property); + +/// \brief Sets the default value for a text property. +/// \param property The text property. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \param default_value The default value. +/// \pre \li Must not be null. +/// \pre \li Must be a valid value for the property. +/// \remark Please see \ref property.h "the property documentation" for more information on the interplay between default values +/// and other settings. +IKA_API void ikarus_text_property_set_default_value( + struct IkarusTextProperty * property, struct IkarusTextValue * default_value ); IKARUS_END_HEADER diff --git a/include/ikarus/objects/properties/toggle_property.h b/include/ikarus/objects/properties/toggle_property.h index baa0801..9e8119d 100644 --- a/include/ikarus/objects/properties/toggle_property.h +++ b/include/ikarus/objects/properties/toggle_property.h @@ -14,10 +14,27 @@ IKARUS_BEGIN_HEADER struct IkarusToggleProperty; IKA_API IkarusToggleProperty * ikarus_toggle_property_create( - struct IkarusProject * project, - char const * name, - struct IkarusPropertySource * property_source, - struct IkarusToggleSettings * settings + struct IkarusProject * project, char const * name, struct IkarusPropertySource * property_source +); + +/// \brief Sets the default value for a toggle property. +/// \param property The toggle property. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \remark The settings take ownership of the value, the caller must not free it. +IKA_API struct IkarusToggleValue * ikarus_toggle_property_get_default_value(struct IkarusToggleProperty * property); + +/// \brief Sets the default value for a toggle property. +/// \param property The toggle property. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \param default_value The default value. +/// \pre \li Must not be null. +/// \pre \li Must be a valid value for the property. +/// \remark Please see \ref property.h "the property documentation" for more information on the interplay between default values +/// and other settings. +IKA_API void ikarus_toggle_property_set_default_value( + struct IkarusToggleProperty * property, struct IkarusToggleValue * default_value ); IKARUS_END_HEADER diff --git a/include/ikarus/values/number_value.h b/include/ikarus/values/number_value.h index a489cd6..d4b6446 100644 --- a/include/ikarus/values/number_value.h +++ b/include/ikarus/values/number_value.h @@ -5,7 +5,7 @@ #include -/// \addtogroup entity_value Entity Values +/// \addtogroup values Values /// @{ IKARUS_BEGIN_HEADER @@ -42,3 +42,5 @@ IKA_API void ikarus_number_value_set(IkarusNumberValue * value, bool new_value); IKA_API struct IkarusValue * ikarus_number_value_to_value(IkarusNumberValue * number_value); IKARUS_END_HEADER + +/// @} diff --git a/include/ikarus/values/text_value.h b/include/ikarus/values/text_value.h index b9ea9c9..8562ab5 100644 --- a/include/ikarus/values/text_value.h +++ b/include/ikarus/values/text_value.h @@ -5,7 +5,7 @@ #include -/// \addtogroup entity_value Entity Values +/// \addtogroup values Values /// @{ IKARUS_BEGIN_HEADER @@ -43,3 +43,5 @@ IKA_API void ikarus_text_value_set(IkarusTextValue * value, bool new_value); IKA_API struct IkarusValue * ikarus_text_value_to_value(IkarusTextValue * text_value); IKARUS_END_HEADER + +/// @} diff --git a/include/ikarus/values/toggle_value.h b/include/ikarus/values/toggle_value.h index e653482..fed97d2 100644 --- a/include/ikarus/values/toggle_value.h +++ b/include/ikarus/values/toggle_value.h @@ -5,7 +5,7 @@ #include -/// \addtogroup entity_value Entity Values +/// \addtogroup values Values /// @{ IKARUS_BEGIN_HEADER @@ -42,3 +42,5 @@ IKA_API void ikarus_toggle_value_set(IkarusToggleValue * value, bool new_value); IKA_API struct IkarusValue * ikarus_toggle_value_to_value(IkarusToggleValue * toggle_value); IKARUS_END_HEADER + +/// @} diff --git a/include/ikarus/values/value.h b/include/ikarus/values/value.h index b8b95fe..1cf1fd0 100644 --- a/include/ikarus/values/value.h +++ b/include/ikarus/values/value.h @@ -5,7 +5,7 @@ #include -/// \defgroup entity_value Entity Values +/// \defgroup values Values /// \brief The values stored in entities. /// \details Each entity has a value for each property it is associated with. /// The value is of the type specified by the property and constrained by the property's settings. diff --git a/src/errors.cpp b/src/errors.cpp index 19c3bf9..54681f2 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -2,21 +2,21 @@ char const * get_error_info_name(IkarusErrorInfo info) { switch (info) { - case IkarusErrorInfo_Source_None: return "IkarusErrorSource_None"; - case IkarusErrorInfo_Source_Client: return "IkarusErrorSource_Client"; - case IkarusErrorInfo_Source_SubSystem: return "IkarusErrorSource_SubSystem"; - case IkarusErrorInfo_Source_LibIkarus: return "IkarusErrorSource_LibIkarus"; - case IkarusErrorInfo_Source_Unknown: return "IkarusErrorSource_Unknown"; - case IkarusErrorInfo_Type_None: return "IkarusErrorType_None"; - case IkarusErrorInfo_Type_Client_Misuse: return "IkarusErrorType_Client_Misuse"; - case IkarusErrorInfo_Type_Client_Input: return "IkarusErrorType_Client_Input"; - case IkarusErrorInfo_Type_SubSystem_Dependency: return "IkarusErrorType_SubSystem_Dependency"; - case IkarusErrorInfo_Type_SubSystem_Database: return "IkarusErrorType_SubSystem_Database"; - case IkarusErrorInfo_Type_SubSystem_Filesystem: return "IkarusErrorType_SubSystem_Filesystem"; - case IkarusErrorInfo_Type_LibIkarus_InvalidState: return "IkarusErrorType_LibIkarus_InvalidState"; - case IkarusErrorInfo_Type_LibIkarus_CannotPerformOperation: return "IkarusErrorType_LibIkarus_CannotPerformOperation"; - case IkarusErrorInfo_Type_LibIkarus_Timeout: return "IkarusErrorType_LibIkarus_Timeout"; - case IkarusErrorInfo_Type_Unknown: return "IkarusErrorType_Unknown"; + case IkarusErrorInfo_Source_None: return "IkarusErrorInfo_Source_None"; + case IkarusErrorInfo_Source_Client: return "IkarusErrorInfo_Source_Client"; + case IkarusErrorInfo_Source_SubSystem: return "IkarusErrorInfo_Source_SubSystem"; + case IkarusErrorInfo_Source_LibIkarus: return "IkarusErrorInfo_Source_LibIkarus"; + case IkarusErrorInfo_Source_Unknown: return "IkarusErrorInfo_Source_Unknown"; + case IkarusErrorInfo_Type_None: return "IkarusErrorInfo_Type_None"; + case IkarusErrorInfo_Type_Client_Misuse: return "IkarusErrorInfo_Type_Client_Misuse"; + case IkarusErrorInfo_Type_Client_Input: return "IkarusErrorInfo_Type_Client_Input"; + case IkarusErrorInfo_Type_SubSystem_Dependency: return "IkarusErrorInfo_Type_SubSystem_Dependency"; + case IkarusErrorInfo_Type_SubSystem_Database: return "IkarusErrorInfo_Type_SubSystem_Database"; + case IkarusErrorInfo_Type_SubSystem_Filesystem: return "IkarusErrorInfo_Type_SubSystem_Filesystem"; + case IkarusErrorInfo_Type_LibIkarus_InvalidState: return "IkarusErrorInfo_Type_LibIkarus_InvalidState"; + case IkarusErrorInfo_Type_LibIkarus_CannotPerformOperation: return "IkarusErrorInfo_Type_LibIkarus_CannotPerformOperation"; + case IkarusErrorInfo_Type_LibIkarus_Timeout: return "IkarusErrorInfo_Type_LibIkarus_Timeout"; + case IkarusErrorInfo_Type_Unknown: return "IkarusErrorInfo_Type_Unknown"; default: return "Unknown"; } } diff --git a/src/id.cpp b/src/id.cpp index 7ec6491..463aa3d 100644 --- a/src/id.cpp +++ b/src/id.cpp @@ -1,32 +1,16 @@ -#include "id.hpp" +#include "ikarus/id.h" #include #include -uint64_t const IKARUS_ID_OBJECT_TYPE_BITS = 8; -uint64_t const IKARUS_ID_OBJECT_RANDOM_BITS = sizeof(IkarusId) - IKARUS_ID_OBJECT_TYPE_BITS; +constexpr uint64_t IKARUS_ID_OBJECT_TYPE_BITS = 8; +constexpr uint64_t IKARUS_ID_OBJECT_RANDOM_BITS = sizeof(IkarusId) - IKARUS_ID_OBJECT_TYPE_BITS; -auto from_data_and_type(int64_t data, IkarusObjectType type) -> IkarusId { +auto ikarus_id_from_data_and_type(int64_t data, IkarusObjectType type) -> IkarusId { return data | (static_cast(type) << IKARUS_ID_OBJECT_RANDOM_BITS); } auto ikarus_id_get_object_type(IkarusId id) -> IkarusObjectType { return static_cast(id >> IKARUS_ID_OBJECT_RANDOM_BITS); } - -TEST_CASE("id_object_type", "[id]") { - // NOLINTNEXTLINE(readability-magic-numbers) - auto id = static_cast(IkarusObjectType_Blueprint) << IKARUS_ID_OBJECT_RANDOM_BITS; - - REQUIRE(ikarus_id_get_object_type(id) == IkarusObjectType_Blueprint); -} - -TEST_CASE("id_equal", "[id]") { - auto id = static_cast(IkarusObjectType_Blueprint) << IKARUS_ID_OBJECT_RANDOM_BITS; - auto copy = id; - auto third = static_cast(IkarusObjectType_Property) << IKARUS_ID_OBJECT_RANDOM_BITS; - - REQUIRE(ikarus_id_is_equal(id, copy)); - REQUIRE(!ikarus_id_is_equal(id, third)); -} diff --git a/src/objects/blueprint.cpp b/src/objects/blueprint.cpp index d6a38b1..3971d9e 100644 --- a/src/objects/blueprint.cpp +++ b/src/objects/blueprint.cpp @@ -8,9 +8,12 @@ #include #include -#include +#include #include +IkarusBlueprint::IkarusBlueprint(IkarusProject * project, IkarusId id): + IkarusObject{project, id} {} + IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char const * name) { LOG_INFO("creating new blueprint"); @@ -19,7 +22,7 @@ IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char c return nullptr; } - auto * ctx = project->function_context(); + auto * ctx = project->get_function_context(); if (name == nullptr) { ctx->set_error("name is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input); @@ -31,12 +34,12 @@ IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char c return nullptr; } - LOG_DEBUG("project={}; name={}", project->path().c_str(), name); + LOG_DEBUG("project={}; name={}", project->get_path().c_str(), name); VTRYRV( - auto id, + auto const id, nullptr, - project->db() + project->get_db() ->transact([name](auto * db) -> cppbase::Result { LOG_VERBOSE("creating blueprint in objects table"); @@ -79,14 +82,15 @@ void ikarus_blueprint_delete(IkarusBlueprint * blueprint) { return; } - auto * ctx = blueprint->project->function_context(); + auto * ctx = blueprint->get_project()->get_function_context(); - LOG_DEBUG("blueprint={}", blueprint->id); + LOG_DEBUG("blueprint={}", blueprint->get_id()); TRYRV( , - blueprint->project->db() - ->execute("DELETE FROM `objects` WHERE `id` = ?", blueprint->id) + 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), @@ -99,7 +103,7 @@ void ikarus_blueprint_delete(IkarusBlueprint * blueprint) { LOG_VERBOSE("blueprint was successfully deleted from database, freeing blueprint"); - blueprint->project->remove_blueprint(blueprint); + blueprint->get_project()->uncache_blueprint(blueprint); LOG_VERBOSE("successfully deleted blueprint"); } @@ -112,15 +116,16 @@ size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) { return 0; } - auto * ctx = blueprint->project->function_context(); + auto * ctx = blueprint->get_project()->get_function_context(); - LOG_DEBUG("blueprint={}", blueprint->id); + LOG_DEBUG("blueprint={}", blueprint->get_id()); VTRYRV( auto count, 0, - blueprint->project->db() - ->query_one("SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint` = ?;", blueprint->id) + blueprint->get_project() + ->get_db() + ->query_one("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), @@ -148,25 +153,26 @@ void ikarus_blueprint_get_properties( return; } - auto * ctx = blueprint->project->function_context(); + auto * ctx = blueprint->get_project()->get_function_context(); if (properties_out == nullptr) { ctx->set_error("properties_out is null", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input); return; } - LOG_DEBUG("blueprint={}; properties_out_size={}", blueprint->id, properties_out_size); + LOG_DEBUG("blueprint={}; properties_out_size={}", blueprint->get_id(), properties_out_size); IkarusId ids[properties_out_size]; TRYRV( , - blueprint->project->db() + blueprint->get_project() + ->get_db() ->query_many_buffered( "SELECT `id` FROM `properties` WHERE `source` = ?", static_cast(ids), properties_out_size, - blueprint->id + blueprint->get_id() ) .on_error([ctx](auto const& err) { ctx->set_error( @@ -182,7 +188,7 @@ void ikarus_blueprint_get_properties( for (size_t i = 0; i < properties_out_size; ++i) { /// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - properties_out[i] = blueprint->project->get_property(ids[i]); + properties_out[i] = blueprint->get_project()->get_property(ids[i]); } LOG_VERBOSE("successfully fetched blueprint properties"); @@ -196,15 +202,16 @@ size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprin return 0; } - auto * ctx = blueprint->project->function_context(); + auto * ctx = blueprint->get_project()->get_function_context(); - LOG_DEBUG("blueprint={}", blueprint->id); + LOG_DEBUG("blueprint={}", blueprint->get_id()); VTRYRV( auto count, 0, - blueprint->project->db() - ->query_one("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?;", blueprint->id) + blueprint->get_project() + ->get_db() + ->query_one("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?;", blueprint->get_id()) .on_error([ctx](auto const& err) { ctx->set_error( fmt::format("failed to fetch blueprint linked entity count: {}", err), @@ -232,25 +239,26 @@ void ikarus_blueprint_get_linked_entities( return; } - auto * ctx = blueprint->project->function_context(); + auto * ctx = blueprint->get_project()->get_function_context(); if (entities_out == nullptr) { ctx->set_error("entities_out is null", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input); return; } - LOG_DEBUG("blueprint={}; entities_out_size={}", blueprint->id, entities_out_size); + LOG_DEBUG("blueprint={}; entities_out_size={}", blueprint->get_id(), entities_out_size); IkarusId ids[entities_out_size]; TRYRV( , - blueprint->project->db() + blueprint->get_project() + ->get_db() ->query_many_buffered( "SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?", static_cast(ids), entities_out_size, - blueprint->id + blueprint->get_id() ) .on_error([ctx](auto const& err) { ctx->set_error( @@ -266,7 +274,7 @@ void ikarus_blueprint_get_linked_entities( 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]); + entities_out[i] = blueprint->get_project()->get_entity(ids[i]); } LOG_VERBOSE("successfully fetched blueprint linked entities"); @@ -286,7 +294,7 @@ IkarusObject const * ikarus_blueprint_to_object_const(IkarusBlueprint const * bl // auto * ctx = blueprint->project->function_context(); - LOG_DEBUG("blueprint={}", blueprint->id); + LOG_DEBUG("blueprint={}", blueprint->get_id()); LOG_VERBOSE("successfully casted blueprint to object"); diff --git a/src/objects/blueprint.hpp b/src/objects/blueprint.hpp index f904074..445efdb 100644 --- a/src/objects/blueprint.hpp +++ b/src/objects/blueprint.hpp @@ -2,8 +2,14 @@ #include -/// \private -struct IkarusBlueprint : public IkarusObject { - inline IkarusBlueprint(struct IkarusProject * project, IkarusId id): - IkarusObject{project, id} {} +struct IkarusBlueprint final : IkarusObject { + inline IkarusBlueprint(struct IkarusProject * project, IkarusId id); + + IkarusBlueprint(IkarusBlueprint const&) = default; + IkarusBlueprint(IkarusBlueprint&&) = default; + + IkarusBlueprint& operator=(IkarusBlueprint const&) = default; + IkarusBlueprint& operator=(IkarusBlueprint&&) = default; + + ~IkarusBlueprint() override = default; }; diff --git a/src/objects/entity.cpp b/src/objects/entity.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/objects/entity.hpp b/src/objects/entity.hpp index 28fb068..fb78e39 100644 --- a/src/objects/entity.hpp +++ b/src/objects/entity.hpp @@ -2,8 +2,15 @@ #include -/// \private -struct IkarusEntity : public IkarusObject { +struct IkarusEntity final : IkarusObject { inline IkarusEntity(struct IkarusProject * project, IkarusId id): IkarusObject{project, id} {} + + IkarusEntity(IkarusEntity const&) = default; + IkarusEntity(IkarusEntity&&) = default; + + IkarusEntity& operator=(IkarusEntity const&) = default; + IkarusEntity& operator=(IkarusEntity&&) = default; + + ~IkarusEntity() override = default; }; diff --git a/src/objects/object.cpp b/src/objects/object.cpp new file mode 100644 index 0000000..72aba6e --- /dev/null +++ b/src/objects/object.cpp @@ -0,0 +1,13 @@ +#include "object.hpp" + +IkarusProject * IkarusObject::get_project() { + return _project; +} + +IkarusProject * IkarusObject::get_project() const { + return _project; +} + +IkarusId IkarusObject::get_id() const { + return _id; +} diff --git a/src/objects/object.hpp b/src/objects/object.hpp index e95c3e0..ee1b147 100644 --- a/src/objects/object.hpp +++ b/src/objects/object.hpp @@ -1,14 +1,27 @@ #pragma once -#include - -#include +#include struct IkarusObject { - struct IkarusProject * project; - IkarusId id; +public: + IkarusObject(struct IkarusProject * project, IkarusId id); - inline IkarusObject(struct IkarusProject * project, IkarusId id): - project{project}, - id{id} {} + IkarusObject(IkarusObject const&) = default; + IkarusObject(IkarusObject&&) = default; + + IkarusObject& operator=(IkarusObject const&) = default; + IkarusObject& operator=(IkarusObject&&) = default; + + virtual ~IkarusObject() = default; + +public: + [[nodiscard]] inline struct IkarusProject * get_project(); + + [[nodiscard]] inline struct IkarusProject * get_project() const; + + [[nodiscard]] inline IkarusId get_id() const; + +private: + struct IkarusProject mutable * _project; + IkarusId _id; }; diff --git a/src/objects/properties/number_property.cpp b/src/objects/properties/number_property.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/objects/properties/number_property.hpp b/src/objects/properties/number_property.hpp new file mode 100644 index 0000000..dd5f8a2 --- /dev/null +++ b/src/objects/properties/number_property.hpp @@ -0,0 +1,5 @@ +// +// Created by Jonathan Purol on 26.11.23. +// + +export module number_property.hpp; diff --git a/src/objects/properties/property.cpp b/src/objects/properties/property.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/objects/properties/property.hpp b/src/objects/properties/property.hpp new file mode 100644 index 0000000..1a74577 --- /dev/null +++ b/src/objects/properties/property.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +struct IkarusProperty : IkarusObject { + IkarusProperty(struct IkarusProject * project, IkarusId id); + + IkarusProperty(IkarusProperty const&) = default; + IkarusProperty(IkarusProperty&&) = default; + + IkarusProperty& operator=(IkarusProperty const&) = default; + IkarusProperty& operator=(IkarusProperty&&) = default; + + ~IkarusProperty() override = default; +}; diff --git a/src/objects/properties/property_source.cpp b/src/objects/properties/property_source.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/objects/properties/property_source.hpp b/src/objects/properties/property_source.hpp new file mode 100644 index 0000000..8da517a --- /dev/null +++ b/src/objects/properties/property_source.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include + +struct IkarusPropertySource { +public: + using Data = std::variant; + +public: + inline explicit IkarusPropertySource(Data data): + _data{data} {} + + IkarusPropertySource(IkarusPropertySource const&) = default; + IkarusPropertySource(IkarusPropertySource&&) = default; + + IkarusPropertySource& operator=(IkarusPropertySource const&) = default; + IkarusPropertySource& operator=(IkarusPropertySource&&) = default; + + ~IkarusPropertySource() = default; + +public: + [[nodiscard]] inline Data const& get_data() const { + return _data; + } + +private: + std::variant _data; +}; diff --git a/src/objects/properties/text_property.cpp b/src/objects/properties/text_property.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/objects/properties/text_property.hpp b/src/objects/properties/text_property.hpp new file mode 100644 index 0000000..3816f35 --- /dev/null +++ b/src/objects/properties/text_property.hpp @@ -0,0 +1,8 @@ +// +// Created by Jonathan Purol on 26.11.23. +// + +#ifndef TEXT_PROPERTY_HPP +#define TEXT_PROPERTY_HPP + +#endif //TEXT_PROPERTY_HPP diff --git a/src/objects/properties/toggle_property.cpp b/src/objects/properties/toggle_property.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/objects/properties/toggle_property.hpp b/src/objects/properties/toggle_property.hpp new file mode 100644 index 0000000..5514d8b --- /dev/null +++ b/src/objects/properties/toggle_property.hpp @@ -0,0 +1,7 @@ +#pragma once + +#include + +struct IkarusToggleProperty final : IkarusProperty { + IkarusToggleProperty(struct IkarusProject * project, IkarusId id); +}; diff --git a/src/objects/property.cpp b/src/objects/property.cpp deleted file mode 100644 index 80d9031..0000000 --- a/src/objects/property.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "property.hpp" - -#include -#include -#include - -#include - -#include -#include -#include -#include -#include diff --git a/src/objects/property.hpp b/src/objects/property.hpp deleted file mode 100644 index b59d7dd..0000000 --- a/src/objects/property.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -/// \private -struct IkarusProperty : public IkarusObject { - inline IkarusProperty(struct IkarusProject * project, IkarusId id): - IkarusObject{project, id} {} -}; diff --git a/src/objects/property_source.hpp b/src/objects/property_source.hpp deleted file mode 100644 index e2a2739..0000000 --- a/src/objects/property_source.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include - -#include - -/// \private -struct IkarusPropertySource { - std::variant data; -}; diff --git a/src/persistence/function_context.cpp b/src/persistence/function_context.cpp new file mode 100644 index 0000000..6e252dc --- /dev/null +++ b/src/persistence/function_context.cpp @@ -0,0 +1,18 @@ +#include "function_context.hpp" + +FunctionContext::FunctionContext(IkarusProject * project): + _project{project} {} + +FunctionContext::~FunctionContext() { + if (_project->_function_contexts.size() == 1) { + if (_project->error_message_buffer.empty()) { + _project->error_message_buffer.push_back('\0'); + } else { + _project->error_message_buffer[0] = '\0'; + } + + _project->error_infos = {}; + } + + _project->_function_contexts.pop(); +} diff --git a/src/persistence/function_context.hpp b/src/persistence/function_context.hpp new file mode 100644 index 0000000..3dc64b3 --- /dev/null +++ b/src/persistence/function_context.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include +#include +#include + +#include + +#include + +#include + +#include + +struct FunctionContext { +public: + explicit FunctionContext(struct IkarusProject * project); + + FunctionContext(FunctionContext const&) noexcept = default; + FunctionContext(FunctionContext&&) noexcept = default; + + auto operator=(FunctionContext const&) noexcept -> FunctionContext& = default; + auto operator=(FunctionContext&&) noexcept -> FunctionContext& = default; + + ~FunctionContext(); + +public: + template + requires(std::is_same_v && ...) && (sizeof...(Infos) <= MAXIMUM_ERROR_INFOS) + auto set_error(std::string_view error_message, bool log_error, Infos... infos) -> void { + if (error_message.size() > _project->error_message_buffer.size()) { + _project->error_message_buffer.resize(error_message.size() + 1); + } + + for (int i = 0; i < error_message.size(); ++i) { + _project->error_message_buffer[i] = error_message[i]; + } + + _project->error_message_buffer[error_message.size()] = '\0'; + _project->error_infos = {infos...}; + + if (log_error) { + LOG_ERROR( + "Error({}): {}", + fmt::join(_project->error_infos | std::views::transform(get_error_info_name), ", "), + error_message + ); + } + } + +private: + struct IkarusProject * _project; +}; diff --git a/src/persistence/project.cpp b/src/persistence/project.cpp new file mode 100644 index 0000000..3ceb821 --- /dev/null +++ b/src/persistence/project.cpp @@ -0,0 +1,49 @@ +#include "project.hpp" + +#include "ikarus/persistence/project.h" + +#include + +auto IkarusProject::get_name() const -> std::string_view { + return _name; +} + +auto IkarusProject::get_path() const -> std::filesystem::path const& { + return _path; +} + +auto IkarusProject::get_db() -> sqlitecpp::Connection * { + return _db.get(); +} + +auto IkarusProject::get_db() const -> sqlitecpp::Connection const * { + return _db.get(); +} + +auto IkarusProject::get_function_context() -> FunctionContext * { + return &_function_contexts.emplace(this); +} + +IkarusBlueprint * IkarusProject::get_blueprint(IkarusId id) { + return get_cached_object(id, this->_blueprints); +} + +auto IkarusProject::uncache_blueprint(IkarusBlueprint * blueprint) -> void { + remove_cached_object(blueprint, _blueprints); +} + +auto IkarusProject::get_entity(IkarusId id) -> IkarusEntity * { + return get_cached_object(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::uncache_property(IkarusProperty * property) -> void { + remove_cached_object(property, _properties); +} diff --git a/src/persistence/project.hpp b/src/persistence/project.hpp index fbb84f2..ef69a81 100644 --- a/src/persistence/project.hpp +++ b/src/persistence/project.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include @@ -9,83 +8,45 @@ #include #include +#include -constexpr inline size_t MAXIMUM_ERROR_INFOS = 8; -constexpr inline size_t MAXIMUM_ERROR_MESSAGE_LENGTH = 256; - -/// \private -class FunctionContext { -public: - explicit FunctionContext(struct IkarusProject * project); - FunctionContext(FunctionContext const&) noexcept = default; - FunctionContext(FunctionContext&&) noexcept = default; - - auto operator=(FunctionContext const&) noexcept -> FunctionContext& = default; - auto operator=(FunctionContext&&) noexcept -> FunctionContext& = default; - - ~FunctionContext(); - - template - requires(std::is_same_v && ...) && (sizeof...(Infos) <= MAXIMUM_ERROR_INFOS) - auto set_error(std::string_view error_message, bool log_error, Infos... infos) -> void; - -private: - struct IkarusProject * _project; -}; +constexpr inline auto MAXIMUM_ERROR_INFOS = 8; /// \private struct IkarusProject { public: - [[nodiscard]] inline auto name() const -> std::string_view { - return _name; - } + [[nodiscard]] auto get_name() const -> std::string_view; - [[nodiscard]] inline auto path() const -> std::filesystem::path const& { - return _path; - } + [[nodiscard]] auto get_path() const -> std::filesystem::path const&; - [[nodiscard]] inline auto db() -> sqlitecpp::Connection * { - return _db.get(); - } + [[nodiscard]] auto get_db() -> sqlitecpp::Connection *; + [[nodiscard]] auto get_db() const -> sqlitecpp::Connection const *; - inline auto function_context() -> FunctionContext * { - return &_function_contexts.emplace(this); - } +public: + [[nodiscard]] auto get_function_context() -> struct FunctionContext *; - [[nodiscard]] IkarusBlueprint * get_blueprint(IkarusId id) { - return get_cached_object(id, this->_blueprints); - } +public: + [[nodiscard]] auto get_blueprint(IkarusId id) -> struct IkarusBlueprint *; + auto uncache_blueprint(struct IkarusBlueprint * blueprint) -> void; - auto remove_blueprint(IkarusBlueprint * blueprint) -> void { - remove_cached_object(blueprint, _blueprints); - } + [[nodiscard]] auto get_entity(IkarusId id) -> struct IkarusEntity *; + auto uncache_entity(struct IkarusEntity * entity) -> void; - [[nodiscard]] auto get_entity(IkarusId id) -> IkarusEntity * { - return get_cached_object(id, this->_entities); - } - - auto remove_entity(IkarusEntity * entity) -> void { - remove_cached_object(entity, _entities); - } - - [[nodiscard]] auto get_property(IkarusId id) -> IkarusProperty * { - return get_cached_object(id, this->_properties); - } - - auto remove_property(IkarusProperty * property) -> void { - remove_cached_object(property, _properties); - } + [[nodiscard]] auto get_property(IkarusId id) -> struct IkarusProperty *; + auto uncache_property(struct IkarusProperty * property) -> void; private: template [[nodiscard]] T * get_cached_object(IkarusId id, std::unordered_map>& cache) { - if (auto iter = cache.find(id); iter == cache.cend()) { + auto const iter = cache.find(id); + + if (iter == cache.cend()) { auto [ret_iter, _] = cache.emplace(id, std::make_unique(this, id)); return ret_iter->second.get(); - } else { - return iter->second.get(); } + + return iter->second.get(); } template @@ -94,7 +55,7 @@ private: } private: - friend class FunctionContext; + friend struct FunctionContext; std::string _name; std::filesystem::path _path; @@ -109,41 +70,3 @@ private: std::stack _function_contexts; }; - -FunctionContext::FunctionContext(struct IkarusProject * project): - _project{project} {} - -FunctionContext::~FunctionContext() { - if (_project->_function_contexts.size() == 1) { - if (_project->error_message_buffer.empty()) { - _project->error_message_buffer.push_back('\0'); - } else { - _project->error_message_buffer[0] = '\0'; - } - - _project->error_infos = {}; - } - - _project->_function_contexts.pop(); -} - -template - requires(std::is_same_v && ...) && (sizeof...(Infos) <= MAXIMUM_ERROR_INFOS) -auto FunctionContext::set_error(std::string_view error_message, bool log_error, Infos... infos) -> void { - if (error_message.size() > _project->error_message_buffer.size()) { - _project->error_message_buffer.resize(error_message.size() + 1); - } - - for (int i = 0; i < error_message.size(); ++i) { - _project->error_message_buffer[i] = error_message[i]; - } - - _project->error_message_buffer[error_message.size()] = '\0'; - _project->error_infos = {infos...}; - - if (log_error) { - LOG_ERROR( - "Error({}): {}", fmt::join(_project->error_infos | std::views::transform(get_error_info_name), ", "), error_message - ); - } -} diff --git a/vendor/sqlitecpp b/vendor/sqlitecpp index 3057656..538616d 160000 --- a/vendor/sqlitecpp +++ b/vendor/sqlitecpp @@ -1 +1 @@ -Subproject commit 3057656ff277294ab424af90e553e630c2a5e8f7 +Subproject commit 538616d8ced2d3f04659261ccae3b039d65da004