diff --git a/clang-format.txt b/clang-format.txt new file mode 100644 index 0000000..a600edb --- /dev/null +++ b/clang-format.txt @@ -0,0 +1,192 @@ +BasedOnStyle: Google + +AccessModifierOffset: -4 + +AlignAfterOpenBracket: BlockIndent +AlignArrayOfStructures: Right +AlignConsecutiveAssignments: + Enabled: false +AlignConsecutiveBitFields: + Enabled: false +AlignConsecutiveDeclarations: + Enabled: false +AlignConsecutiveMacros: AcrossEmptyLines +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: true + +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Empty +AllowShortCaseLabelsOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true + +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes + +BinPackArguments: false +BinPackParameters: false + +BitFieldColonSpacing: Both + +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyNamespace: false + SplitEmptyRecord: false + +BracedInitializerIndentWidth: 4 + +# BreakAdjacentStringLiterals: true +BreakAfterAttributes: Never +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeConceptDeclarations: Always +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: false +BreakConstructorInitializers: AfterColon +BreakInheritanceList: AfterColon +BreakStringLiterals: false + +ColumnLimit: 140 + +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 + +Cpp11BracedListStyle: true + +DerivePointerAlignment: false + +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: Always + +ExperimentalAutoDetectBinPacking: true + +FixNamespaceComments: true + +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^".+\.(h|hpp)"$' + Priority: 1 + - Regex: '^<[a-z0-9_]+\.h>$' + Priority: 2 + - Regex: '^<[a-z0-9_]+>$' + Priority: 3 + - Regex: '^$' + Priority: 4 + - Regex: '^$' + Priority: 5 + - Regex: '^$' + Priority: 6 + - Regex: '^$' + Priority: 7 + - Regex: '^$' + Priority: 8 + - Regex: '^$' + Priority: 9 + - Regex: '^$' + Priority: 10 + - Regex: '^$' + Priority: 11 + - Regex: '^$' + Priority: 12 + - Regex: '^$' + Priority: 13 + +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: NoIndent +IndentGotoLabels: false +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: true +InsertNewlineAtEOF: true +InsertTrailingCommas: Wrapped + +IntegerLiteralSeparator: + Binary: -1 + Decimal: 3 + Hex: -1 + +KeepEmptyLinesAtEOF: false +KeepEmptyLinesAtTheStartOfBlocks: false + +LambdaBodyIndentation: Signature +Language: Cpp + +LineEnding: LF + +MaxEmptyLinesToKeep: 1 + +NamespaceIndentation: None + +PPIndentWidth: -1 +PackConstructorInitializers: Never + +PointerAlignment: Middle +QualifierAlignment: Right +# QualifierOrder: [ 'friend', 'constexpr', 'inline', 'static', 'type', 'const', 'volatile' ] +ReferenceAlignment: Middle + +ReflowComments: true +RemoveBracesLLVM: false +RemoveParentheses: MultipleParentheses +RemoveSemicolon: true + +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope + +SeparateDefinitionBlocks: Always + +SortIncludes: CaseInsensitive +SortUsingDeclarations: LexicographicNumeric + +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: false +SpaceAroundPointerQualifiers: Both +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: false +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: 1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: c++20 + +TabWidth: 4 +UseTab: Never diff --git a/src/objects/blueprint.cpp b/src/objects/blueprint.cpp index c8bf1e5..c15c7bc 100644 --- a/src/objects/blueprint.cpp +++ b/src/objects/blueprint.cpp @@ -27,7 +27,7 @@ IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char c } void ikarus_blueprint_delete(IkarusBlueprint * blueprint) { - ikarus::util::delete_object(blueprint->project, blueprint); + ikarus::util::delete_object(blueprint); } void ikarus_blueprint_get_properties( diff --git a/src/objects/entity.cpp b/src/objects/entity.cpp index ce5d853..80d047e 100644 --- a/src/objects/entity.cpp +++ b/src/objects/entity.cpp @@ -2,20 +2,36 @@ #include +#include +#include #include #include 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 + return ikarus::util::insert_object( + project, + IkarusObjectType_Entity, + name, + [](auto * db, IkarusId id) { return db->execute("INSERT INTO `entities`(`id`) VALUES(?)", id); }, + [project](IkarusId id) { return project->get_entity(id); } + ).unwrap_value_or(nullptr); } + +void ikarus_entity_delete(IkarusEntity * entity) { + ikarus::util::delete_object(entity); +} + +bool ikarus_entity_is_linked_to_blueprint(IkarusEntity const * entity, struct IkarusBlueprint const * blueprint) { + return ikarus::util::check_exists( + entity, + ikarus::util::ExistsQueryData{ + .table_name = "entity_blueprint_links", + .where_field_name = "blueprint", + .where_field_value = blueprint->id, + .relation_desc = "linked blueprints" + } + ) + .unwrap_value_or(false); +} + +bool ikarus_entity_link_to_blueprint(IkarusEntity * entity, struct IkarusBlueprint * blueprint) {} diff --git a/src/objects/properties/property.cpp b/src/objects/properties/property.cpp index bea1a0d..d28275d 100644 --- a/src/objects/properties/property.cpp +++ b/src/objects/properties/property.cpp @@ -23,10 +23,6 @@ IkarusProperty::Data const & IkarusProperty::get_data() const { } cppbase::Result IkarusProperty::get_property_type(IkarusProject * project, IkarusId id) { - LOG_DEBUG("fetching unboxed property type"); - - LOG_VERBOSE("project={};property={}", project->get_path().c_str(), id); - auto * ctx = project->get_function_context(); VTRY( @@ -45,10 +41,6 @@ cppbase::Result IkarusProperty: } IKA_API void ikarus_property_delete(IkarusProperty * property) { - LOG_INFO("deleting property"); - - LOG_VERBOSE("project={};property={}", property->project->get_path().c_str(), property->id); - auto * ctx = property->project->get_function_context(); TRYRV(, property->project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", property->id).on_error([ctx](auto const & err) { @@ -60,24 +52,14 @@ IKA_API void ikarus_property_delete(IkarusProperty * property) { ); })); - LOG_VERBOSE("property was successfully deleted from database, freeing"); - property->project->uncache(property); - - LOG_VERBOSE("successfully deleted property"); } IkarusPropertyType ikarus_property_get_type(IkarusProperty const * property) { - LOG_VERBOSE("fetching property type"); - return IkarusProperty::get_property_type(property->project, property->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->project->get_path().c_str(), property->id); - auto * ctx = property->project->get_function_context(); VTRYRV( @@ -112,10 +94,6 @@ IkarusPropertySource const * ikarus_property_get_source(IkarusProperty const * p } IkarusValue * ikarus_property_get_default_value(IkarusProperty const * property) { - LOG_VERBOSE("fetching property default value"); - - LOG_VERBOSE("project={};property={}", property->project->get_path().c_str(), property->id); - auto * ctx = property->project->get_function_context(); VTRYRV( diff --git a/src/objects/util.hpp b/src/objects/util.hpp index 0ba4b37..2779478 100644 --- a/src/objects/util.hpp +++ b/src/objects/util.hpp @@ -28,10 +28,6 @@ template ) { auto const * object_type_str = ikarus_object_type_to_string(type); - LOG_INFO("creating new {}", object_type_str); - - LOG_DEBUG("project={}; name={}", project->get_path().c_str(), name); - auto * ctx = project->get_function_context(); if (cppbase::is_empty_or_blank(name)) { @@ -43,14 +39,10 @@ template auto const id, project->get_db() ->transact([&](auto * db) -> cppbase::Result { - LOG_VERBOSE("creating {} in objects table", object_type_str); - TRY(db->execute("INSERT INTO `objects` (`object_type`, `name`) VALUES(?, ?);", static_cast(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); @@ -65,23 +57,17 @@ template }) ); - LOG_VERBOSE("successfully created {}", object_type_str); - return cppbase::ok(object_factory(id)); } template requires std::derived_from -void delete_object(IkarusProject * project, Object * object) { +void delete_object(Object * object) { auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id)); - LOG_INFO("deleting {}", object_type_str); - - LOG_DEBUG("project={}; {}={}", object_type_str, object->project->get_path().c_str(), object->id); - auto * ctx = object->project->get_function_context(); - TRYRV(, project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", object->id).on_error([&](auto const & err) { + TRYRV(, object->project->get_db()->execute("DELETE FROM `objects` WHERE `id` = ?", object->id).on_error([&](auto const & err) { ctx->set_error( fmt::format("failed to delete {} from objects table: {}", object_type_str, err), true, @@ -90,11 +76,7 @@ void delete_object(IkarusProject * project, Object * object) { ); })); - LOG_VERBOSE("{} was successfully deleted from database, freeing", object_type_str); - - project->uncache(object); - - LOG_VERBOSE("successfully deleted {}", object_type_str); + object->project->uncache(object); } struct SingleQueryData { @@ -107,10 +89,6 @@ template cppbase::Result fetch_single_field(Object const * object, SingleQueryData const & query_data) { auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id)); - LOG_VERBOSE("fetching property default value"); - - LOG_VERBOSE("project={};property={}", object->project->get_path().c_str(), object->id); - auto * ctx = object->project->get_function_context(); VTRY( @@ -155,10 +133,6 @@ void fetch_multiple_buffered( auto object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id)); - LOG_VERBOSE("fetching {} {}", object_type_str, query_data.relation_desc); - - LOG_VERBOSE("project={};{}={}", object->project->get_path().c_str(), object_type_str, object->id); - Selected select_buffer[buffer_size]; TRYRV( @@ -185,8 +159,6 @@ void fetch_multiple_buffered( }) ); - LOG_DEBUG("{} {}: [{}]", object_type_str, query_data.relation_desc, fmt::join(select_buffer, select_buffer + buffer_size, ", ")); - for (size_t i = 0; i < buffer_size; ++i) { VTRYRV(mapped_buffer[i], , transformer(object->project, ctx, select_buffer[i])); } @@ -204,12 +176,8 @@ template cppbase::Result fetch_count(Object const * object, CountQueryData const & query_data) { auto * object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id)); - LOG_VERBOSE("fetching {} {} count", object_type_str, query_data.relation_desc); - auto * ctx = object->project->get_function_context(); - LOG_DEBUG("{}={}", object_type_str, object->id); - VTRY( auto count, object->project->get_db() @@ -232,11 +200,42 @@ cppbase::Result fetch_count(Object const }) ); - LOG_DEBUG("{} {} count: {}", object_type_str, query_data.relation_desc, count); - - LOG_VERBOSE("successfully fetched {} {} count", object_type_str, query_data.relation_desc); - return cppbase::ok(static_cast(count)); } +template +struct ExistsQueryData { + std::string_view table_name; + std::string_view where_field_name; + T where_field_value; + std::string_view relation_desc; +}; + +template + requires std::derived_from +cppbase::Result check_exists(Object const * object, ExistsQueryData const & query_data) { + auto * object_type_str = ikarus_object_type_to_string(ikarus_id_get_object_type(object->id)); + + auto * ctx = object->project->get_function_context(); + + VTRY( + auto exists, + object->project->get_db() + ->template query_one( + fmt::format("SELECT EXISTS(SELECT 1 FROM `{}` WHERE `{}` = ?);", query_data.table_name, query_data.where_field_name), + query_data.where_field_value + ) + .on_error([&](auto const & err) { + ctx->set_error( + fmt::format("failed to check whether {} {} exists: {}", object_type_str, query_data.relation_desc, err), + true, + IkarusErrorInfo_Source_SubSystem, + IkarusErrorInfo_Type_SubSystem_Database + ); + }) + ); + + return cppbase::ok(static_cast(exists)); +} + } // namespace ikarus::util diff --git a/vendor/sqlitecpp b/vendor/sqlitecpp index 2a93b8b..00a1afc 160000 --- a/vendor/sqlitecpp +++ b/vendor/sqlitecpp @@ -1 +1 @@ -Subproject commit 2a93b8b1a8be03a9a9c4c72956b1111299de0ddd +Subproject commit 00a1afcc5f564f562c436f1ddfa4f44bb6489b17