add flatbuffers support and initial rewrite

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
Folling 2024-05-12 14:15:42 +02:00 committed by Folling
parent 7e883d24eb
commit 98cb7a44ef
No known key found for this signature in database
72 changed files with 3929 additions and 1403 deletions

View file

@ -1,13 +1,5 @@
#include "ikarus/errors.h"
#include <string.h>
#include <ranges>
#include <fmt/format.h>
#include <cppbase/functional.hpp>
char const * ikarus_get_error_info_name(IkarusErrorInfo info) {
switch (info) {
case IkarusErrorInfo_None: return "None";

View file

@ -78,16 +78,3 @@ inline void safe_strcpy(char * dest, std::string_view src, size_t dest_size) {
#define IKARUS_VTRYRV_OR_FAIL(value, ret, msg, err_info, ...) \
IKARUS_VTRYRV_OR_FAIL_IMPL(CPPBASE_UNIQUE_NAME(result), value, ret, msg, err_info, __VA_ARGS__);
#define IKARUS_FAIL_IF_OBJECT_MISSING_IMPL(var_name, obj, ret) \
IKARUS_VTRYRV_OR_FAIL( \
bool const var_name, \
ret, \
"unable to check whether object exists: {}", \
IkarusErrorInfo_Database_QueryFailed, \
(obj)->project->db->template query_one<bool>("SELECT EXISTS(SELECT 1 FROM `objects` WHERE `id` = ?)", (obj)->id) \
) \
\
IKARUS_FAIL_IF(!(var_name), ret, "object does not exist", IkarusErrorInfo_Client_Misuse);
#define IKARUS_FAIL_IF_OBJECT_MISSING(obj, ret) IKARUS_FAIL_IF_OBJECT_MISSING_IMPL(CPPBASE_UNIQUE_NAME(exists), obj, ret);

View file

@ -1,7 +0,0 @@
#include "ikarus/global.h"
#include <cstdlib>
void ikarus_free(void * ptr) {
std::free(ptr);
}

View file

@ -1,14 +0,0 @@
#include "ikarus/id.h"
#include <ikarus/objects/object_type.h>
uint64_t IKARUS_ID_OBJECT_TYPE_BITS = 8;
uint64_t IKARUS_ID_OBJECT_RANDOM_BITS = sizeof(IkarusId) * 8 - IKARUS_ID_OBJECT_TYPE_BITS;
IkarusId ikarus_id_from_data_and_type(int64_t data, IkarusObjectType type) {
return data | (static_cast<IkarusId>(type) << IKARUS_ID_OBJECT_RANDOM_BITS);
}
IkarusObjectType ikarus_id_get_object_type(IkarusId id) {
return static_cast<IkarusObjectType>(id >> IKARUS_ID_OBJECT_RANDOM_BITS);
}

View file

@ -11,24 +11,27 @@
#include <ikarus/objects/properties/property.hpp>
#include <ikarus/objects/util.hpp>
#include <ikarus/persistence/project.hpp>
#include <ikarus/values/value_type.h>
IkarusBlueprint::IkarusBlueprint(IkarusProject * project, IkarusId id):
IkarusBlueprint::IkarusBlueprint(IkarusProject * project, int64_t id):
IkarusObject{project, id} {}
std::string_view IkarusBlueprint::get_table_name() const noexcept {
return "blueprints";
}
IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char const * name, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(project, nullptr);
IKARUS_FAIL_IF_NAME_INVALID_OR_DUPLICATE(name, project, static_cast<IkarusEntity const *>(nullptr), nullptr);
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
IKARUS_VTRYRV_OR_FAIL(
IkarusId const id,
int64_t 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`) VALUES(?)", IkarusObjectType_Blueprint));
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Blueprint);
TRY(db->execute("INSERT INTO `blueprints`(`id`, `name`) VALUES(?, ?)", id, name));
return cppbase::ok(id);
project->db->transact([name](auto * db) -> cppbase::Result<int64_t, sqlitecpp::TransactionError> {
TRY(db->execute("INSERT INTO `blueprints`(`name`) VALUES(?)", name));
return cppbase::ok(db->last_insert_rowid());
})
);
@ -43,13 +46,13 @@ void ikarus_blueprint_delete(IkarusBlueprint * blueprint, IkarusErrorData * erro
,
"unable to delete blueprint: {}",
IkarusErrorInfo_Database_QueryFailed,
blueprint->project->db->execute("DELETE FROM `objects` WHERE `id` = ?", blueprint->id)
blueprint->project->db->execute("DELETE FROM `blueprints` WHERE `id` = ?", blueprint->id)
);
blueprint->project->uncache(blueprint);
}
IkarusId ikarus_blueprint_get_id(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
int64_t ikarus_blueprint_get_id(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
return ikarus::util::object_get_id(blueprint, error_out);
}
@ -79,14 +82,14 @@ void ikarus_blueprint_get_properties(
return;
}
std::tuple<IkarusId, IkarusPropertyType> ids_and_types[properties_out_size];
std::tuple<int64_t, IkarusValueType> 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` = ?",
blueprint->project->db->query_many_buffered<int64_t, IkarusValueType>(
"SELECT `id` FROM `properties` WHERE `blueprint` = ?",
ids_and_types,
properties_out_size,
blueprint->id
@ -109,7 +112,7 @@ size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint, Ik
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)
blueprint->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `properties` WHERE `blueprint` = ?", blueprint->id)
);
return ret;
@ -129,13 +132,13 @@ void ikarus_blueprint_get_linked_entities(
return;
}
IkarusId ids[entities_out_size];
int64_t 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>(
blueprint->project->db->query_many_buffered<int64_t>(
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
ids,
entities_out_size,
@ -157,7 +160,8 @@ size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprin
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)
blueprint->project->db
->query_one<int64_t>("SELECT COUNT(`entity`) FROM `entity_blueprint_links` WHERE `blueprint` = ?", blueprint->id)
);
return ret;

View file

@ -2,9 +2,9 @@
#include <ikarus/objects/object.hpp>
struct IkarusBlueprint : IkarusObject {
struct IkarusBlueprint : public IkarusObject {
public:
IkarusBlueprint(struct IkarusProject * project, IkarusId id);
IkarusBlueprint(struct IkarusProject * project, int64_t id);
IkarusBlueprint(IkarusBlueprint const &) = default;
IkarusBlueprint(IkarusBlueprint &&) = default;
@ -15,7 +15,5 @@ public:
~IkarusBlueprint() override = default;
public:
inline std::string_view get_table_name() const noexcept override {
return "blueprints";
}
std::string_view get_table_name() const noexcept override;
};

View file

@ -10,22 +10,38 @@
#include <ikarus/persistence/project.hpp>
#include <ikarus/values/entity_property_value.hpp>
#include <ikarus/values/value.hpp>
#include <ikarus/values/value_type.h>
IkarusEntity * ikarus_entity_create(struct IkarusProject * project, char const * name, IkarusErrorData * error_out) {
IkarusEntity::IkarusEntity(IkarusProject * project, int64_t id):
IkarusObject{project, id} {}
std::string_view IkarusEntity::get_table_name() const noexcept {
return "entities";
}
IkarusEntity * ikarus_entity_create(
struct IkarusProject * project,
char const * name,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(project, nullptr);
IKARUS_FAIL_IF_NAME_INVALID_OR_DUPLICATE(name, project, static_cast<IkarusEntity const *>(nullptr), nullptr);
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
IKARUS_VTRYRV_OR_FAIL(
IkarusId const id,
int64_t 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);
})
project->db->transact(
[name](auto * db
) -> cppbase::Result<int64_t, sqlitecpp::TransactionError> {
TRY(db->execute(
"INSERT INTO `entities`(`name`) VALUES(?, ?)",
name
));
return cppbase::ok(db->last_insert_rowid());
}
)
);
return project->get_entity(id);
@ -39,25 +55,37 @@ void ikarus_entity_delete(IkarusEntity * entity, IkarusErrorData * error_out) {
,
"unable to delete entity: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute("DELETE FROM `objects` WHERE `id` = ?", entity->id)
entity->project->db
->execute("DELETE FROM `entities` WHERE `id` = ?", entity->id)
);
entity->project->uncache(entity);
}
IkarusId ikarus_entity_get_id(IkarusEntity const * entity, IkarusErrorData * error_out) {
int64_t
ikarus_entity_get_id(IkarusEntity const * entity, IkarusErrorData * error_out) {
return ikarus::util::object_get_id(entity, error_out);
}
IkarusProject * ikarus_entity_get_project(IkarusEntity const * entity, IkarusErrorData * error_out) {
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) {
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) {
void ikarus_entity_set_name(
IkarusEntity * entity,
char const * name,
IkarusErrorData * error_out
) {
ikarus::util::object_set_name(entity, name, error_out);
}
@ -77,7 +105,9 @@ bool ikarus_entity_is_linked_to_blueprint(
"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` = ?)",
"SELECT EXISTS(SELECT 1 FROM `entity_blueprint_links` WHERE "
"`entity` = ? AND "
"`blueprint` = ?)",
entity->id,
blueprint->id
)
@ -86,7 +116,11 @@ bool ikarus_entity_is_linked_to_blueprint(
return ret;
}
void ikarus_entity_link_to_blueprint(IkarusEntity * entity, struct IkarusBlueprint * blueprint, IkarusErrorData * error_out) {
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, );
@ -97,14 +131,20 @@ void ikarus_entity_link_to_blueprint(IkarusEntity * entity, struct IkarusBluepri
"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",
"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) {
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, );
@ -114,9 +154,26 @@ void ikarus_entity_unlink_from_blueprint(IkarusEntity * entity, struct IkarusBlu
,
"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)
entity->project->db->execute(
"DELETE FROM `entity_blueprint_links` WHERE `entity` = ? AND "
"`blueprint` = ?",
entity->id,
blueprint->id
)
);
IKARUS_TRYRV_OR_FAIL(
,
"unable to remove entity property values: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute(
"DELETE FROM `entity_property_values` WHERE `entity` = ? AND "
"`property` IN (SELECT "
"`id` FROM `properties` WHERE `blueprint` = ?)",
entity->id,
blueprint->id
)
)
}
void ikarus_entity_get_linked_blueprints(
@ -133,14 +190,15 @@ void ikarus_entity_get_linked_blueprints(
return;
}
IkarusId ids[blueprints_out_size];
int64_t 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` = ?",
entity->project->db->query_many_buffered<int64_t>(
"SELECT `blueprint` FROM `entity_blueprint_links` WHERE `entity` = "
"?",
ids,
blueprints_out_size,
entity->id
@ -152,7 +210,10 @@ void ikarus_entity_get_linked_blueprints(
}
}
size_t ikarus_entity_get_linked_blueprint_count(IkarusEntity const * entity, IkarusErrorData * error_out) {
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);
@ -161,31 +222,144 @@ size_t ikarus_entity_get_linked_blueprint_count(IkarusEntity const * entity, Ika
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)
entity->project->db->query_one<int64_t>(
"SELECT COUNT(`blueprint`) 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) {
bool ikarus_entity_has_value(
IkarusEntity const * entity,
char const * name,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, false);
IKARUS_FAIL_IF_OBJECT_MISSING(entity, false);
IKARUS_FAIL_IF_NAME_INVALID(name, false);
IKARUS_VTRYRV_OR_FAIL(
auto const has_value,
false,
"unable to check whether entity has value: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_one<bool>(
"SELECT EXISTS(SELECT 1 FROM `entity_values` WHERE `entity` = ? "
"AND `name` = ?)",
entity->id,
name
)
);
return has_value;
}
struct IkarusValue * ikarus_entity_get_value(
IkarusEntity const * entity,
char const * name,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, nullptr);
IKARUS_FAIL_IF_OBJECT_MISSING(entity, nullptr);
IKARUS_FAIL_IF_VALUE_MISSING(entity, name, nullptr);
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
auto * value = fetch_value_from_db(
entity->project,
error_out,
"SELECT `value` FROM `entity_values` WHERE `entity` = ? AND `name` = ?",
entity->id,
name
);
IKARUS_FAIL_IF_ERROR(nullptr);
return value;
}
void ikarus_entity_set_value(
IkarusEntity * entity,
char const * name,
struct IkarusValue const * value,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_FAIL_IF_NAME_INVALID(name, );
IKARUS_FAIL_IF_NULL(value, );
IKARUS_TRYRV_OR_FAIL(
,
"unable to set entity value: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute(
"INSERT INTO `entity_values`(`entity`, `name`, `value`) VALUES(?1, "
"?2, ?3) ON "
"CONFLICT(`entity`, `name`) DO UPDATE SET `value` = ?3",
entity->id,
name,
boost::json::serialize(value->to_json())
)
);
}
void ikarus_entity_delete_value(
IkarusEntity * entity,
char const * name,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, );
IKARUS_FAIL_IF_OBJECT_MISSING(entity, );
IKARUS_FAIL_IF_VALUE_MISSING(entity, name, );
IKARUS_FAIL_IF_NAME_INVALID(name, );
IKARUS_TRYRV_OR_FAIL(
,
"unable to delete entity value: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->execute(
"DELETE FROM `entity_values` WHERE `entity` = ? AND `name` = ?",
entity->id,
name
)
);
}
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);
// given that values are loaded lazily we can't just check
// `entity_property_values` here
IKARUS_VTRYRV_OR_FAIL(
auto const ret,
auto const has_property,
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` = ?)",
"SELECT EXISTS(\n"
" SELECT 1\n"
" FROM `entity_blueprint_links`\n"
" JOIN `properties` ON `properties`.`blueprint` = "
"`entity_blueprint_links`.`blueprint`\n"
" WHERE `entity_blueprint_links`.`entity` = ? AND "
"`properties`.`id` = ?\n"
")",
entity->id,
property->id
)
)
return ret;
return has_property;
}
void ikarus_entity_get_properties(
@ -202,19 +376,25 @@ void ikarus_entity_get_properties(
return;
}
std::tuple<IkarusId, IkarusPropertyType> ids_and_types[properties_out_size];
std::tuple<int64_t, IkarusValueType> ids_and_types[properties_out_size];
// given that values are loaded lazily we can't just check
// `entity_property_values` here
IKARUS_TRYRV_OR_FAIL(
,
"unable to fetch entity properties from database: {}",
"unable to fetch properties from entity: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_many_buffered<IkarusId, IkarusPropertyType>(
"SELECT `property`, `type` FROM `properties` WHERE `source` = ?",
entity->project->db->query_many_buffered<int64_t, IkarusValueType>(
"SELECT `properties`.`id`, `properties`.`type`\n"
"FROM `entity_blueprint_links`\n"
"JOIN `properties` ON `properties`.`blueprint` = "
"`entity_blueprint_links`.`blueprint`\n"
"WHERE `entity_blueprint_links`.`entity` = ?\n",
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];
@ -222,23 +402,39 @@ void ikarus_entity_get_properties(
}
}
size_t ikarus_entity_get_property_count(IkarusEntity const * entity, IkarusErrorData * error_out) {
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);
// given that values are loaded lazily we can't just check
// `entity_property_values` here
IKARUS_VTRYRV_OR_FAIL(
size_t const ret,
size_t const count,
0,
"unable to fetch entity property count from database: {}",
"unable to fetch property count from entity: {}",
IkarusErrorInfo_Database_QueryFailed,
entity->project->db->query_one<int64_t>("SELECT COUNT(*) FROM `properties` WHERE `source` = ?", entity->id)
entity->project->db->query_one<int64_t>(
"SELECT COUNT(`properties`.`id`)\n"
"FROM `entity_blueprint_links`\n"
"JOIN `properties` ON `properties`.`blueprint` = "
"`entity_blueprint_links`.`blueprint`\n"
"WHERE `entity_blueprint_links`.`entity` = ?\n"
")",
entity->id
)
);
return ret;
return count;
}
struct IkarusEntityPropertyValue *
ikarus_entity_get_value(IkarusEntity const * entity, struct IkarusProperty const * property, IkarusErrorData * error_out) {
struct IkarusValue * ikarus_entity_get_property_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);
@ -247,25 +443,27 @@ ikarus_entity_get_value(IkarusEntity const * entity, struct IkarusProperty const
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` = ?))",
"SELECT IFNULL(\n"
" (\n"
" SELECT `value`\n"
" FROM `entity_property_values`\n"
" WHERE `entity` = ?1 AND `property` = ?2\n"
" ),\n"
" (SELECT `default_value` FROM `properties` WHERE `id` = ?2)\n"
")",
entity->id,
property->id,
property->id
);
IKARUS_FAIL_IF_ERROR(nullptr);
return new IkarusEntityPropertyValue{
.entity = entity,
.property = property,
.value = value,
};
return value;
}
void ikarus_entity_set_value(
void ikarus_entity_set_property_value(
IkarusEntity * entity,
struct IkarusProperty const * property,
struct IkarusEntityPropertyValue * value,
struct IkarusValue * value,
IkarusErrorData * error_out
) {
IKARUS_FAIL_IF_NULL(entity, );
@ -274,18 +472,17 @@ void ikarus_entity_set_value(
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` = ?",
"INSERT INTO `entity_property_values`(`entity`, `property`, "
"`value`) VALUES(?1, ?2, "
"?3) ON CONFLICT(`entity`, `property`) DO UPDATE SET `value` = ?3",
entity->id,
property->id,
value_json_str,
value_json_str
boost::json::serialize(value->to_json())
)
);
}

