intermediate commit

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
folling 2023-11-24 14:52:46 +01:00 committed by Folling
parent 82e5309f80
commit ea221cdf85
Signed by: folling
SSH key fingerprint: SHA256:S9qEx5WCFFLK49tE/LKnKuJYM5sw+++Dn6qJbbyxnCY
11 changed files with 175 additions and 175 deletions

View file

@ -1,4 +1,4 @@
#include "blueprint.hpp"
#include <iterator>
#include <cppbase/logger.hpp>
#include <cppbase/result.hpp>
@ -6,6 +6,7 @@
#include <ikarus/objects/blueprint.h>
#include <objects/blueprint.hpp>
#include <objects/entity.hpp>
#include <objects/property.hpp>
#include <persistence/project.hpp>
@ -18,7 +19,7 @@ IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char c
return nullptr;
}
auto ctx = project->function_context();
auto * ctx = project->function_context();
if (name == nullptr) {
ctx->set_error("name is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
@ -30,7 +31,7 @@ IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char c
return nullptr;
}
LOG_VERBOSE("project={}; name={}", project->path().c_str(), name);
LOG_DEBUG("project={}; name={}", project->path().c_str(), name);
VTRYRV(
auto id,
@ -45,9 +46,9 @@ IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char c
name
));
auto id = db->last_insert_rowid();
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Blueprint);
LOG_VERBOSE("id is {}", id);
LOG_DEBUG("blueprint is {}", id);
LOG_VERBOSE("inserting blueprint into blueprints table");
@ -80,7 +81,7 @@ void ikarus_blueprint_delete(IkarusBlueprint * blueprint) {
auto * ctx = blueprint->project->function_context();
LOG_VERBOSE("blueprint={}", blueprint->id);
LOG_DEBUG("blueprint={}", blueprint->id);
TRYRV(
,
@ -113,13 +114,13 @@ size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
auto * ctx = blueprint->project->function_context();
LOG_VERBOSE("blueprint={}", blueprint->id);
LOG_DEBUG("blueprint={}", blueprint->id);
VTRYRV(
auto count,
0,
blueprint->project->db()
->query_one<int>("SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint_id` = ?;", blueprint->id)
->query_one<int>("SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint` = ?;", blueprint->id)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint property count: {}", err),
@ -130,6 +131,10 @@ size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
})
);
LOG_DEBUG("blueprint property count: {}", count);
LOG_VERBOSE("successfully fetched blueprint property count");
return static_cast<size_t>(count);
}
@ -146,11 +151,11 @@ void ikarus_blueprint_get_properties(
auto * ctx = blueprint->project->function_context();
if (properties_out == nullptr) {
LOG_ERROR("properties_out is nullptr");
ctx->set_error("properties_out is null", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return;
}
LOG_VERBOSE("blueprint={}; properties_out_size={}", blueprint->id, properties_out_size);
LOG_DEBUG("blueprint={}; properties_out_size={}", blueprint->id, properties_out_size);
IkarusId ids[properties_out_size];
@ -173,6 +178,8 @@ void ikarus_blueprint_get_properties(
})
);
LOG_DEBUG("blueprint properties: [{}]", fmt::join(ids, ids + properties_out_size, ", "));
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]);
@ -180,3 +187,108 @@ void ikarus_blueprint_get_properties(
LOG_VERBOSE("successfully fetched blueprint properties");
}
size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint) {
LOG_VERBOSE("fetching blueprint linked entity count");
if (blueprint == nullptr) {
LOG_ERROR("blueprint is nullptr");
return 0;
}
auto * ctx = blueprint->project->function_context();
LOG_DEBUG("blueprint={}", blueprint->id);
VTRYRV(
auto count,
0,
blueprint->project->db()
->query_one<int>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?;", blueprint->id)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint linked entity count: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint linked entity count: {}", count);
LOG_VERBOSE("successfully fetched blueprint linked entity count: {}", count);
return static_cast<size_t>(count);
}
void ikarus_blueprint_get_linked_entities(
IkarusBlueprint const * blueprint, struct IkarusEntity ** entities_out, size_t entities_out_size
) {
LOG_VERBOSE("fetching blueprint linked entities");
if (blueprint == nullptr) {
LOG_ERROR("blueprint is nullptr");
return;
}
auto * ctx = blueprint->project->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);
IkarusId ids[entities_out_size];
TRYRV(
,
blueprint->project->db()
->query_many_buffered<IkarusId>(
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
static_cast<IkarusId *>(ids),
entities_out_size,
blueprint->id
)
.on_error([ctx](auto const& err) {
ctx->set_error(
fmt::format("failed to fetch blueprint linked entity ids: {}", err),
true,
IkarusErrorInfo_Source_SubSystem,
IkarusErrorInfo_Type_SubSystem_Database
);
})
);
LOG_DEBUG("blueprint linked entities: [{}]", fmt::join(ids, ids + entities_out_size, ", "));
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]);
}
LOG_VERBOSE("successfully fetched blueprint linked entities");
}
IkarusObject * ikarus_blueprint_to_object(IkarusBlueprint * blueprint) {
return const_cast<IkarusObject *>(ikarus_blueprint_to_object_const(blueprint));
}
IkarusObject const * ikarus_blueprint_to_object_const(IkarusBlueprint const * blueprint) {
LOG_VERBOSE("casting blueprint to object");
if (blueprint == nullptr) {
LOG_ERROR("blueprint is nullptr");
return nullptr;
}
// auto * ctx = blueprint->project->function_context();
LOG_DEBUG("blueprint={}", blueprint->id);
LOG_VERBOSE("successfully casted blueprint to object");
return static_cast<IkarusObject const *>(blueprint);
}

