add src/ikarus subdir and make names unique for objects per scope
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
c65211e4ff
commit
5dce2ced94
51 changed files with 590 additions and 735 deletions
160
src/ikarus/objects/blueprint.cpp
Normal file
160
src/ikarus/objects/blueprint.cpp
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
#include "ikarus/objects/blueprint.h"
|
||||
|
||||
#include <cppbase/logger.hpp>
|
||||
#include <cppbase/result.hpp>
|
||||
#include <cppbase/strings.hpp>
|
||||
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/objects/blueprint.hpp>
|
||||
#include <ikarus/objects/entity.hpp>
|
||||
#include <ikarus/objects/properties/property.h>
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/objects/util.hpp>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
|
||||
IkarusBlueprint::IkarusBlueprint(IkarusProject * project, IkarusId id):
|
||||
IkarusObject{project, id} {}
|
||||
|
||||
IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char const * name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, project, nullptr, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
IkarusId const id,
|
||||
nullptr,
|
||||
"failed to create blueprint: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->transact([name](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
|
||||
TRY(db->execute("INSERT INTO `objects`(`type`, `name`, `information`) VALUES(?, ?, ?)", IkarusObjectType_Blueprint, name, ""));
|
||||
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Blueprint);
|
||||
TRY(db->execute("INSERT INTO `blueprints`(`id`) VALUES(?)", id));
|
||||
return cppbase::ok(id);
|
||||
})
|
||||
);
|
||||
|
||||
return project->get_blueprint(id);
|
||||
}
|
||||
|
||||
void ikarus_blueprint_delete(IkarusBlueprint * blueprint, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(blueprint, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to delete blueprint: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
blueprint->project->db->execute("DELETE FROM `objects` WHERE `id` = ?", blueprint->id)
|
||||
);
|
||||
|
||||
blueprint->project->uncache(blueprint);
|
||||
}
|
||||
|
||||
IkarusProject * ikarus_blueprint_get_project(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
|
||||
return ikarus::util::object_get_project(blueprint, error_out);
|
||||
}
|
||||
|
||||
char const * ikarus_blueprint_get_name(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
|
||||
return ikarus::util::object_get_name(blueprint, error_out);
|
||||
}
|
||||
|
||||
void ikarus_blueprint_set_name(IkarusBlueprint * blueprint, char const * name, IkarusErrorData * error_out) {
|
||||
ikarus::util::object_set_name(blueprint, name, error_out);
|
||||
}
|
||||
|
||||
void ikarus_blueprint_get_properties(
|
||||
IkarusBlueprint const * blueprint,
|
||||
struct IkarusProperty ** properties_out,
|
||||
size_t properties_out_size,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(blueprint, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
|
||||
IKARUS_FAIL_IF_NULL(properties_out, );
|
||||
|
||||
if (properties_out_size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::tuple<IkarusId, IkarusPropertyType> ids_and_types[properties_out_size];
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to fetch blueprint properties from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
blueprint->project->db->query_many_buffered<IkarusId, IkarusPropertyType>(
|
||||
"SELECT `id` FROM `properties` WHERE `source` = ?",
|
||||
ids_and_types,
|
||||
properties_out_size,
|
||||
blueprint->id
|
||||
)
|
||||
)
|
||||
|
||||
for (size_t i = 0; i < properties_out_size; ++i) {
|
||||
auto [id, type] = ids_and_types[i];
|
||||
|
||||
properties_out[i] = blueprint->project->get_property(id, type);
|
||||
}
|
||||
}
|
||||
|
||||
size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(blueprint, 0);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, 0);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
0,
|
||||
"unable to fetch blueprint property count from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
blueprint->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `properties` WHERE `source` = ?", blueprint->id)
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ikarus_blueprint_get_linked_entities(
|
||||
IkarusBlueprint const * blueprint,
|
||||
struct IkarusEntity ** entities_out,
|
||||
size_t entities_out_size,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(blueprint, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
|
||||
IKARUS_FAIL_IF_NULL(entities_out, );
|
||||
|
||||
if (entities_out_size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
IkarusId ids[entities_out_size];
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to fetch blueprint linked entities from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
blueprint->project->db->query_many_buffered<IkarusId>(
|
||||
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
|
||||
ids,
|
||||
entities_out_size,
|
||||
blueprint->id
|
||||
)
|
||||
)
|
||||
|
||||
for (size_t i = 0; i < entities_out_size; ++i) {
|
||||
entities_out[i] = blueprint->project->get_entity(ids[i]);
|
||||
}
|
||||
}
|
||||
|
||||
size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(blueprint, 0);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, 0);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
0,
|
||||
"unable to fetch blueprint linked entity count from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
blueprint->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?", blueprint->id)
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
21
src/ikarus/objects/blueprint.hpp
Normal file
21
src/ikarus/objects/blueprint.hpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/object.hpp>
|
||||
|
||||
struct IkarusBlueprint : IkarusObject {
|
||||
public:
|
||||
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;
|
||||
|
||||
public:
|
||||
inline std::string_view get_table_name() const noexcept override {
|
||||
return "blueprints";
|
||||
}
|
||||
};
|
||||
286
src/ikarus/objects/entity.cpp
Normal file
286
src/ikarus/objects/entity.cpp
Normal file
|
|
@ -0,0 +1,286 @@
|
|||
#include "entity.hpp"
|
||||
|
||||
#include <cppbase/strings.hpp>
|
||||
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/objects/blueprint.hpp>
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/objects/util.hpp>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
#include <ikarus/values/entity_property_value.hpp>
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
IkarusEntity * ikarus_entity_create(struct IkarusProject * project, char const * name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, project, nullptr, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
IkarusId const id,
|
||||
nullptr,
|
||||
"failed to create entity: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->transact([name](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
|
||||
TRY(db->execute("INSERT INTO `objects`(`type`) VALUES(?, ?, ?)", IkarusObjectType_Entity));
|
||||
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Entity);
|
||||
TRY(db->execute("INSERT INTO `entities`(`id`, `name`) VALUES(?)", id, name));
|
||||
return cppbase::ok(id);
|
||||
})
|
||||
);
|
||||
|
||||
return project->get_entity(id);
|
||||
}
|
||||
|
||||
void ikarus_entity_delete(IkarusEntity * entity, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(entity, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to delete entity: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->execute("DELETE FROM `objects` WHERE `id` == ?", entity->id)
|
||||
);
|
||||
|
||||
entity->project->uncache(entity);
|
||||
}
|
||||
|
||||
IkarusProject * ikarus_entity_get_project(IkarusEntity const * entity, IkarusErrorData * error_out) {
|
||||
return ikarus::util::object_get_project(entity, error_out);
|
||||
}
|
||||
|
||||
char const * ikarus_entity_get_name(IkarusEntity const * entity, IkarusErrorData * error_out) {
|
||||
return ikarus::util::object_get_name(entity, error_out);
|
||||
}
|
||||
|
||||
void ikarus_entity_set_name(IkarusEntity * entity, char const * name, IkarusErrorData * error_out) {
|
||||
ikarus::util::object_set_name(entity, name, error_out);
|
||||
}
|
||||
|
||||
bool ikarus_entity_is_linked_to_blueprint(
|
||||
IkarusEntity const * entity,
|
||||
struct IkarusBlueprint const * blueprint,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(entity, false);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, false);
|
||||
IKARUS_FAIL_IF_NULL(blueprint, false);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, false);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
false,
|
||||
"unable to check whether entity is linked to blueprint",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->query_one<bool>(
|
||||
"SELECT EXISTS(SELECT 1 FROM `entity_blueprint_links` WHERE `entity` = ? AND `blueprint` = ?)",
|
||||
entity->id,
|
||||
blueprint->id
|
||||
)
|
||||
)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ikarus_entity_link_to_blueprint(IkarusEntity * entity, struct IkarusBlueprint * blueprint, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(entity, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
|
||||
IKARUS_FAIL_IF_NULL(blueprint, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to link entity to blueprint: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->execute(
|
||||
"INSERT INTO `entity_blueprint_links`(`entity`, `blueprint`) VALUES(?, ?) ON CONFLICT(`entity`, `blueprint`) DO NOTHING",
|
||||
entity->id,
|
||||
blueprint->id
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void ikarus_entity_unlink_from_blueprint(IkarusEntity * entity, struct IkarusBlueprint * blueprint, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(entity, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
|
||||
IKARUS_FAIL_IF_NULL(blueprint, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(blueprint, );
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to unlink entity from blueprint: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db
|
||||
->execute("DELETE FROM `entity_blueprint_links` WHERE `entity` = ? AND `blueprint` = ?", entity->id, blueprint->id)
|
||||
);
|
||||
}
|
||||
|
||||
void ikarus_entity_get_linked_blueprints(
|
||||
IkarusEntity const * entity,
|
||||
struct IkarusBlueprint ** blueprints_out,
|
||||
size_t blueprints_out_size,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(entity, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
|
||||
IKARUS_FAIL_IF_NULL(blueprints_out, );
|
||||
|
||||
if (blueprints_out_size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
IkarusId ids[blueprints_out_size];
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to fetch entity linked blueprints from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->query_many_buffered<IkarusId>(
|
||||
"SELECT `blueprint` FROM `entity_blueprint_links` WHERE `entity` = ?",
|
||||
ids,
|
||||
blueprints_out_size,
|
||||
entity->id
|
||||
)
|
||||
)
|
||||
|
||||
for (size_t i = 0; i < blueprints_out_size; ++i) {
|
||||
blueprints_out[i] = entity->project->get_blueprint(ids[i]);
|
||||
}
|
||||
}
|
||||
|
||||
size_t ikarus_entity_get_linked_blueprint_count(IkarusEntity const * entity, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(entity, 0);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, 0);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
0,
|
||||
"unable to fetch entity linked blueprint count from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `entity` = ?", entity->id)
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ikarus_entity_has_property(IkarusEntity const * entity, struct IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(entity, false);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, false);
|
||||
IKARUS_FAIL_IF_NULL(property, false);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, false);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
false,
|
||||
"unable to check whether entity has property: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->query_one<bool>(
|
||||
"SELECT EXISTS(SELECT 1 FROM `entity_properties` WHERE `entity` = ? AND `property` = ?)",
|
||||
entity->id,
|
||||
property->id
|
||||
)
|
||||
)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ikarus_entity_get_properties(
|
||||
IkarusEntity const * entity,
|
||||
struct IkarusProperty ** properties_out,
|
||||
size_t properties_out_size,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(entity, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
|
||||
IKARUS_FAIL_IF_NULL(properties_out, );
|
||||
|
||||
if (properties_out_size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::tuple<IkarusId, IkarusPropertyType> ids_and_types[properties_out_size];
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to fetch entity properties from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->query_many_buffered<IkarusId, IkarusPropertyType>(
|
||||
"SELECT `property`, `type` FROM `properties` WHERE `source` = ?",
|
||||
ids_and_types,
|
||||
properties_out_size,
|
||||
entity->id
|
||||
)
|
||||
)
|
||||
|
||||
for (size_t i = 0; i < properties_out_size; ++i) {
|
||||
auto [id, type] = ids_and_types[i];
|
||||
properties_out[i] = entity->project->get_property(id, type);
|
||||
}
|
||||
}
|
||||
|
||||
size_t ikarus_entity_get_property_count(IkarusEntity const * entity, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(entity, 0);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, 0);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
size_t const ret,
|
||||
0,
|
||||
"unable to fetch entity property count from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `properties` WHERE `source` = ?", entity->id)
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct IkarusEntityPropertyValue *
|
||||
ikarus_entity_get_value(IkarusEntity const * entity, struct IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(entity, nullptr);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(property, nullptr);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, nullptr);
|
||||
|
||||
auto * value = fetch_value_from_db(
|
||||
entity->project,
|
||||
error_out,
|
||||
"SELECT IFNULL((SELECT `value` FROM `values` WHERE `entity` = ? AND `property` = ?), (SELECT `default_value` FROM `properties` WHERE `id` = ?))",
|
||||
entity->id,
|
||||
property->id,
|
||||
property->id
|
||||
);
|
||||
|
||||
IKARUS_FAIL_IF_ERROR(nullptr);
|
||||
|
||||
return new IkarusEntityPropertyValue{
|
||||
.entity = entity,
|
||||
.property = property,
|
||||
.value = value,
|
||||
};
|
||||
}
|
||||
|
||||
void ikarus_entity_set_value(
|
||||
IkarusEntity * entity,
|
||||
struct IkarusProperty const * property,
|
||||
struct IkarusEntityPropertyValue * value,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(entity, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
|
||||
IKARUS_FAIL_IF_NULL(property, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, );
|
||||
IKARUS_FAIL_IF_NULL(value, );
|
||||
|
||||
auto value_json_str = boost::json::serialize(value->value->to_json());
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to set entity property value: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->execute(
|
||||
"INSERT INTO `values`(`entity`, `property`, `value`) VALUES(?, ?, ?) ON CONFLICT(`entity`, `property`) DO UPDATE SET `value` = ?",
|
||||
entity->id,
|
||||
property->id,
|
||||
value_json_str,
|
||||
value_json_str
|
||||
)
|
||||
);
|
||||
}
|
||||
22
src/ikarus/objects/entity.hpp
Normal file
22
src/ikarus/objects/entity.hpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/object.hpp>
|
||||
|
||||
struct IkarusEntity : IkarusObject {
|
||||
public:
|
||||
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;
|
||||
|
||||
public:
|
||||
inline std::string_view get_table_name() const noexcept override {
|
||||
return "entities";
|
||||
}
|
||||
};
|
||||
93
src/ikarus/objects/object.cpp
Normal file
93
src/ikarus/objects/object.cpp
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
#include "object.hpp"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <cppbase/strings.hpp>
|
||||
|
||||
#include <ikarus/errors.h>
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/objects/blueprint.hpp>
|
||||
#include <ikarus/objects/entity.hpp>
|
||||
#include <ikarus/objects/object.h>
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
|
||||
IkarusObject::IkarusObject(IkarusProject * project, IkarusId id):
|
||||
project{project},
|
||||
id{id} {}
|
||||
|
||||
void ikarus_object_visit(
|
||||
IkarusObject * object,
|
||||
void (*blueprint_visitor)(struct IkarusBlueprint *, IkarusErrorData * error_out, void *),
|
||||
void (*property_visitor)(struct IkarusProperty *, IkarusErrorData * error_out, void *),
|
||||
void (*entity_visitor)(struct IkarusEntity *, IkarusErrorData * error_out, void *),
|
||||
void * data,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
struct Data {
|
||||
void (*blueprint_visitor)(struct IkarusBlueprint *, IkarusErrorData * error_out, void *);
|
||||
void (*property_visitor)(struct IkarusProperty *, IkarusErrorData * error_out, void *);
|
||||
void (*entity_visitor)(struct IkarusEntity *, IkarusErrorData * error_out, void *);
|
||||
void * data;
|
||||
};
|
||||
|
||||
Data passthru_data{blueprint_visitor, property_visitor, entity_visitor, data};
|
||||
|
||||
ikarus_object_visit_const(
|
||||
object,
|
||||
[](IkarusBlueprint const * blueprint, IkarusErrorData * error_out, void * data) {
|
||||
auto const * passthru_data = static_cast<Data *>(data);
|
||||
passthru_data->blueprint_visitor(const_cast<IkarusBlueprint *>(blueprint), error_out, passthru_data->data);
|
||||
},
|
||||
[](IkarusProperty const * property, IkarusErrorData * error_out, void * data) {
|
||||
auto const * passthru_data = static_cast<Data *>(data);
|
||||
passthru_data->property_visitor(const_cast<IkarusProperty *>(property), error_out, passthru_data->data);
|
||||
},
|
||||
[](IkarusEntity const * entity, IkarusErrorData * error_out, void * data) {
|
||||
auto const * passthru_data = static_cast<Data *>(data);
|
||||
passthru_data->entity_visitor(const_cast<IkarusEntity *>(entity), error_out, passthru_data->data);
|
||||
},
|
||||
&passthru_data,
|
||||
error_out
|
||||
);
|
||||
}
|
||||
|
||||
void ikarus_object_visit_const(
|
||||
IkarusObject const * object,
|
||||
void (*blueprint_visitor)(struct IkarusBlueprint const *, IkarusErrorData * error_out, void *),
|
||||
void (*property_visitor)(struct IkarusProperty const *, IkarusErrorData * error_out, void *),
|
||||
void (*entity_visitor)(struct IkarusEntity const *, IkarusErrorData * error_out, void *),
|
||||
void * data,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(object, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(object, );
|
||||
|
||||
switch (ikarus_id_get_object_type(object->id)) {
|
||||
case IkarusObjectType_Entity: {
|
||||
auto const * entity = dynamic_cast<IkarusEntity const *>(object);
|
||||
IKARUS_FAIL_IF(entity == nullptr, , "object with entity id wasn't a entity", IkarusErrorInfo_LibIkarus_InvalidState);
|
||||
entity_visitor(entity, error_out, data);
|
||||
}
|
||||
case IkarusObjectType_Blueprint: {
|
||||
auto const * blueprint = dynamic_cast<IkarusBlueprint const *>(object);
|
||||
IKARUS_FAIL_IF(blueprint == nullptr, , "object with blueprint id wasn't a blueprint", IkarusErrorInfo_LibIkarus_InvalidState);
|
||||
blueprint_visitor(blueprint, error_out, data);
|
||||
}
|
||||
case IkarusObjectType_Property: {
|
||||
auto const * property = dynamic_cast<IkarusProperty const *>(object);
|
||||
IKARUS_FAIL_IF(property == nullptr, , "object with property id wasn't a property", IkarusErrorInfo_LibIkarus_InvalidState);
|
||||
property_visitor(property, error_out, data);
|
||||
}
|
||||
default: {
|
||||
IKARUS_FAIL(
|
||||
,
|
||||
fmt::format("unknown object type: {}", ikarus_object_type_to_string(ikarus_id_get_object_type(object->id))),
|
||||
IkarusErrorInfo_LibIkarus_InvalidState
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// not needed, but a good delineation of our intention if this function gets extended
|
||||
IKARUS_FAIL_IF_ERROR()
|
||||
}
|
||||
25
src/ikarus/objects/object.hpp
Normal file
25
src/ikarus/objects/object.hpp
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <ikarus/id.h>
|
||||
|
||||
struct IkarusObject {
|
||||
public:
|
||||
IkarusObject(struct IkarusProject * project, IkarusId id);
|
||||
|
||||
IkarusObject(IkarusObject const &) = default;
|
||||
IkarusObject(IkarusObject &&) = default;
|
||||
|
||||
IkarusObject & operator=(IkarusObject const &) = default;
|
||||
IkarusObject & operator=(IkarusObject &&) = default;
|
||||
|
||||
virtual ~IkarusObject() = default;
|
||||
|
||||
public:
|
||||
virtual std::string_view get_table_name() const noexcept = 0;
|
||||
|
||||
public:
|
||||
struct IkarusProject * project;
|
||||
IkarusId id;
|
||||
};
|
||||
12
src/ikarus/objects/object_type.cpp
Normal file
12
src/ikarus/objects/object_type.cpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include "ikarus/objects/object_type.h"
|
||||
|
||||
char const * ikarus_object_type_to_string(IkarusObjectType type) {
|
||||
switch (type) {
|
||||
case IkarusObjectType_None: return "none";
|
||||
case IkarusObjectType_Entity: return "entity";
|
||||
case IkarusObjectType_Blueprint: return "blueprint";
|
||||
case IkarusObjectType_Property: return "property";
|
||||
}
|
||||
|
||||
return "unknown";
|
||||
}
|
||||
32
src/ikarus/objects/properties/number_property.cpp
Normal file
32
src/ikarus/objects/properties/number_property.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include "number_property.hpp"
|
||||
|
||||
#include <cppbase/result.hpp>
|
||||
|
||||
#include <ikarus/objects/properties/property.h>
|
||||
#include <ikarus/objects/properties/property_scope.hpp>
|
||||
#include <ikarus/objects/properties/util.hpp>
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
IkarusNumberProperty::IkarusNumberProperty(IkarusProject * project, IkarusId id):
|
||||
IkarusProperty{project, id, this} {}
|
||||
|
||||
IkarusNumberProperty * ikarus_number_property_create(
|
||||
struct IkarusProject * project,
|
||||
char const * name,
|
||||
struct IkarusPropertyScope * property_source,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
return ikarus::util::create_property<IkarusNumberProperty>(project, name, property_source, error_out);
|
||||
}
|
||||
|
||||
IkarusNumberValue * ikarus_number_property_get_default_value(IkarusNumberProperty * property, IkarusErrorData * error_out) {
|
||||
return ikarus::util::get_default_value<IkarusNumberProperty>(property, error_out);
|
||||
}
|
||||
|
||||
void ikarus_number_property_set_default_value(
|
||||
IkarusNumberProperty * property,
|
||||
IkarusNumberValue * new_default_value,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
ikarus::util::set_default_value<IkarusNumberProperty>(property, new_default_value, error_out);
|
||||
}
|
||||
14
src/ikarus/objects/properties/number_property.hpp
Normal file
14
src/ikarus/objects/properties/number_property.hpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/objects/properties/property_type.h>
|
||||
#include <ikarus/values/number_value.hpp>
|
||||
|
||||
struct IkarusNumberProperty : IkarusProperty {
|
||||
public:
|
||||
using value_type = IkarusNumberValue;
|
||||
constexpr auto static PropertyType = IkarusPropertyType_Number;
|
||||
|
||||
public:
|
||||
IkarusNumberProperty(struct IkarusProject * project, IkarusId id);
|
||||
};
|
||||
139
src/ikarus/objects/properties/property.cpp
Normal file
139
src/ikarus/objects/properties/property.cpp
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
#include "property.hpp"
|
||||
|
||||
#include <cppbase/logger.hpp>
|
||||
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/objects/properties/property.h>
|
||||
#include <ikarus/objects/properties/property_scope.hpp>
|
||||
#include <ikarus/objects/properties/property_type.h>
|
||||
#include <ikarus/objects/util.hpp>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
IkarusProperty::IkarusProperty(IkarusProject * project, IkarusId id, Data data):
|
||||
IkarusObject{project, id},
|
||||
data{data} {}
|
||||
|
||||
IKA_API void ikarus_property_delete(IkarusProperty * property, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(property, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, );
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to delete property: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
property->project->db->execute("DELETE FROM `objects` WHERE `id` = ?", property->id)
|
||||
);
|
||||
|
||||
property->project->uncache(property);
|
||||
}
|
||||
|
||||
IkarusProject * ikarus_property_get_project(IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
return ikarus::util::object_get_project(property, error_out);
|
||||
}
|
||||
|
||||
char const * ikarus_property_get_name(IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
return ikarus::util::object_get_name(property, error_out);
|
||||
}
|
||||
|
||||
void ikarus_property_set_name(IkarusProperty * property, char const * name, IkarusErrorData * error_out) {
|
||||
ikarus::util::object_set_name(property, name, error_out);
|
||||
}
|
||||
|
||||
IkarusPropertyType ikarus_property_get_type(IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(property, IkarusPropertyType_Toggle);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, IkarusPropertyType_Toggle);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
IkarusPropertyType_Toggle,
|
||||
"unable to fetch property type from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
property->project->db->query_one<int>("SELECT `type` FROM `properties` WHERE `id` = ?", property->id)
|
||||
);
|
||||
|
||||
return static_cast<IkarusPropertyType>(ret);
|
||||
}
|
||||
|
||||
IkarusPropertyScope const * ikarus_property_get_scope(IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(property, nullptr);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const source,
|
||||
nullptr,
|
||||
"unable to fetch property source from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
property->project->db->query_one<IkarusId>("SELECT `source` FROM `properties` WHERE `id` = ?", property->id)
|
||||
);
|
||||
|
||||
switch (ikarus_id_get_object_type(source)) {
|
||||
case IkarusObjectType_Blueprint: return new IkarusPropertyScope{property->project->get_blueprint(source)};
|
||||
case IkarusObjectType_Entity: return new IkarusPropertyScope{property->project->get_entity(source)};
|
||||
default:
|
||||
IKARUS_FAIL(
|
||||
nullptr,
|
||||
fmt::format("invalid property source type: {}", ikarus_object_type_to_string(ikarus_id_get_object_type(source))),
|
||||
IkarusErrorInfo_LibIkarus_InvalidState
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
IkarusValue * ikarus_property_get_default_value(IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(property, nullptr);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, nullptr);
|
||||
|
||||
return fetch_value_from_db(property->project, error_out, "SELECT `default_value` FROM `properties` WHERE `id` = ?", property->id);
|
||||
}
|
||||
|
||||
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,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(property, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, );
|
||||
|
||||
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->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,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(property, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, );
|
||||
|
||||
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->data
|
||||
);
|
||||
}
|
||||
|
||||
// No existence checks here for performance reasons. All methods on IkarusObject perform this check anyway.
|
||||
|
||||
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);
|
||||
}
|
||||
29
src/ikarus/objects/properties/property.hpp
Normal file
29
src/ikarus/objects/properties/property.hpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include <variant>
|
||||
|
||||
#include <ikarus/objects/object.hpp>
|
||||
|
||||
struct IkarusProperty : IkarusObject {
|
||||
public:
|
||||
using Data = std::variant<struct IkarusToggleProperty *, struct IkarusNumberProperty *, struct IkarusTextProperty *>;
|
||||
|
||||
public:
|
||||
IkarusProperty(struct IkarusProject * project, IkarusId id, Data data);
|
||||
|
||||
IkarusProperty(IkarusProperty const &) = default;
|
||||
IkarusProperty(IkarusProperty &&) = default;
|
||||
|
||||
IkarusProperty & operator=(IkarusProperty const &) = default;
|
||||
IkarusProperty & operator=(IkarusProperty &&) = default;
|
||||
|
||||
~IkarusProperty() override = default;
|
||||
|
||||
public:
|
||||
inline std::string_view get_table_name() const noexcept override {
|
||||
return "properties";
|
||||
}
|
||||
|
||||
public:
|
||||
Data data;
|
||||
};
|
||||
57
src/ikarus/objects/properties/property_scope.cpp
Normal file
57
src/ikarus/objects/properties/property_scope.cpp
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#include "property_scope.hpp"
|
||||
|
||||
#include <cppbase/templates.hpp>
|
||||
|
||||
#include <ikarus/objects/blueprint.hpp>
|
||||
#include <ikarus/objects/entity.hpp>
|
||||
|
||||
IkarusPropertyScope::IkarusPropertyScope(Data data):
|
||||
data{data} {}
|
||||
|
||||
IkarusId IkarusPropertyScope::get_id() const {
|
||||
return std::visit(
|
||||
cppbase::overloaded{
|
||||
[](IkarusBlueprint const * blueprint) { return blueprint->id; },
|
||||
[](IkarusEntity const * entity) { return entity->id; }
|
||||
},
|
||||
data
|
||||
);
|
||||
}
|
||||
|
||||
IkarusPropertyScope * ikarus_property_source_create_blueprint(IkarusBlueprint * blueprint) {
|
||||
return new IkarusPropertyScope{blueprint};
|
||||
}
|
||||
|
||||
IkarusPropertyScope * ikarus_property_source_create_entity(IkarusEntity * entity) {
|
||||
return new IkarusPropertyScope{entity};
|
||||
}
|
||||
|
||||
void ikarus_property_source_visit(
|
||||
struct IkarusPropertyScope * property_source,
|
||||
void (*blueprint_visitor)(struct IkarusBlueprint *, void *),
|
||||
void (*entity_visitor)(struct IkarusEntity *, void *),
|
||||
void * user_data
|
||||
) {
|
||||
std::visit(
|
||||
cppbase::overloaded{
|
||||
[blueprint_visitor, user_data](IkarusBlueprint * blueprint) { blueprint_visitor(blueprint, user_data); },
|
||||
[entity_visitor, user_data](IkarusEntity * entity) { entity_visitor(entity, user_data); }
|
||||
},
|
||||
property_source->data
|
||||
);
|
||||
}
|
||||
|
||||
void ikarus_property_source_visit_const(
|
||||
struct IkarusPropertyScope const * property_source,
|
||||
void (*blueprint_visitor)(struct IkarusBlueprint const *, void *),
|
||||
void (*entity_visitor)(struct IkarusEntity const *, void *),
|
||||
void * user_data
|
||||
) {
|
||||
std::visit(
|
||||
cppbase::overloaded(
|
||||
[blueprint_visitor, user_data](IkarusBlueprint const * blueprint) { blueprint_visitor(blueprint, user_data); },
|
||||
[entity_visitor, user_data](IkarusEntity const * entity) { entity_visitor(entity, user_data); }
|
||||
),
|
||||
property_source->data
|
||||
);
|
||||
}
|
||||
39
src/ikarus/objects/properties/property_scope.hpp
Normal file
39
src/ikarus/objects/properties/property_scope.hpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
|
||||
#include <variant>
|
||||
|
||||
#include <ikarus/id.h>
|
||||
#include <ikarus/objects/properties/property_scope.h>
|
||||
|
||||
#define IKARUS_FAIL_IF_PROPERTY_SCOPE_INVALID(scope, ret) \
|
||||
std::visit( \
|
||||
cppbase::overloaded{[error_out](auto const * object) { \
|
||||
IKARUS_FAIL_IF_NULL(object, ); \
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(object, ); \
|
||||
}}, \
|
||||
scope->data \
|
||||
); \
|
||||
\
|
||||
IKARUS_FAIL_IF_ERROR(nullptr);
|
||||
|
||||
struct IkarusPropertyScope {
|
||||
public:
|
||||
using Data = std::variant<IkarusBlueprint *, IkarusEntity *>;
|
||||
|
||||
public:
|
||||
explicit IkarusPropertyScope(Data data);
|
||||
|
||||
IkarusPropertyScope(IkarusPropertyScope const &) = default;
|
||||
IkarusPropertyScope(IkarusPropertyScope &&) = default;
|
||||
|
||||
IkarusPropertyScope & operator=(IkarusPropertyScope const &) = default;
|
||||
IkarusPropertyScope & operator=(IkarusPropertyScope &&) = default;
|
||||
|
||||
virtual ~IkarusPropertyScope() = default;
|
||||
|
||||
public:
|
||||
[[nodiscard]] IkarusId get_id() const;
|
||||
|
||||
public:
|
||||
Data data;
|
||||
};
|
||||
32
src/ikarus/objects/properties/text_property.cpp
Normal file
32
src/ikarus/objects/properties/text_property.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include "text_property.hpp"
|
||||
|
||||
#include <cppbase/result.hpp>
|
||||
|
||||
#include <ikarus/objects/properties/property.h>
|
||||
#include <ikarus/objects/properties/property_scope.hpp>
|
||||
#include <ikarus/objects/properties/util.hpp>
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
IkarusTextProperty::IkarusTextProperty(IkarusProject * project, IkarusId id):
|
||||
IkarusProperty{project, id, this} {}
|
||||
|
||||
IkarusTextProperty * ikarus_text_property_create(
|
||||
struct IkarusProject * project,
|
||||
char const * name,
|
||||
struct IkarusPropertyScope * property_source,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
return ikarus::util::create_property<IkarusTextProperty>(project, name, property_source, error_out);
|
||||
}
|
||||
|
||||
IkarusTextValue * ikarus_text_property_get_default_value(IkarusTextProperty * property, IkarusErrorData * error_out) {
|
||||
return ikarus::util::get_default_value<IkarusTextProperty>(property, error_out);
|
||||
}
|
||||
|
||||
void ikarus_text_property_set_default_value(
|
||||
IkarusTextProperty * property,
|
||||
IkarusTextValue * new_default_value,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
ikarus::util::set_default_value<IkarusTextProperty>(property, new_default_value, error_out);
|
||||
}
|
||||
14
src/ikarus/objects/properties/text_property.hpp
Normal file
14
src/ikarus/objects/properties/text_property.hpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/objects/properties/property_type.h>
|
||||
#include <ikarus/values/text_value.hpp>
|
||||
|
||||
struct IkarusTextProperty : IkarusProperty {
|
||||
public:
|
||||
using value_type = IkarusTextValue;
|
||||
constexpr auto static PropertyType = IkarusPropertyType_Text;
|
||||
|
||||
public:
|
||||
IkarusTextProperty(struct IkarusProject * project, IkarusId id);
|
||||
};
|
||||
32
src/ikarus/objects/properties/toggle_property.cpp
Normal file
32
src/ikarus/objects/properties/toggle_property.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include "toggle_property.hpp"
|
||||
|
||||
#include <cppbase/result.hpp>
|
||||
|
||||
#include <ikarus/objects/properties/property.h>
|
||||
#include <ikarus/objects/properties/property_scope.hpp>
|
||||
#include <ikarus/objects/properties/util.hpp>
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
IkarusToggleProperty::IkarusToggleProperty(IkarusProject * project, IkarusId id):
|
||||
IkarusProperty{project, id, this} {}
|
||||
|
||||
IkarusToggleProperty * ikarus_toggle_property_create(
|
||||
struct IkarusProject * project,
|
||||
char const * name,
|
||||
struct IkarusPropertyScope * property_source,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
return ikarus::util::create_property<IkarusToggleProperty>(project, name, property_source, error_out);
|
||||
}
|
||||
|
||||
IkarusToggleValue * ikarus_toggle_property_get_default_value(struct IkarusToggleProperty * property, IkarusErrorData * error_out) {
|
||||
return ikarus::util::get_default_value<IkarusToggleProperty>(property, error_out);
|
||||
}
|
||||
|
||||
void ikarus_toggle_property_set_default_value(
|
||||
struct IkarusToggleProperty * property,
|
||||
struct IkarusToggleValue * new_default_value,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
ikarus::util::set_default_value<IkarusToggleProperty>(property, new_default_value, error_out);
|
||||
}
|
||||
14
src/ikarus/objects/properties/toggle_property.hpp
Normal file
14
src/ikarus/objects/properties/toggle_property.hpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/objects/properties/property_type.h>
|
||||
#include <ikarus/values/toggle_value.hpp>
|
||||
|
||||
struct IkarusToggleProperty : IkarusProperty {
|
||||
public:
|
||||
using value_type = IkarusToggleValue;
|
||||
constexpr auto static PropertyType = IkarusPropertyType_Toggle;
|
||||
|
||||
public:
|
||||
IkarusToggleProperty(struct IkarusProject * project, IkarusId id);
|
||||
};
|
||||
95
src/ikarus/objects/properties/util.hpp
Normal file
95
src/ikarus/objects/properties/util.hpp
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#pragma once
|
||||
|
||||
#include <boost/type_index.hpp>
|
||||
|
||||
#include <cppbase/strings.hpp>
|
||||
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/objects/properties/property.h>
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/objects/properties/property_scope.hpp>
|
||||
#include <ikarus/objects/util.hpp>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
namespace ikarus::util {
|
||||
template<typename T>
|
||||
T * create_property(
|
||||
struct IkarusProject * project,
|
||||
char const * name,
|
||||
struct IkarusPropertyScope * property_scope,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(property_scope, nullptr);
|
||||
IKARUS_FAIL_IF_PROPERTY_SCOPE_INVALID(property_scope, nullptr);
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, project, property_scope, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
IkarusId const id,
|
||||
nullptr,
|
||||
"failed to create property: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->transact([name, property_scope](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
|
||||
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);
|
||||
TRY(db->execute(
|
||||
"INSERT INTO `properties`(`id`, `type`, `source`) VALUES(?, ?, ?)",
|
||||
id,
|
||||
T::PropertyType,
|
||||
property_scope->get_id()
|
||||
));
|
||||
return cppbase::ok(id);
|
||||
})
|
||||
);
|
||||
|
||||
auto * ret = dynamic_cast<T *>(project->get_property(id, T::PropertyType));
|
||||
|
||||
IKARUS_FAIL_IF(
|
||||
ret == nullptr,
|
||||
nullptr,
|
||||
fmt::format("created {} cannot be casted down from IkarusProject", boost::typeindex::type_id<T>().pretty_name()),
|
||||
IkarusErrorInfo_LibIkarus_InvalidState
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename T::value_type * get_default_value(IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
auto * value = ikarus_property_get_default_value(property, error_out);
|
||||
IKARUS_FAIL_IF_ERROR(nullptr);
|
||||
|
||||
auto * ret = boost::variant2::get_if<typename T::value_type *>(&value->data);
|
||||
|
||||
IKARUS_FAIL_IF(
|
||||
ret == nullptr,
|
||||
nullptr,
|
||||
fmt::format(
|
||||
"{} default value is not a(n) {}",
|
||||
boost::typeindex::type_id<T>().pretty_name(),
|
||||
boost::typeindex::type_id<typename T::value_type>().pretty_name()
|
||||
),
|
||||
IkarusErrorInfo_Database_InvalidState
|
||||
);
|
||||
|
||||
return *ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void set_default_value(T * property, typename T::value_type * new_default_value, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(property, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, );
|
||||
IKARUS_FAIL_IF_NULL(new_default_value, );
|
||||
|
||||
auto value_json_str = boost::json::serialize(new_default_value->to_json());
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to set property default value: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
property->project->db->execute("UPDATE `properties` SET `default_value` = ? WHERE `id` = ?", value_json_str, property->id)
|
||||
);
|
||||
}
|
||||
|
||||
} // namespace ikarus::util
|
||||
125
src/ikarus/objects/util.hpp
Normal file
125
src/ikarus/objects/util.hpp
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <cppbase/strings.hpp>
|
||||
|
||||
#include <ikarus/errors.h>
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/objects/blueprint.hpp>
|
||||
#include <ikarus/objects/entity.hpp>
|
||||
#include <ikarus/objects/object.hpp>
|
||||
#include <ikarus/objects/properties/property.h>
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/objects/properties/property_scope.hpp>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
|
||||
namespace ikarus::util {
|
||||
|
||||
template<typename O>
|
||||
[[nodiscard]] IkarusProject * object_get_project(O const * object, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(object, nullptr);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(object, nullptr);
|
||||
|
||||
return object->project;
|
||||
}
|
||||
|
||||
template<typename O>
|
||||
[[nodiscard]] char const * object_get_name(O const * object, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(object, nullptr);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(object, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
nullptr,
|
||||
fmt::runtime(fmt::format("unable to fetch {} name: {{}}", ikarus_object_type_to_string(ikarus_id_get_object_type(object->id)))),
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
object->project->db
|
||||
->template query_one<std::string>(fmt::format("SELECT `name` FROM `{}` WHERE `id` = ?", object->get_table_name()), object->id)
|
||||
);
|
||||
|
||||
return strdup(ret.data());
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool name_is_unique(
|
||||
IkarusProject const * project,
|
||||
std::string_view name,
|
||||
[[maybe_unused]] IkarusBlueprint const * blueprint,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
bool const exists,
|
||||
false,
|
||||
"unable to check if blueprint name is unique",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_one<bool>("SELECT EXISTS(SELECT 1 FROM `blueprints` WHERE `name` = ?)", name)
|
||||
);
|
||||
|
||||
return exists;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool name_is_unique(
|
||||
IkarusProject const * project,
|
||||
std::string_view name,
|
||||
[[maybe_unused]] IkarusEntity const * entity,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
bool const exists,
|
||||
false,
|
||||
"unable to check if entity name is unique",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_one<bool>("SELECT EXISTS(SELECT 1 FROM `entities` WHERE `name` = ?)", name)
|
||||
);
|
||||
|
||||
return exists;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool
|
||||
name_is_unique(IkarusProject const * project, std::string_view name, IkarusPropertyScope const * scope, IkarusErrorData * error_out) {
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
bool const exists,
|
||||
false,
|
||||
"unable to check if property name is unique",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_one<bool>("SELECT EXISTS(SELECT 1 FROM `properties` WHERE `name` = ? AND `scope` = ?)", name, scope->get_id())
|
||||
);
|
||||
|
||||
return exists;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool
|
||||
name_is_unique(IkarusProject * project, std::string_view name, IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
std::unique_ptr<IkarusPropertyScope const> scope{ikarus_property_get_scope(property, error_out)};
|
||||
IKARUS_FAIL_IF_ERROR(false);
|
||||
|
||||
return name_is_unique(project, name, scope.get(), error_out);
|
||||
}
|
||||
|
||||
#define IKARUS_FAIL_IF_NAME_INVALID(name, project, object, ret, ...) \
|
||||
IKARUS_FAIL_IF_NULL(name, ret); \
|
||||
IKARUS_FAIL_IF(cppbase::is_empty_or_blank(name), ret, "name must not be empty", IkarusErrorInfo_Client_InvalidInput); \
|
||||
IKARUS_FAIL_IF( \
|
||||
!ikarus::util::name_is_unique(project, name, object, error_out), \
|
||||
ret, \
|
||||
"name must be unique", \
|
||||
IkarusErrorInfo_Client_InvalidInput \
|
||||
); \
|
||||
IKARUS_FAIL_IF_ERROR(ret);
|
||||
|
||||
template<typename O>
|
||||
void object_set_name(O * object, char const * name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(object, );
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(object, );
|
||||
IKARUS_FAIL_IF_NULL(name, );
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, object->project, object, );
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
fmt::runtime(fmt::format("unable to set {} name: {{}}", ikarus_object_type_to_string(ikarus_id_get_object_type(object->id)))),
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
object->project->db->execute(fmt::format("UPDATE `{}` SET `name` = ? WHERE `id` = ?", object->get_table_name()), name, object->id)
|
||||
);
|
||||
}
|
||||
|
||||
} // namespace ikarus::util
|
||||
Loading…
Add table
Add a link
Reference in a new issue