View file

@ -2,10 +2,9 @@
#include <ikarus/objects/object.hpp>
struct IkarusEntity : IkarusObject {
struct IkarusEntity : public IkarusObject {
public:
inline IkarusEntity(struct IkarusProject * project, IkarusId id):
IkarusObject{project, id} {}
inline IkarusEntity(struct IkarusProject * project, int64_t id);
IkarusEntity(IkarusEntity const &) = default;
IkarusEntity(IkarusEntity &&) = default;
@ -16,7 +15,5 @@ public:
~IkarusEntity() override = default;
public:
inline std::string_view get_table_name() const noexcept override {
return "entities";
}
std::string_view get_table_name() const noexcept override;
};

View file

@ -1,93 +1,7 @@
#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):
IkarusObject::IkarusObject(IkarusProject * project, int64_t 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()
}

View file

@ -2,11 +2,9 @@
#include <string_view>
#include <ikarus/id.h>
struct IkarusObject {
class IkarusObject {
public:
IkarusObject(struct IkarusProject * project, IkarusId id);
IkarusObject(struct IkarusProject * project, int64_t id);
IkarusObject(IkarusObject const &) = default;
IkarusObject(IkarusObject &&) = default;
@ -21,5 +19,18 @@ public:
public:
struct IkarusProject * project;
IkarusId id;
int64_t id;
};
#define IKARUS_FAIL_IF_OBJECT_MISSING_IMPL(var_name, obj, ret) \
IKARUS_VTRYRV_OR_FAIL( \
bool const var_name, \
ret, \
"unable to check whether object exists: {}", \
IkarusErrorInfo_Database_QueryFailed, \
(obj)->project->db->template query_one<bool>("SELECT EXISTS(SELECT 1 FROM `objects` WHERE `id` = ?)", (obj)->id) \
) \
\
IKARUS_FAIL_IF(!(var_name), ret, "object does not exist", IkarusErrorInfo_Client_Misuse);
#define IKARUS_FAIL_IF_OBJECT_MISSING(obj, ret) IKARUS_FAIL_IF_OBJECT_MISSING_IMPL(CPPBASE_UNIQUE_NAME(exists), obj, ret);

View file

@ -1,12 +0,0 @@
#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";
}

View file

@ -7,7 +7,7 @@
#include <ikarus/objects/properties/util.hpp>
#include <ikarus/values/value.hpp>
IkarusNumberProperty::IkarusNumberProperty(IkarusProject * project, IkarusId id):
IkarusNumberProperty::IkarusNumberProperty(IkarusProject * project, int64_t id):
IkarusProperty{project, id, this} {}
IkarusNumberProperty * ikarus_number_property_create(

View file

@ -1,14 +1,14 @@
#pragma once
#include <ikarus/objects/properties/property.hpp>
#include <ikarus/objects/properties/property_type.h>
#include <ikarus/values/number_value.hpp>
#include <ikarus/values/value_type.h>
struct IkarusNumberProperty : IkarusProperty {
struct IkarusNumberProperty : public IkarusProperty {
public:
using value_type = IkarusNumberValue;
constexpr auto static PropertyType = IkarusPropertyType_Number;
constexpr auto static PropertyType = IkarusValueType_Number;
public:
IkarusNumberProperty(struct IkarusProject * project, IkarusId id);
IkarusNumberProperty(struct IkarusProject * project, int64_t id);
};

View file

@ -5,12 +5,12 @@
#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/values/value_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):
IkarusProperty::IkarusProperty(IkarusProject * project, int64_t id, Data data):
IkarusObject{project, id},
data{data} {}
@ -28,7 +28,7 @@ IKA_API void ikarus_property_delete(IkarusProperty * property, IkarusErrorData *
property->project->uncache(property);
}
IkarusId ikarus_property_get_id(IkarusProperty const * property, IkarusErrorData * error_out) {
int64_t ikarus_property_get_id(IkarusProperty const * property, IkarusErrorData * error_out) {
return ikarus::util::object_get_id(property, error_out);
}
@ -44,19 +44,19 @@ void ikarus_property_set_name(IkarusProperty * property, char const * name, Ikar
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);
IkarusValueType ikarus_property_get_type(IkarusProperty const * property, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(property, IkarusValueType_Toggle);
IKARUS_FAIL_IF_OBJECT_MISSING(property, IkarusValueType_Toggle);
IKARUS_VTRYRV_OR_FAIL(
auto const ret,
IkarusPropertyType_Toggle,
IkarusValueType_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);
return static_cast<IkarusValueType>(ret);
}
IkarusPropertyScope const * ikarus_property_get_scope(IkarusProperty const * property, IkarusErrorData * error_out) {
@ -68,7 +68,7 @@ IkarusPropertyScope const * ikarus_property_get_scope(IkarusProperty const * pro
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)
property->project->db->query_one<int64_t>("SELECT `source` FROM `properties` WHERE `id` = ?", property->id)
);
switch (ikarus_id_get_object_type(source)) {
@ -83,7 +83,7 @@ IkarusPropertyScope const * ikarus_property_get_scope(IkarusProperty const * pro
}
}
IkarusValue * ikarus_property_get_default_value(IkarusProperty const * property, IkarusErrorData * error_out) {
IkarusValueData * ikarus_property_get_default_value(IkarusProperty const * property, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(property, nullptr);
IKARUS_FAIL_IF_OBJECT_MISSING(property, nullptr);

View file

@ -4,12 +4,12 @@
#include <ikarus/objects/object.hpp>
struct IkarusProperty : IkarusObject {
struct IkarusProperty : public IkarusObject {
public:
using Data = std::variant<struct IkarusToggleProperty *, struct IkarusNumberProperty *, struct IkarusTextProperty *>;
public:
IkarusProperty(struct IkarusProject * project, IkarusId id, Data data);
IkarusProperty(struct IkarusProject * project, int64_t id, Data data);
IkarusProperty(IkarusProperty const &) = default;
IkarusProperty(IkarusProperty &&) = default;

View file

@ -1,57 +0,0 @@
#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
);
}

View file

@ -1,39 +0,0 @@
#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;
};

View file

@ -7,7 +7,7 @@
#include <ikarus/objects/properties/util.hpp>
#include <ikarus/values/value.hpp>
IkarusTextProperty::IkarusTextProperty(IkarusProject * project, IkarusId id):
IkarusTextProperty::IkarusTextProperty(IkarusProject * project, int64_t id):
IkarusProperty{project, id, this} {}
IkarusTextProperty * ikarus_text_property_create(

View file

@ -1,14 +1,14 @@
#pragma once
#include <ikarus/objects/properties/property.hpp>
#include <ikarus/objects/properties/property_type.h>
#include <ikarus/values/value_type.h>
#include <ikarus/values/text_value.hpp>
struct IkarusTextProperty : IkarusProperty {
struct IkarusTextProperty : public IkarusProperty {
public:
using value_type = IkarusTextValue;
constexpr auto static PropertyType = IkarusPropertyType_Text;
constexpr auto static PropertyType = IkarusValueType_Text;
public:
IkarusTextProperty(struct IkarusProject * project, IkarusId id);
IkarusTextProperty(struct IkarusProject * project, int64_t id);
};

View file

@ -7,7 +7,7 @@
#include <ikarus/objects/properties/util.hpp>
#include <ikarus/values/value.hpp>
IkarusToggleProperty::IkarusToggleProperty(IkarusProject * project, IkarusId id):
IkarusToggleProperty::IkarusToggleProperty(IkarusProject * project, int64_t id):
IkarusProperty{project, id, this} {}
IkarusToggleProperty * ikarus_toggle_property_create(

View file

@ -1,14 +1,17 @@
#pragma once
#include <ikarus/objects/properties/property.hpp>
#include <ikarus/objects/properties/property_type.h>
#include <ikarus/values/toggle_value.hpp>
#include <ikarus/values/value_type.h>
struct IkarusToggleProperty : IkarusProperty {
struct IkarusToggleProperty {
public:
using value_type = IkarusToggleValue;
constexpr auto static PropertyType = IkarusPropertyType_Toggle;
constexpr auto static PropertyType = IkarusValueType_Toggle;
public:
IkarusToggleProperty(struct IkarusProject * project, IkarusId id);
IkarusToggleProperty(struct IkarusProject * project, int64_t id);
public:
IkarusProperty * property;
};

View file

@ -22,15 +22,14 @@ T * create_property(
) {
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_OR_DUPLICATE(name, project, property_scope, nullptr);
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
IKARUS_VTRYRV_OR_FAIL(
IkarusId const id,
int64_t const id,
nullptr,
"failed to create property: {}",
IkarusErrorInfo_Database_QueryFailed,
project->db->transact([name, property_scope](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
project->db->transact([name, property_scope](auto * db) -> cppbase::Result<int64_t, sqlitecpp::TransactionError> {
TRY(db->execute("INSERT INTO `objects`(`type`) VALUES(?)", IkarusObjectType_Property));
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Property);
TRY(db->execute(

View file

@ -6,18 +6,12 @@
#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]] IkarusId object_get_id(O const * object, IkarusErrorData * error_out) {
[[nodiscard]] int64_t object_get_id(O const * object, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(object, 0);
IKARUS_FAIL_IF_OBJECT_MISSING(object, 0);
@ -49,81 +43,14 @@ template<typename O>
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 const * 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, 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);
#define IKARUS_FAIL_IF_NAME_INVALID_OR_DUPLICATE(name, project, object, ret, ...) \
IKARUS_FAIL_IF_NAME_INVALID(name, ret); \
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);
#define IKARUS_FAIL_IF_NAME_INVALID(name, ret) IKARUS_FAIL_IF_NULL(name, 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_OR_DUPLICATE(name, object->project, object, );
IKARUS_FAIL_IF_NAME_INVALID(name, );
IKARUS_TRYRV_OR_FAIL(
,

View file

@ -8,11 +8,12 @@
#include <sqlitecpp/connection.hpp>
namespace ikarus {
CPPBASE_ASSET(m0_initial_layout, "ikarus/persistence/migrations/m0_initial_layout.sql");
class Migration : public sqlitecpp::Migration {
public:
Migration(char const * sql, size_t size):
Migration(char const * sql, size_t const size):
sql{sql, size} {}
~Migration() override = default;

View file

@ -1,68 +1,51 @@
CREATE TABLE `objects`
(
`do_not_access_rowid_alias` INTEGER PRIMARY KEY,
`type` INT NOT NULL,
`id` INT GENERATED ALWAYS AS (`do_not_access_rowid_alias` | (`type` << 56)) VIRTUAL UNIQUE
) STRICT;
CREATE UNIQUE INDEX `object_id` ON `objects` (`id`);
CREATE INDEX `object_type` ON `objects` (`type`);
CREATE TABLE `entities`
(
`id` INT,
`name` TEXT NOT NULL,
`id` INTEGER PRIMARY KEY,
`name` TEXT NOT NULL
) STRICT;
PRIMARY KEY (`id`),
UNIQUE (`name`),
FOREIGN KEY (`id`) REFERENCES `objects` (`id`) ON DELETE CASCADE
CREATE TABLE `entity_values`
(
`entity` INTEGER NOT NULL REFERENCES `entities` (`id`) ON DELETE CASCADE,
`name` TEXT NOT NULL,
`type` INT NOT NULL,
`value` TEXT NOT NULL,
PRIMARY KEY (`entity`, `name`)
) WITHOUT ROWID, STRICT;
CREATE TABLE `blueprints`
(
`id` INT,
`name` TEXT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`name`),
FOREIGN KEY (`id`) REFERENCES `objects` (`id`) ON DELETE CASCADE
) WITHOUT ROWID, STRICT;
CREATE TABLE `entity_blueprint_links`
(
`entity` INT NOT NULL,
`blueprint` INT NOT NULL,
PRIMARY KEY (`entity`, `blueprint`),
FOREIGN KEY (`entity`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`blueprint`) REFERENCES `blueprints` (`id`) ON DELETE CASCADE
) WITHOUT ROWID, STRICT;
`id` INTEGER PRIMARY KEY,
`name` TEXT NOT NULL
) STRICT;
CREATE TABLE `properties`
(
`id` INT,
`name` TEXT NOT NULL,
`type` INT NOT NULL,
`default_value` TEXT NOT NULL,
`settings` TEXT NOT NULL,
`source` INT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`source`, `name`),
FOREIGN KEY (`id`) REFERENCES `objects` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`source`) REFERENCES `objects` (`id`) ON DELETE CASCADE
) WITHOUT ROWID, STRICT;
`id` INTEGER PRIMARY KEY,
`blueprint` INTEGER NOT NULL REFERENCES `blueprints` (`id`) ON DELETE CASCADE,
`name` TEXT NOT NULL,
`type` INT NOT NULL,
`cardinality` INT NOT NULL,
`default_value` TEXT NOT NULL,
`settings` TEXT NOT NULL
) STRICT;
CREATE INDEX `properties_type` ON `properties` (`type`);
CREATE INDEX `properties_source` ON `properties` (`source`);
CREATE TABLE `values`
CREATE TABLE `entity_blueprint_links`
(
`entity` INT NOT NULL,
`property` INT NOT NULL,
`value` TEXT NOT NULL,
`entity` INTEGER NOT NULL REFERENCES `entities` (`id`) ON DELETE CASCADE,
`blueprint` INTEGER NOT NULL REFERENCES `blueprints` (`id`) ON DELETE CASCADE,
PRIMARY KEY (`entity`, `property`),
FOREIGN KEY (`entity`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`property`) REFERENCES `properties` (`id`) ON DELETE CASCADE
PRIMARY KEY (`entity`, `blueprint`)
) STRICT;
CREATE TABLE `entity_property_values`
(
`entity` INTEGER NOT NULL REFERENCES `entities` (`id`) ON DELETE CASCADE,
`property` INTEGER NOT NULL REFERENCES `properties` (`id`) ON DELETE CASCADE,
`value` TEXT NOT NULL,
PRIMARY KEY (`entity`, `property`)
) WITHOUT ROWID, STRICT;

View file

@ -22,7 +22,7 @@ IkarusProject::IkarusProject(std::string_view name, std::string_view path, std::
_properties{},
_entities{} {}
IkarusBlueprint * IkarusProject::get_blueprint(IkarusId id) {
IkarusBlueprint * IkarusProject::get_blueprint(int64_t id) {
return get_cached_object<IkarusBlueprint>(id, this->_blueprints);
}
@ -30,7 +30,7 @@ auto IkarusProject::uncache(IkarusBlueprint * blueprint) -> void {
remove_cached_object(blueprint, _blueprints);
}
auto IkarusProject::get_entity(IkarusId id) -> IkarusEntity * {
auto IkarusProject::get_entity(int64_t id) -> IkarusEntity * {
return get_cached_object<IkarusEntity>(id, this->_entities);
}
@ -38,16 +38,16 @@ auto IkarusProject::uncache(IkarusEntity * entity) -> void {
remove_cached_object(entity, _entities);
}
auto IkarusProject::get_property(IkarusId id, IkarusPropertyType type) -> IkarusProperty * {
auto IkarusProject::get_property(int64_t id, IkarusValueType type) -> IkarusProperty * {
auto const iter = _properties.find(id);
if (iter == _properties.cend()) {
switch (type) {
case IkarusPropertyType_Toggle:
case IkarusValueType_Toggle:
return _properties.emplace(id, std::make_unique<IkarusToggleProperty>(this, id)).first->second.get();
case IkarusPropertyType_Number:
case IkarusValueType_Number:
return _properties.emplace(id, std::make_unique<IkarusNumberProperty>(this, id)).first->second.get();
case IkarusPropertyType_Text: return _properties.emplace(id, std::make_unique<IkarusTextProperty>(this, id)).first->second.get();
case IkarusValueType_Text: return _properties.emplace(id, std::make_unique<IkarusTextProperty>(this, id)).first->second.get();
}
}
@ -212,13 +212,13 @@ void ikarus_project_get_blueprints(
return;
}
IkarusId ids[blueprints_out_size];
int64_t ids[blueprints_out_size];
IKARUS_TRYRV_OR_FAIL(
,
"unable to fetch project blueprints from database: {}",
IkarusErrorInfo_Database_QueryFailed,
project->db->query_many_buffered<IkarusId>("SELECT `id` FROM `blueprints`", ids, blueprints_out_size)
project->db->query_many_buffered<int64_t>("SELECT `id` FROM `blueprints`", ids, blueprints_out_size)
);
for (size_t i = 0; i < blueprints_out_size; ++i) {
@ -253,13 +253,13 @@ void ikarus_project_get_entities(
return;
}
IkarusId ids[entities_out_size];
int64_t ids[entities_out_size];
IKARUS_TRYRV_OR_FAIL(
,
"unable to fetch project entities from database: {}",
IkarusErrorInfo_Database_QueryFailed,
project->db->query_many_buffered<IkarusId>("SELECT `id` FROM `entities`", ids, entities_out_size)
project->db->query_many_buffered<int64_t>("SELECT `id` FROM `entities`", ids, entities_out_size)
);
for (size_t i = 0; i < entities_out_size; ++i) {
@ -293,7 +293,7 @@ struct IkarusEntity * get_entity_by_name(IkarusProject * project, char const * n
nullptr,
"unable to find entity in database: {}",
IkarusErrorInfo_Client_InvalidInput,
project->db->query_one<IkarusId>("SELECT `id` FROM `entities` WHERE `name` = ?", name)
project->db->query_one<int64_t>("SELECT `id` FROM `entities` WHERE `name` = ?", name)
);
return project->get_entity(id);
@ -310,7 +310,7 @@ get_property_by_name(IkarusProject * project, IkarusPropertyScope * scope, char
nullptr,
"unable to find property in database: {}",
IkarusErrorInfo_Client_InvalidInput,
project->db->query_one<IkarusId, IkarusPropertyType>(
project->db->query_one<int64_t, IkarusValueType>(
"SELECT `id`, `type` FROM `properties` WHERE `name` = ? AND `scope` = ?",
name,
scope->get_id()
@ -332,7 +332,7 @@ IkarusBlueprint * get_blueprints_by_name(IkarusProject * project, char const * n
nullptr,
"unable to find blueprint in database: {}",
IkarusErrorInfo_Client_InvalidInput,
project->db->query_one<IkarusId>("SELECT `id` FROM `blueprints` WHERE `name` = ?", name)
project->db->query_one<int64_t>("SELECT `id` FROM `blueprints` WHERE `name` = ?", name)
);
return project->get_blueprint(id);

View file

@ -1,7 +1,5 @@
#pragma once
#include <ranges>
#include <stack>
#include <string>
#include <boost/filesystem.hpp>
@ -9,8 +7,7 @@
#include <sqlitecpp/connection.hpp>
#include <ikarus/errors.h>
#include <ikarus/id.h>
#include <ikarus/objects/properties/property_type.h>
#include <ikarus/values/value_type.h>
namespace fs = boost::filesystem;
@ -20,19 +17,19 @@ public:
IkarusProject(std::string_view name, std::string_view path, std::unique_ptr<sqlitecpp::Connection> && db);
public:
[[nodiscard]] auto get_blueprint(IkarusId id) -> struct IkarusBlueprint *;
[[nodiscard]] auto get_blueprint(int64_t id) -> struct IkarusBlueprint *;
auto uncache(struct IkarusBlueprint * blueprint) -> void;
[[nodiscard]] auto get_entity(IkarusId id) -> struct IkarusEntity *;
[[nodiscard]] auto get_entity(int64_t id) -> struct IkarusEntity *;
auto uncache(struct IkarusEntity * entity) -> void;
// TODO improve this to take a template param so that we don't have to cast in e.g. ikarus_toggle_property_create
[[nodiscard]] auto get_property(IkarusId id, IkarusPropertyType type) -> struct IkarusProperty *;
[[nodiscard]] auto get_property(int64_t id, IkarusValueType type) -> struct IkarusProperty *;
auto uncache(struct IkarusProperty * property) -> void;
private:
template<typename T>
[[nodiscard]] T * get_cached_object(IkarusId id, auto & cache) {
[[nodiscard]] T * get_cached_object(int64_t id, auto & cache) {
auto const iter = cache.find(id);
if (iter == cache.cend()) {
@ -43,7 +40,7 @@ private:
}
template<typename T>
void remove_cached_object(T * object, std::unordered_map<IkarusId, std::unique_ptr<T>> & cache) {
void remove_cached_object(T * object, std::unordered_map<int64_t, std::unique_ptr<T>> & cache) {
cache.erase(object->id);
}
@ -53,9 +50,9 @@ public:
std::unique_ptr<sqlitecpp::Connection> db;
private:
std::unordered_map<IkarusId, std::unique_ptr<struct IkarusBlueprint>> mutable _blueprints;
std::unordered_map<IkarusId, std::unique_ptr<struct IkarusProperty>> mutable _properties;
std::unordered_map<IkarusId, std::unique_ptr<struct IkarusEntity>> mutable _entities;
std::unordered_map<int64_t, std::unique_ptr<struct IkarusBlueprint>> mutable _blueprints;
std::unordered_map<int64_t, std::unique_ptr<struct IkarusProperty>> mutable _properties;
std::unordered_map<int64_t, std::unique_ptr<struct IkarusEntity>> mutable _entities;
};
constexpr std::string_view DB_PROJECT_NAME_KEY = "PROJECT_NAME";

View file

@ -1,7 +1 @@
#pragma once
struct IkarusEntityPropertyValue {
struct IkarusEntity const * entity;
struct IkarusProperty const * property;
struct IkarusValue const * value;
};

View file

@ -6,60 +6,60 @@
#include <ikarus/values/value_base.hpp>
IkarusNumberValue::IkarusNumberValue():
IkarusValue{this} {}
IkarusValueData{this} {}
IkarusNumberValue * ikarus_number_value_create() {
IkarusNumberValue * ikarus_number_value_data_create() {
return new IkarusNumberValue{};
}
double const * ikarus_number_value_get(IkarusNumberValue * value, size_t idx) {
return ikarus_value_base_get(value, idx);
double const * ikarus_number_value_data_get(IkarusNumberValue * value, size_t idx) {
return ikarus_value_data_base_get(value, idx);
}
size_t ikarus_number_value_get_size(IkarusNumberValue const * value) {
return ikarus_value_base_get_size(value);
size_t ikarus_number_value_data_get_size(IkarusNumberValue const * value) {
return ikarus_value_data_base_get_size(value);
}
void ikarus_number_value_set(IkarusNumberValue * value, size_t idx, double new_data) {
return ikarus_value_base_set(value, idx, new_data);
void ikarus_number_value_data_set(IkarusNumberValue * value, size_t idx, double new_data) {
return ikarus_value_data_base_set(value, idx, new_data);
}
void ikarus_number_value_remove(IkarusNumberValue * value, size_t idx) {
return ikarus_value_base_remove(value, idx);
void ikarus_number_value_data_remove(IkarusNumberValue * value, size_t idx) {
return ikarus_value_data_base_remove(value, idx);
}
void ikarus_number_value_insert(IkarusNumberValue * value, size_t idx, long double new_data) {
return ikarus_value_base_insert(value, idx, new_data);
void ikarus_number_value_data_insert(IkarusNumberValue * value, size_t idx, long double new_data) {
return ikarus_value_data_base_insert(value, idx, new_data);
}
void ikarus_number_value_clear(IkarusNumberValue * value) {
return ikarus_value_base_clear(value);
void ikarus_number_value_data_clear(IkarusNumberValue * value) {
return ikarus_value_data_base_clear(value);
}
bool ikarus_number_value_is_undefined(IkarusNumberValue const * value) {
return ikarus_value_base_is_undefined(value);
bool ikarus_number_value_data_is_undefined(IkarusNumberValue const * value) {
return ikarus_value_data_base_is_undefined(value);
}
void ikarus_number_value_set_undefined(IkarusNumberValue * value, bool undefined) {
return ikarus_value_base_set_undefined(value, undefined);
void ikarus_number_value_data_set_undefined(IkarusNumberValue * value, bool undefined) {
return ikarus_value_data_base_set_undefined(value, undefined);
}
char const * ikarus_number_value_to_string(IkarusNumberValue const * value) {
return ikarus_value_base_to_string(value, [](auto const & value) { return value; });
char const * ikarus_number_value_data_to_string(IkarusNumberValue const * value) {
return ikarus_value_data_base_to_string(value, [](auto const & value) { return value; });
}
bool ikarus_number_value_is_equal(IkarusNumberValue const * lhs, IkarusNumberValue const * rhs) {
return ikarus_value_base_is_equal(lhs, rhs);
bool ikarus_number_value_data_is_equal(IkarusNumberValue const * lhs, IkarusNumberValue const * rhs) {
return ikarus_value_data_base_is_equal(lhs, rhs);
}
IkarusNumberValue * ikarus_number_value_copy(IkarusNumberValue const * value) {
return ikarus_value_base_copy(value);
IkarusNumberValue * ikarus_number_value_data_copy(IkarusNumberValue const * value) {
return ikarus_value_data_base_copy(value);
}
struct IkarusValue * ikarus_number_value_to_value(IkarusNumberValue * value) {
return ikarus_value_base_to_value(value);
struct IkarusValueData * ikarus_number_value_data_to_value(IkarusNumberValue * value) {
return ikarus_value_data_base_to_value(value);
}
struct IkarusValue const * ikarus_number_value_to_value_const(IkarusNumberValue const * value) {
return ikarus_value_base_to_value_const(value);
struct IkarusValueData const * ikarus_number_value_data_to_value_data_const(IkarusNumberValue const * value) {
return ikarus_value_data_base_to_value_data_const(value);
}

View file

@ -5,7 +5,7 @@
#include <ikarus/values/value.hpp>
struct IkarusNumberValue : IkarusValue {
struct IkarusNumberValue : IkarusValueData {
public:
using DataType = double;

View file

@ -7,60 +7,60 @@
#include <ikarus/values/value_base.hpp>
IkarusTextValue::IkarusTextValue():
IkarusValue{this} {}
IkarusValueData{this} {}
IkarusTextValue * ikarus_text_value_create() {
IkarusTextValue * ikarus_text_value_data_create() {
return new IkarusTextValue{};
}
char const * ikarus_text_value_get(IkarusTextValue * value, size_t idx) {
return ikarus_value_base_get(value, idx)->data();
char const * ikarus_text_value_data_get(IkarusTextValue * value, size_t idx) {
return ikarus_value_data_base_get(value, idx)->data();
}
size_t ikarus_text_value_get_size(IkarusTextValue const * value) {
return ikarus_value_base_get_size(value);
size_t ikarus_text_value_data_get_size(IkarusTextValue const * value) {
return ikarus_value_data_base_get_size(value);
}
void ikarus_text_value_set(IkarusTextValue * value, size_t idx, char const * new_data) {
return ikarus_value_base_set(value, idx, new_data);
void ikarus_text_value_data_set(IkarusTextValue * value, size_t idx, char const * new_data) {
return ikarus_value_data_base_set(value, idx, new_data);
}
void ikarus_text_value_remove(IkarusTextValue * value, size_t idx) {
return ikarus_value_base_remove(value, idx);
void ikarus_text_value_data_remove(IkarusTextValue * value, size_t idx) {
return ikarus_value_data_base_remove(value, idx);
}
void ikarus_text_value_insert(IkarusTextValue * value, size_t idx, char const * new_data) {
return ikarus_value_base_insert(value, idx, new_data);
void ikarus_text_value_data_insert(IkarusTextValue * value, size_t idx, char const * new_data) {
return ikarus_value_data_base_insert(value, idx, new_data);
}
void ikarus_text_value_clear(IkarusTextValue * value) {
return ikarus_value_base_clear(value);
void ikarus_text_value_data_clear(IkarusTextValue * value) {
return ikarus_value_data_base_clear(value);
}
bool ikarus_text_value_is_undefined(IkarusTextValue const * value) {
return ikarus_value_base_is_undefined(value);
bool ikarus_text_value_data_is_undefined(IkarusTextValue const * value) {
return ikarus_value_data_base_is_undefined(value);
}
void ikarus_text_value_set_undefined(IkarusTextValue * value, bool undefined) {
return ikarus_value_base_set_undefined(value, undefined);
void ikarus_text_value_data_set_undefined(IkarusTextValue * value, bool undefined) {
return ikarus_value_data_base_set_undefined(value, undefined);
}
char const * ikarus_text_value_to_string(IkarusTextValue const * value) {
return ikarus_value_base_to_string(value, [](auto const & value) { return value; });
char const * ikarus_text_value_data_to_string(IkarusTextValue const * value) {
return ikarus_value_data_base_to_string(value, [](auto const & value) { return value; });
}
bool ikarus_text_value_is_equal(IkarusTextValue const * lhs, IkarusTextValue const * rhs) {
return ikarus_value_base_is_equal(lhs, rhs);
bool ikarus_text_value_data_is_equal(IkarusTextValue const * lhs, IkarusTextValue const * rhs) {
return ikarus_value_data_base_is_equal(lhs, rhs);
}
IkarusTextValue * ikarus_text_value_copy(IkarusTextValue const * value) {
return ikarus_value_base_copy(value);
IkarusTextValue * ikarus_text_value_data_copy(IkarusTextValue const * value) {
return ikarus_value_data_base_copy(value);
}
struct IkarusValue * ikarus_text_value_to_value(IkarusTextValue * value) {
return ikarus_value_base_to_value(value);
struct IkarusValueData * ikarus_text_value_data_to_value(IkarusTextValue * value) {
return ikarus_value_data_base_to_value(value);
}
struct IkarusValue const * ikarus_text_value_to_value_const(IkarusTextValue const * value) {
return ikarus_value_base_to_value_const(value);
struct IkarusValueData const * ikarus_text_value_data_to_value_data_const(IkarusTextValue const * value) {
return ikarus_value_data_base_to_value_data_const(value);
}

View file

@ -4,7 +4,7 @@
#include <ikarus/values/value.hpp>
struct IkarusTextValue : IkarusValue {
struct IkarusTextValue : IkarusValueData {
public:
using DataType = std::string;

View file

@ -7,60 +7,60 @@
#include <ikarus/values/value_base.hpp>
IkarusToggleValue::IkarusToggleValue():
IkarusValue{this} {}
IkarusValueData{this} {}
IkarusToggleValue * ikarus_toggle_value_create() {
IkarusToggleValue * ikarus_toggle_value_data_create() {
return new IkarusToggleValue{};
}
bool const * ikarus_toggle_value_get(IkarusToggleValue * value, size_t idx) {
return ikarus_value_base_get(value, idx);
bool const * ikarus_toggle_value_data_get(IkarusToggleValue * value, size_t idx) {
return ikarus_value_data_base_get(value, idx);
}
size_t ikarus_toggle_value_get_size(IkarusToggleValue const * value) {
return ikarus_value_base_get_size(value);
size_t ikarus_toggle_value_data_get_size(IkarusToggleValue const * value) {
return ikarus_value_data_base_get_size(value);
}
void ikarus_toggle_value_set(IkarusToggleValue * value, size_t idx, bool new_data) {
return ikarus_value_base_set(value, idx, new_data);
void ikarus_toggle_value_data_set(IkarusToggleValue * value, size_t idx, bool new_data) {
return ikarus_value_data_base_set(value, idx, new_data);
}
void ikarus_toggle_value_remove(IkarusToggleValue * value, size_t idx) {
return ikarus_value_base_remove(value, idx);
void ikarus_toggle_value_data_remove(IkarusToggleValue * value, size_t idx) {
return ikarus_value_data_base_remove(value, idx);
}
void ikarus_toggle_value_insert(IkarusToggleValue * value, size_t idx, bool new_data) {
return ikarus_value_base_insert(value, idx, new_data);
void ikarus_toggle_value_data_insert(IkarusToggleValue * value, size_t idx, bool new_data) {
return ikarus_value_data_base_insert(value, idx, new_data);
}
void ikarus_toggle_value_clear(IkarusToggleValue * value) {
return ikarus_value_base_clear(value);
void ikarus_toggle_value_data_clear(IkarusToggleValue * value) {
return ikarus_value_data_base_clear(value);
}
bool ikarus_toggle_value_is_undefined(IkarusToggleValue const * value) {
return ikarus_value_base_is_undefined(value);
bool ikarus_toggle_value_data_is_undefined(IkarusToggleValue const * value) {
return ikarus_value_data_base_is_undefined(value);
}
void ikarus_toggle_value_set_undefined(IkarusToggleValue * value, bool undefined) {
return ikarus_value_base_set_undefined(value, undefined);
void ikarus_toggle_value_data_set_undefined(IkarusToggleValue * value, bool undefined) {
return ikarus_value_data_base_set_undefined(value, undefined);
}
char const * ikarus_toggle_value_to_string(IkarusToggleValue const * value) {
return ikarus_value_base_to_string(value, [](auto const & value) { return value ? "" : ""; });
char const * ikarus_toggle_value_data_to_string(IkarusToggleValue const * value) {
return ikarus_value_data_base_to_string(value, [](auto const & value) { return value ? "" : ""; });
}
bool ikarus_toggle_value_is_equal(IkarusToggleValue const * lhs, IkarusToggleValue const * rhs) {
return ikarus_value_base_is_equal(lhs, rhs);
bool ikarus_toggle_value_data_is_equal(IkarusToggleValue const * lhs, IkarusToggleValue const * rhs) {
return ikarus_value_data_base_is_equal(lhs, rhs);
}
IkarusToggleValue * ikarus_toggle_value_copy(IkarusToggleValue const * value) {
return ikarus_value_base_copy(value);
IkarusToggleValue * ikarus_toggle_value_data_copy(IkarusToggleValue const * value) {
return ikarus_value_data_base_copy(value);
}
struct IkarusValue * ikarus_toggle_value_to_value(IkarusToggleValue * value) {
return ikarus_value_base_to_value(value);
struct IkarusValueData * ikarus_toggle_value_data_to_value(IkarusToggleValue * value) {
return ikarus_value_data_base_to_value(value);
}
struct IkarusValue const * ikarus_toggle_value_to_value_const(IkarusToggleValue const * value) {
return ikarus_value_base_to_value_const(value);
struct IkarusValueData const * ikarus_toggle_value_data_to_value_data_const(IkarusToggleValue const * value) {
return ikarus_value_data_base_to_value_data_const(value);
}

View file

@ -4,7 +4,7 @@
#include <ikarus/values/value.hpp>
struct IkarusToggleValue : IkarusValue {
struct IkarusToggleValue : IkarusValueData {
public:
using DataType = bool;

View file

@ -3,41 +3,53 @@
#include <string>
#include <boost/container/small_vector.hpp>
// required for header-only inclusion
#include "cppbase/templates.hpp"
#include <boost/json/src.hpp>
#include <ikarus/objects/properties/property_type.h>
// required for header-only inclusion
#include <cppbase/templates.hpp>
#include <ikarus/values/number_value.hpp>
#include <ikarus/values/text_value.hpp>
#include <ikarus/values/toggle_value.hpp>
#include <ikarus/values/value.hpp>
#include <ikarus/values/value_cardinality.h>
#include <ikarus/values/value_type.h>
IkarusValue::IkarusValue(Data data):
data(data) {}
IkarusValue::IkarusValue(Data data, IkarusValueCardinality cardinality):
data{data},
cardinality{cardinality} {}
cppbase::Result<IkarusValue *, IkarusValue::FromJsonError> IkarusValue::from_json(boost::json::value json) {
if (auto const * obj = json.if_object(); obj == nullptr) {
return cppbase::err(FromJsonError{});
} else {
boost::int64_t const * type = nullptr;
boost::json::value const * data = nullptr;
int64_t const * type;
int64_t const * cardinality;
boost::json::value const * data;
if (auto const * type_value = obj->if_contains("type"); type_value == nullptr) {
if (auto const * type_value = obj->if_contains(IKARUS_VALUE_JSON_TYPE_KEY); type_value == nullptr) {
return cppbase::err(FromJsonError{});
} else if (type = type_value->if_int64(); type == nullptr) {
return cppbase::err(FromJsonError{});
}
if (data = obj->if_contains("data"); data == nullptr) {
if (auto const * cardinality_value = obj->if_contains(IKARUS_VALUE_JSON_CARDINALITY_KEY); cardinality_value == nullptr) {
return cppbase::err(FromJsonError{});
} else if (cardinality = cardinality_value->if_int64(); cardinality == nullptr) {
return cppbase::err(FromJsonError{});
} else if (*cardinality != IkarusValueCardinality_Single && *cardinality != IkarusValueCardinality_Multiple) {
return cppbase::err(FromJsonError{});
}
auto create_value = [data]<typename T>() -> cppbase::Result<IkarusValue *, FromJsonError> {
if (data = obj->if_contains(IKARUS_VALUE_JSON_DATA_KEY); data == nullptr) {
return cppbase::err(FromJsonError{});
}
auto create_value = [data, cardinality]<typename T>() -> cppbase::Result<IkarusValue *, FromJsonError> {
T * ret = nullptr;
ret->cardinality = *cardinality;
if (data->is_null()) {
ret = new T{};
ret->data = boost::variant2::monostate{};
@ -58,13 +70,13 @@ cppbase::Result<IkarusValue *, IkarusValue::FromJsonError> IkarusValue::from_jso
};
switch (*type) {
case IkarusPropertyType_Toggle: {
case IkarusValueType_Toggle: {
return create_value.operator()<IkarusToggleValue>();
}
case IkarusPropertyType_Number: {
case IkarusValueType_Number: {
return create_value.operator()<IkarusNumberValue>();
}
case IkarusPropertyType_Text: {
case IkarusValueType_Text: {
return create_value.operator()<IkarusTextValue>();
}
default: {
@ -75,18 +87,18 @@ cppbase::Result<IkarusValue *, IkarusValue::FromJsonError> IkarusValue::from_jso
}
boost::json::value IkarusValue::to_json() const {
auto type = boost::variant2::visit(
auto type = std::visit(
cppbase::overloaded{
[]([[maybe_unused]] IkarusToggleValue const * value) { return IkarusPropertyType_Toggle; },
[]([[maybe_unused]] IkarusNumberValue const * value) { return IkarusPropertyType_Number; },
[]([[maybe_unused]] IkarusTextValue const * value) { return IkarusPropertyType_Text; }
[]([[maybe_unused]] IkarusToggleValue const * value) { return IkarusValueType_Toggle; },
[]([[maybe_unused]] IkarusNumberValue const * value) { return IkarusValueType_Number; },
[]([[maybe_unused]] IkarusTextValue const * value) { return IkarusValueType_Text; }
},
data
);
auto data_json = boost::variant2::visit(
auto data_json = std::visit(
[]<typename T>(T const * value) -> boost::json::value {
return boost::variant2::visit(
return std::visit(
cppbase::overloaded{
[]([[maybe_unused]] boost::variant2::monostate const & data) -> boost::json::value { return nullptr; },
[](auto const & data) -> boost::json::value { return boost::json::value_from(data); }
@ -98,8 +110,9 @@ boost::json::value IkarusValue::to_json() const {
);
return {
{"type", type},
{"data", data_json}
{ IKARUS_VALUE_JSON_TYPE_KEY, type},
{IKARUS_VALUE_JSON_CARDINALITY_KEY, cardinality},
{ IKARUS_VALUE_JSON_DATA_KEY, data_json}
};
}
@ -110,7 +123,7 @@ void ikarus_value_visit(
void (*text_visitor)(IkarusTextValue *, void *),
void * data
) {
boost::variant2::visit(
std::visit(
cppbase::overloaded{
[toggle_visitor, data](IkarusToggleValue * toggle_value) { toggle_visitor(toggle_value, data); },
[number_visitor, data](IkarusNumberValue * number_value) { number_visitor(number_value, data); },
@ -119,20 +132,3 @@ void ikarus_value_visit(
value->data
);
}
void ikarus_value_visit_const(
IkarusValue const * value,
void (*toggle_visitor)(IkarusToggleValue const *, void *),
void (*number_visitor)(IkarusNumberValue const *, void *),
void (*text_visitor)(IkarusTextValue const *, void *),
void * data
) {
boost::variant2::visit(
cppbase::overloaded{
[toggle_visitor, data](IkarusToggleValue const * toggle_value) { toggle_visitor(toggle_value, data); },
[number_visitor, data](IkarusNumberValue const * number_value) { number_visitor(number_value, data); },
[text_visitor, data](IkarusTextValue const * text_value) { text_visitor(text_value, data); }
},
value->data
);
}

View file

@ -1,20 +1,24 @@
#pragma once
#include <boost/json.hpp>
#include <boost/variant2.hpp>
#include <cppbase/result.hpp>
#include <ikarus/errors.hpp>
#include <ikarus/persistence/project.hpp>
#include <ikarus/values/value_cardinality.h>
constexpr auto IKARUS_VALUE_JSON_TYPE_KEY = "type";
constexpr auto IKARUS_VALUE_JSON_CARDINALITY_KEY = "card";
constexpr auto IKARUS_VALUE_JSON_DATA_KEY = "data";
struct IkarusValue {
public:
using Data = boost::variant2::variant<struct IkarusToggleValue *, struct IkarusNumberValue *, struct IkarusTextValue *>;
using Data = std::variant<struct IkarusToggleValue *, struct IkarusNumberValue *, struct IkarusTextValue *>;
constexpr static auto SMALL_VEC_VALUE_SIZE = 8;
public:
explicit IkarusValue(Data data);
explicit IkarusValue(Data data, IkarusValueCardinality cardinality);
IkarusValue(IkarusValue const &) = default;
IkarusValue(IkarusValue &&) noexcept = default;
@ -32,6 +36,7 @@ public:
public:
Data data;
IkarusValueCardinality cardinality;
};
template<>
@ -74,3 +79,20 @@ IkarusValue * fetch_value_from_db(IkarusProject * project, IkarusErrorData * err
return ret;
}
#define IKARUS_FAIL_IF_VALUE_MISSING_IMPL(var_name, entity, name, ret) \
IKARUS_VTRYRV_OR_FAIL( \
bool const var_name, \
ret, \
"unable to check whether value exists: {}", \
IkarusErrorInfo_Database_QueryFailed, \
(entity)->project->db->template query_one<bool>( \
"SELECT EXISTS(SELECT 1 FROM `entity_values` WHERE `entity` = ? AND `name` = ?)", \
(entity)->id, \
name \
) \
) \
\
IKARUS_FAIL_IF(!(var_name), ret, "entity value does not exist", IkarusErrorInfo_Client_Misuse);
#define IKARUS_FAIL_IF_VALUE_MISSING(entity, name, ret) IKARUS_FAIL_IF_VALUE_MISSING_IMPL(CPPBASE_UNIQUE_NAME(exists), entity, name, ret);

View file

@ -7,9 +7,10 @@
#include <cppbase/templates.hpp>
template<typename V>
typename V::DataType const * ikarus_value_base_get(V * value, size_t idx) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(&value->data);
typename V::DataType const * ikarus_value_data_base_get(V * value, size_t idx) {
if (auto * data = boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValueData::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
return &(*data)[idx];
}
@ -18,9 +19,10 @@ typename V::DataType const * ikarus_value_base_get(V * value, size_t idx) {
}
template<typename V>
size_t ikarus_value_base_get_size(V const * value) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(&value->data);
size_t ikarus_value_data_base_get_size(V const * value) {
if (auto * data = boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValueData::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
return data->size();
}
@ -29,57 +31,61 @@ size_t ikarus_value_base_get_size(V const * value) {
}
template<typename V>
void ikarus_value_base_set(V * value, size_t idx, typename V::DataType new_data) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(&value->data);
void ikarus_value_data_base_set(V * value, size_t idx, typename V::DataType new_data) {
if (auto * data = boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValueData::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
(*data)[idx] = new_data;
}
}
template<typename V>
void ikarus_value_base_remove(V * value, size_t idx) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(&value->data);
void ikarus_value_data_base_remove(V * value, size_t idx) {
if (auto * data = boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValueData::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
data->erase(data->begin() + idx);
}
}
template<typename V>
void ikarus_value_base_insert(V * value, size_t idx, typename V::DataType new_data) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(&value->data);
void ikarus_value_data_base_insert(V * value, size_t idx, typename V::DataType new_data) {
if (auto * data = boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValueData::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
data->insert(data->begin() + idx, new_data);
}
}
template<typename V>
void ikarus_value_base_clear(V * value) {
if (auto * data =
boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(&value->data);
void ikarus_value_data_base_clear(V * value) {
if (auto * data = boost::variant2::get_if<boost::container::small_vector<typename V::DataType, IkarusValueData::SMALL_VEC_VALUE_SIZE>>(
&value->data
);
data != nullptr) {
data->clear();
}
}
template<typename V>
bool ikarus_value_base_is_undefined(V const * value) {
bool ikarus_value_data_base_is_undefined(V const * value) {
return boost::variant2::holds_alternative<boost::variant2::monostate>(value->data);
}
template<typename V>
void ikarus_value_base_set_undefined(V * value, bool undefined) {
void ikarus_value_data_base_set_undefined(V * value, bool undefined) {
if (undefined) {
value->data = boost::variant2::monostate{};
} else {
value->data = boost::container::small_vector<typename V::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>{};
value->data = boost::container::small_vector<typename V::DataType, IkarusValueData::SMALL_VEC_VALUE_SIZE>{};
}
}
template<typename V, std::invocable<typename V::DataType> F>
char const * ikarus_value_base_to_string(V const * value, F transformer) {
char const * ikarus_value_data_base_to_string(V const * value, F transformer) {
return boost::variant2::visit(
cppbase::overloaded{
[](boost::variant2::monostate const &) -> char const * { return nullptr; },
@ -100,21 +106,21 @@ char const * ikarus_value_base_to_string(V const * value, F transformer) {
}
template<typename V>
bool ikarus_value_base_is_equal(V const * lhs, V const * rhs) {
bool ikarus_value_data_base_is_equal(V const * lhs, V const * rhs) {
return lhs->data == rhs->data;
}
template<typename V>
V * ikarus_value_base_copy(V const * value) {
V * ikarus_value_data_base_copy(V const * value) {
return new V{*value};
}
template<typename V>
struct IkarusValue * ikarus_value_base_to_value(V * value) {
return static_cast<IkarusValue *>(value);
struct IkarusValueData * ikarus_value_data_base_to_value(V * value) {
return static_cast<IkarusValueData *>(value);
}
template<typename V>
struct IkarusValue const * ikarus_value_base_to_value_const(V const * value) {
return static_cast<IkarusValue const *>(value);
struct IkarusValueData const * ikarus_value_data_base_to_value_data_const(V const * value) {
return static_cast<IkarusValueData const *>(value);
}