View file

@ -9,79 +9,5 @@
#include <objects/blueprint.hpp>
#include <objects/entity.hpp>
#include <objects/property.hpp>
#include <objects/property_info.hpp>
#include <objects/property_source.hpp>
#include <persistence/project.hpp>
IkarusProperty * ikarus_property_create(
struct IkarusProject * project,
char const * name,
struct IkarusPropertySource * property_source,
struct IkarusPropertyInfo * property_info
) {
LOG_INFO("creating new property");
if (project == nullptr) {
LOG_ERROR("project is nullptr");
return nullptr;
}
auto ctx = project->function_context();
if (property_source == nullptr) {
ctx->set_error("property_source is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
if (name == nullptr) {
ctx->set_error("name is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
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;
}
if (property_info == nullptr) {
ctx->set_error("property_info is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
return nullptr;
}
LOG_VERBOSE(
"project={}; name={}; property_source={}; property_info={}",
project->path().c_str(),
name,
std::visit(
cppbase::overload{
[](IkarusBlueprint * blueprint) { return fmt::format("Blueprint({})", blueprint->id); },
[](IkarusEntity * entity) { return fmt::format("Entity({})", entity->id); }},
property_source->data
),
std::visit(
cppbase::overload{
[](IkarusTogglePropertyInfo * info) {
return fmt::format(
"Toggle(default_value={})",
cppbase::OwningString{ikarus_value_to_string(ikarus_toggle_value_to_entity_value(info->default_value))}
.data
);
},
[](IkarusNumberPropertyInfo * info) {
return fmt::format(
"Number(default_value={})",
cppbase::OwningString{ikarus_value_to_string(ikarus_number_value_to_entity_value(info->default_value))}
.data
);
},
[](IkarusTextPropertyInfo * info) {
return fmt::format(
"Text(default_value={})",
cppbase::OwningString{ikarus_value_to_string(ikarus_text_value_to_entity_value(info->default_value))}
.data
);
}},
property_info->data
)
);
}

View file

@ -1,74 +0,0 @@
#pragma once
#include <variant>
#include <ikarus/objects/property_info.h>
#include <values/number_value.hpp>
#include <values/text_value.hpp>
#include <values/toggle_value.hpp>
// this looks a bit cursed, but there's reason to my madness:
// Let's go over the facts:
// 1. The client uses the concrete types (e.g. Ikarus"Toggle"PropertyInfo)
// 2. The API needs to accept a common type (i.e. IkarusPropertyInfo)
// 3. Casting between a concrete subtype and a common type is only defined behaviour if we use inheritance, otherwise an
// allocation is unavoidable
// 4. On the implementation side, we need to be able to distinguish between the concrete types
//
// There's a few ways to model this (using dynamic_casts, using an enum, a union, the visitor pattern, ...) but std::variant
// gives us the most type safety without any performance overhead
/// \private
struct IkarusPropertyInfo {
public:
using IkarusPropertyInfoData =
std::variant<struct IkarusTogglePropertyInfo *, struct IkarusNumberPropertyInfo *, struct IkarusTextPropertyInfo *>;
public:
inline explicit IkarusPropertyInfo(
std::variant<struct IkarusTogglePropertyInfo *, struct IkarusNumberPropertyInfo *, struct IkarusTextPropertyInfo *> data
):
data{data} {}
public:
[[nodiscard]] inline IkarusPropertyInfoData const& get_data() const {
return data;
}
private:
IkarusPropertyInfoData data;
};
/// \private
struct IkarusTogglePropertyInfo : public IkarusPropertyInfo {
public:
inline IkarusTogglePropertyInfo():
IkarusPropertyInfo{this} {}
public:
[[nodiscard]] IkarusToggleValue * get_default_value() const {}
private:
IkarusToggleValue * default_value{nullptr};
};
/// \private
struct IkarusNumberPropertyInfo : public IkarusPropertyInfo {
public:
inline IkarusNumberPropertyInfo():
IkarusPropertyInfo{this} {}
private:
IkarusNumberValue * default_value{nullptr};
};
/// \private
struct IkarusTextPropertyInfo : public IkarusPropertyInfo {
public:
inline IkarusTextPropertyInfo():
IkarusPropertyInfo{this} {}
private:
IkarusTextValue * default_value{nullptr};
};

View file

@ -2,7 +2,7 @@
#include <variant>
#include <ikarus/objects/property_source.h>
#include <ikarus/objects/properties/property_source.h>
/// \private
struct IkarusPropertySource {