finalize schema/data setup
Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
parent
98cb7a44ef
commit
a49912337d
89 changed files with 2324 additions and 6271 deletions
|
|
@ -7,4 +7,4 @@ file(
|
|||
"*.c"
|
||||
)
|
||||
|
||||
set(SOURCE_FILES ${FILES} PARENT_SCOPE)
|
||||
set(SOURCE_FILES ${FILES} PARENT_SCOPE)
|
||||
|
|
|
|||
|
|
@ -1,41 +1,72 @@
|
|||
#include "ikarus/errors.h"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <ikarus/errors.hpp>
|
||||
|
||||
void safe_strcpy(
|
||||
std::string_view const src,
|
||||
char * dest,
|
||||
std::size_t const dest_size
|
||||
) {
|
||||
int i = 0;
|
||||
for (; i < dest_size - 1; ++i) {
|
||||
if (src[i] == '\0') {
|
||||
break;
|
||||
}
|
||||
|
||||
dest[i] = src[i];
|
||||
}
|
||||
|
||||
dest[i] = '\0';
|
||||
}
|
||||
|
||||
char const * ikarus_get_error_info_name(IkarusErrorInfo info) {
|
||||
switch (info) {
|
||||
case IkarusErrorInfo_None: return "None";
|
||||
switch (info) {
|
||||
case IkarusErrorInfo_None: return "None";
|
||||
|
||||
case IkarusErrorInfo_Client_Misuse: return "Client::Misuse";
|
||||
case IkarusErrorInfo_Client_InvalidInput: return "Client::InvalidInput";
|
||||
case IkarusErrorInfo_Client_InvalidFormat: return "Client::InvalidFormat";
|
||||
case IkarusErrorInfo_Client_ConstraintViolated: return "Client::ConstraintViolated";
|
||||
case IkarusErrorInfo_Client_Misuse: return "Client::Misuse";
|
||||
case IkarusErrorInfo_Client_InvalidInput: return "Client::InvalidInput";
|
||||
case IkarusErrorInfo_Client_NonExistent: return "Client::NonExistent";
|
||||
case IkarusErrorInfo_Client_InvalidFormat: return "Client::InvalidFormat";
|
||||
case IkarusErrorInfo_Client_ConstraintViolated:
|
||||
return "Client::ConstraintViolated";
|
||||
|
||||
case IkarusErrorInfo_Filesystem_NotFound: return "Filesystem::NotFound";
|
||||
case IkarusErrorInfo_Filesystem_AlreadyExists: return "Filesystem::AlreadyExists";
|
||||
case IkarusErrorInfo_Filesystem_MissingPermissions: return "Filesystem::MissingPermissions";
|
||||
case IkarusErrorInfo_Filesystem_InsufficientSpace: return "Filesystem::InsufficientSpace";
|
||||
case IkarusErrorInfo_Filesystem_InvalidPath: return "Filesystem::InvalidPath";
|
||||
case IkarusErrorInfo_Filesystem_NotFound: return "Filesystem::NotFound";
|
||||
case IkarusErrorInfo_Filesystem_AlreadyExists:
|
||||
return "Filesystem::AlreadyExists";
|
||||
case IkarusErrorInfo_Filesystem_MissingPermissions:
|
||||
return "Filesystem::MissingPermissions";
|
||||
case IkarusErrorInfo_Filesystem_InsufficientSpace:
|
||||
return "Filesystem::InsufficientSpace";
|
||||
case IkarusErrorInfo_Filesystem_InvalidPath:
|
||||
return "Filesystem::InvalidPath";
|
||||
|
||||
case IkarusErrorInfo_Database_ConnectionFailed: return "Database::ConnectionFailed";
|
||||
case IkarusErrorInfo_Database_QueryFailed: return "Database::QueryFailed";
|
||||
case IkarusErrorInfo_Database_MigrationFailed: return "Database::MigrationFailed";
|
||||
case IkarusErrorInfo_Database_InvalidState: return "Database::InvalidState";
|
||||
case IkarusErrorInfo_Database_ConnectionFailed:
|
||||
return "Database::ConnectionFailed";
|
||||
case IkarusErrorInfo_Database_QueryFailed: return "Database::QueryFailed";
|
||||
case IkarusErrorInfo_Database_MigrationFailed:
|
||||
return "Database::MigrationFailed";
|
||||
case IkarusErrorInfo_Database_InvalidState: return "Database::InvalidState";
|
||||
|
||||
case IkarusErrorInfo_OS_SystemCallFailed: return "OS::SystemCallFailed";
|
||||
case IkarusErrorInfo_OS_InvalidReturnValue: return "OS::InvalidReturnValue";
|
||||
case IkarusErrorInfo_OS_InsufficientMemory: return "OS::InsufficientMemory";
|
||||
case IkarusErrorInfo_OS_SystemCallFailed: return "OS::SystemCallFailed";
|
||||
case IkarusErrorInfo_OS_InvalidReturnValue: return "OS::InvalidReturnValue";
|
||||
case IkarusErrorInfo_OS_InsufficientMemory: return "OS::InsufficientMemory";
|
||||
|
||||
case IkarusErrorInfo_LibIkarus_InvalidState: return "LibIkarus::InvalidState";
|
||||
case IkarusErrorInfo_LibIkarus_CannotPerformOperation: return "LibIkarus::CannotPerformOperation";
|
||||
case IkarusErrorInfo_LibIkarus_Timeout: return "LibIkarus::Timeout";
|
||||
case IkarusErrorInfo_LibIkarus_InvalidState:
|
||||
return "LibIkarus::InvalidState";
|
||||
case IkarusErrorInfo_LibIkarus_CannotPerformOperation:
|
||||
return "LibIkarus::CannotPerformOperation";
|
||||
case IkarusErrorInfo_LibIkarus_Timeout: return "LibIkarus::Timeout";
|
||||
|
||||
default: return "Invalid";
|
||||
}
|
||||
default: return "Invalid";
|
||||
}
|
||||
}
|
||||
|
||||
bool ikarus_error_data_is_success(IkarusErrorData const * data) {
|
||||
return data->info == IkarusErrorInfo_None;
|
||||
return data->info == IkarusErrorInfo_None;
|
||||
}
|
||||
|
||||
bool ikarus_error_data_is_error(IkarusErrorData const * data) {
|
||||
return data->info != IkarusErrorInfo_None;
|
||||
return data->info != IkarusErrorInfo_None;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,80 +1,169 @@
|
|||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
#include <ikarus/errors.h>
|
||||
|
||||
inline void safe_strcpy(char * dest, std::string_view src, size_t dest_size) {
|
||||
for (int i = 0; i < dest_size; ++i) {
|
||||
if (src[i] == '\0') {
|
||||
dest[i] = '\0';
|
||||
return;
|
||||
}
|
||||
void safe_strcpy(
|
||||
std::string_view const src,
|
||||
char * dest,
|
||||
size_t const dest_size
|
||||
);
|
||||
|
||||
dest[i] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
#define IKARUS_SET_ERROR(msg, err_info) \
|
||||
if (error_out != nullptr) { \
|
||||
safe_strcpy(static_cast<char *>(error_out->message), msg, IKARUS_ERROR_DATA_MAX_MESSAGE_LIMIT); \
|
||||
error_out->info = err_info; \
|
||||
}
|
||||
#define IKARUS_SET_ERROR(msg, err_info) \
|
||||
if (error_out != nullptr) { \
|
||||
safe_strcpy( \
|
||||
msg, \
|
||||
static_cast<char *>(error_out->message), \
|
||||
IKARUS_ERROR_DATA_MAX_MESSAGE_LIMIT \
|
||||
); \
|
||||
error_out->info = err_info; \
|
||||
}
|
||||
|
||||
#define IKARUS_FAIL(ret, msg, err_info) \
|
||||
IKARUS_SET_ERROR(msg, err_info); \
|
||||
return ret
|
||||
IKARUS_SET_ERROR(msg, err_info); \
|
||||
return ret
|
||||
|
||||
#define IKARUS_FAIL_IF(condition, ret, msg, err_info) \
|
||||
if (condition) { \
|
||||
IKARUS_SET_ERROR(msg, err_info) \
|
||||
return ret; \
|
||||
}
|
||||
if (condition) { \
|
||||
IKARUS_SET_ERROR(msg, err_info) \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#define IKARUS_FAIL_IF_ERROR(ret) \
|
||||
if (ikarus_error_data_is_error(error_out)) { \
|
||||
return ret; \
|
||||
}
|
||||
#define IKARUS_TRY_OR_FAIL_IMPL(var_name, msg, err_info, ...) \
|
||||
auto var_name = __VA_ARGS__; \
|
||||
if (var_name.is_error()) { \
|
||||
IKARUS_SET_ERROR( \
|
||||
fmt::format( \
|
||||
fmt::runtime(msg), \
|
||||
std::move(var_name).unwrap_error() \
|
||||
), \
|
||||
err_info \
|
||||
); \
|
||||
return var_name; \
|
||||
}
|
||||
|
||||
#define IKARUS_FAIL_IF_NULL(ptr, ret) IKARUS_FAIL_IF(((ptr) == nullptr), ret, #ptr " must not be null", IkarusErrorInfo_Client_InvalidNull)
|
||||
#define IKARUS_TRY_OR_FAIL(msg, err_info, ...) \
|
||||
IKARUS_TRY_OR_FAIL_IMPL( \
|
||||
CPPBASE_UNIQUE_NAME(result), \
|
||||
msg, \
|
||||
err_info, \
|
||||
__VA_ARGS__ \
|
||||
);
|
||||
|
||||
#define IKARUS_TRY_OR_FAIL_IMPL(var_name, msg, err_info, ...) \
|
||||
auto var_name = __VA_ARGS__; \
|
||||
if (var_name.is_error()) { \
|
||||
IKARUS_SET_ERROR(fmt::format(msg, var_name.unwrap_error()), err_info); \
|
||||
return var_name; \
|
||||
}
|
||||
|
||||
#define IKARUS_TRY_OR_FAIL(msg, err_info, ...) IKARUS_TRY_OR_FAIL_IMPL(CPPBASE_UNIQUE_NAME(result), msg, err_info, __VA_ARGS__);
|
||||
|
||||
#define IKARUS_TRYRV_OR_FAIL_IMPL(var_name, ret, msg, err_info, ...) \
|
||||
auto var_name = __VA_ARGS__; \
|
||||
if (var_name.is_error()) { \
|
||||
IKARUS_SET_ERROR(fmt::format(msg, var_name.unwrap_error()), err_info); \
|
||||
return ret; \
|
||||
}
|
||||
#define IKARUS_TRYRV_OR_FAIL_IMPL(var_name, ret, msg, err_info, ...) \
|
||||
auto var_name = __VA_ARGS__; \
|
||||
if (var_name.is_error()) { \
|
||||
IKARUS_SET_ERROR( \
|
||||
fmt::format( \
|
||||
fmt::runtime(msg), \
|
||||
std::move(var_name).unwrap_error() \
|
||||
), \
|
||||
err_info \
|
||||
); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#define IKARUS_TRYRV_OR_FAIL(ret, msg, err_info, ...) \
|
||||
IKARUS_TRYRV_OR_FAIL_IMPL(CPPBASE_UNIQUE_NAME(result), ret, msg, err_info, __VA_ARGS__);
|
||||
IKARUS_TRYRV_OR_FAIL_IMPL( \
|
||||
CPPBASE_UNIQUE_NAME(result), \
|
||||
ret, \
|
||||
msg, \
|
||||
err_info, \
|
||||
__VA_ARGS__ \
|
||||
);
|
||||
|
||||
#define IKARUS_VTRY_OR_FAIL_IMPL(var_name, value, msg, err_info, ...) \
|
||||
auto var_name = __VA_ARGS__; \
|
||||
if (var_name.is_error()) { \
|
||||
IKARUS_SET_ERROR(fmt::format(msg, var_name.unwrap_error()), err_info); \
|
||||
return var_name; \
|
||||
} \
|
||||
value = var_name.unwrap_value()
|
||||
#define IKARUS_VTRY_OR_FAIL_IMPL(var_name, value, msg, err_info, ...) \
|
||||
auto var_name = __VA_ARGS__; \
|
||||
if (var_name.is_error()) { \
|
||||
IKARUS_SET_ERROR( \
|
||||
fmt::format( \
|
||||
fmt::runtime(msg), \
|
||||
std::move(var_name).unwrap_error() \
|
||||
), \
|
||||
err_info \
|
||||
); \
|
||||
return var_name; \
|
||||
} \
|
||||
value = std::move(var_name).unwrap_value()
|
||||
|
||||
#define IKARUS_VTRY_OR_FAIL(value, msg, err_info, ...) \
|
||||
IKARUS_VTRY_OR_FAIL_IMPL(CPPBASE_UNIQUE_NAME(result), value, msg, err_info, __VA_ARGS__);
|
||||
IKARUS_VTRY_OR_FAIL_IMPL( \
|
||||
CPPBASE_UNIQUE_NAME(result), \
|
||||
value, \
|
||||
msg, \
|
||||
err_info, \
|
||||
__VA_ARGS__ \
|
||||
);
|
||||
|
||||
#define IKARUS_VTRYRV_OR_FAIL_IMPL(var_name, value, ret, msg, err_info, ...) \
|
||||
auto var_name = __VA_ARGS__; \
|
||||
if (var_name.is_error()) { \
|
||||
IKARUS_SET_ERROR(fmt::format(msg, var_name.unwrap_error()), err_info); \
|
||||
return ret; \
|
||||
} \
|
||||
value = var_name.unwrap_value()
|
||||
#define IKARUS_VTRYRV_OR_FAIL_IMPL(var_name, value, ret, msg, err_info, ...) \
|
||||
auto var_name = __VA_ARGS__; \
|
||||
if (var_name.is_error()) { \
|
||||
IKARUS_SET_ERROR( \
|
||||
fmt::format(fmt::runtime(msg), var_name.unwrap_error()), \
|
||||
err_info \
|
||||
); \
|
||||
return ret; \
|
||||
} \
|
||||
value = std::move(var_name).unwrap_value()
|
||||
|
||||
#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__);
|
||||
IKARUS_VTRYRV_OR_FAIL_IMPL( \
|
||||
CPPBASE_UNIQUE_NAME(result), \
|
||||
value, \
|
||||
ret, \
|
||||
msg, \
|
||||
err_info, \
|
||||
__VA_ARGS__ \
|
||||
);
|
||||
|
||||
#define IKARUS_FAIL_IF_ERROR(ret) \
|
||||
if (ikarus_error_data_is_error(error_out)) { \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#define IKARUS_FAIL_IF_NULL(ptr, ret) \
|
||||
IKARUS_FAIL_IF( \
|
||||
((ptr) == nullptr), \
|
||||
ret, \
|
||||
#ptr " must not be null", \
|
||||
IkarusErrorInfo_Client_InvalidNull \
|
||||
)
|
||||
|
||||
#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_NOT_EXIST(object, ret) \
|
||||
IKARUS_VTRYRV_OR_FAIL( \
|
||||
auto exists, \
|
||||
ret, \
|
||||
fmt::format( \
|
||||
"failed to check if {} exists", \
|
||||
std::remove_cvref_t<decltype(*object)>::object_name \
|
||||
), \
|
||||
IkarusErrorInfo_Database_QueryFailed, \
|
||||
object->project->db->query_one<bool>( \
|
||||
fmt::format( \
|
||||
"SELECT EXISTS(SELECT 1 FROM `{}` WHERE `id` = ?)", \
|
||||
std::remove_cvref_t<decltype(*object)>::table_name \
|
||||
), \
|
||||
object->id \
|
||||
) \
|
||||
); \
|
||||
\
|
||||
IKARUS_FAIL_IF( \
|
||||
!exists, \
|
||||
ret, \
|
||||
fmt::format( \
|
||||
"{} doesn't exist", \
|
||||
std::remove_cvref_t<decltype(*object)>::object_name \
|
||||
), \
|
||||
IkarusErrorInfo_Client_NonExistent \
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,168 +1,5 @@
|
|||
#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>
|
||||
#include <ikarus/values/value_type.h>
|
||||
|
||||
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(name, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
int64_t const id,
|
||||
nullptr,
|
||||
"failed to create blueprint: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
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());
|
||||
})
|
||||
);
|
||||
|
||||
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 `blueprints` WHERE `id` = ?", blueprint->id)
|
||||
);
|
||||
|
||||
blueprint->project->uncache(blueprint);
|
||||
}
|
||||
|
||||
int64_t ikarus_blueprint_get_id(IkarusBlueprint const * blueprint, IkarusErrorData * error_out) {
|
||||
return ikarus::util::object_get_id(blueprint, error_out);
|
||||
}
|
||||
|
||||
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<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<int64_t, IkarusValueType>(
|
||||
"SELECT `id` FROM `properties` WHERE `blueprint` = ?",
|
||||
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 `blueprint` = ?", 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;
|
||||
}
|
||||
|
||||
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<int64_t>(
|
||||
"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(`entity`) FROM `entity_blueprint_links` WHERE `blueprint` = ?", blueprint->id)
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/object.hpp>
|
||||
#include <string>
|
||||
|
||||
struct IkarusBlueprint : public IkarusObject {
|
||||
public:
|
||||
IkarusBlueprint(struct IkarusProject * project, int64_t id);
|
||||
struct IkarusBlueprint {
|
||||
consteval static inline auto OBJECT_NAME() -> std::string_view {
|
||||
return "blueprint";
|
||||
}
|
||||
|
||||
IkarusBlueprint(IkarusBlueprint const &) = default;
|
||||
IkarusBlueprint(IkarusBlueprint &&) = default;
|
||||
consteval static inline auto TABLE_NAME() -> std::string_view {
|
||||
return "blueprints";
|
||||
}
|
||||
|
||||
IkarusBlueprint & operator=(IkarusBlueprint const &) = default;
|
||||
IkarusBlueprint & operator=(IkarusBlueprint &&) = default;
|
||||
IkarusBlueprint(
|
||||
struct IkarusProject * project,
|
||||
int64_t id,
|
||||
std::string_view name
|
||||
);
|
||||
|
||||
~IkarusBlueprint() override = default;
|
||||
|
||||
public:
|
||||
std::string_view get_table_name() const noexcept override;
|
||||
struct IkarusProject * project;
|
||||
int64_t id;
|
||||
std::string name;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,488 +1,171 @@
|
|||
#include "ikarus/objects/entity.h"
|
||||
|
||||
#include <cppbase/strings.hpp>
|
||||
#include "entity.hpp"
|
||||
|
||||
#include <ikarus/errors.h>
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/objects/blueprint.hpp>
|
||||
#include <ikarus/objects/entity.hpp>
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/objects/util.hpp>
|
||||
#include <ikarus/objects/entity.h>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
#include <ikarus/values/entity_property_value.hpp>
|
||||
#include <ikarus/values/value.hpp>
|
||||
#include <ikarus/values/value_type.h>
|
||||
|
||||
IkarusEntity::IkarusEntity(IkarusProject * project, int64_t id):
|
||||
IkarusObject{project, id} {}
|
||||
|
||||
std::string_view IkarusEntity::get_table_name() const noexcept {
|
||||
return "entities";
|
||||
}
|
||||
IkarusEntity::IkarusEntity(
|
||||
struct IkarusProject * project,
|
||||
int64_t id,
|
||||
std::string_view name
|
||||
):
|
||||
project{project},
|
||||
id{id},
|
||||
name{name} {}
|
||||
|
||||
IkarusEntity * ikarus_entity_create(
|
||||
struct IkarusProject * project,
|
||||
char const * name,
|
||||
IkarusErrorData * error_out
|
||||
struct IkarusProject * project,
|
||||
char const * name,
|
||||
IkarusEntityCreateFlags flags,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
int64_t const id,
|
||||
nullptr,
|
||||
"failed to create entity: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
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());
|
||||
}
|
||||
)
|
||||
);
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to create entity: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->execute("INSERT INTO `entities`(`name`) VALUES(?)", name)
|
||||
);
|
||||
|
||||
return project->get_entity(id);
|
||||
auto const id = project->db->last_insert_rowid();
|
||||
return new IkarusEntity{project, id, name};
|
||||
}
|
||||
|
||||
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 `entities` WHERE `id` = ?", entity->id)
|
||||
);
|
||||
|
||||
entity->project->uncache(entity);
|
||||
}
|
||||
|
||||
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
|
||||
void ikarus_entity_delete(
|
||||
IkarusEntity * entity,
|
||||
IkarusEntityDeleteFlags flags,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
return ikarus::util::object_get_project(entity, error_out);
|
||||
IKARUS_FAIL_IF_NULL(entity, );
|
||||
IKARUS_FAIL_IF_NOT_EXIST(entity, );
|
||||
IKARUS_FAIL_IF_NULL(entity->project, );
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"failed to delete entity: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db
|
||||
->execute("DELETE FROM `entities` WHERE `id` = ?", entity->id)
|
||||
);
|
||||
|
||||
delete entity;
|
||||
}
|
||||
|
||||
char const * ikarus_entity_get_name(
|
||||
IkarusEntity const * entity,
|
||||
IkarusErrorData * error_out
|
||||
IkarusEntity * ikarus_entity_copy(
|
||||
IkarusEntity * entity,
|
||||
IkarusEntityCopyFlags flags,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
return ikarus::util::object_get_name(entity, error_out);
|
||||
IKARUS_FAIL_IF_NULL(entity, nullptr);
|
||||
IKARUS_FAIL_IF_NOT_EXIST(entity, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(entity->project, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto id,
|
||||
nullptr,
|
||||
"failed to copy entity: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->transact(
|
||||
[entity](auto * db)
|
||||
-> cppbase::Result<int64_t, sqlitecpp::TransactionError> {
|
||||
TRY(entity->project->db->execute(
|
||||
"INSERT INTO `entities`(`name`) VALUES(?)",
|
||||
entity->name.data()
|
||||
));
|
||||
|
||||
TRY(entity->project->db->execute(
|
||||
"INSERT INTO `entity_values`(`entity`, `name`, `value`)"
|
||||
" SELECT ?1, `name`, `value` FROM `entity_values` WHERE "
|
||||
"`entity` = ?1",
|
||||
entity->id
|
||||
))
|
||||
|
||||
TRY(entity->project->db->execute(
|
||||
"INSERT INTO `entity_property_values`("
|
||||
" `entity`, "
|
||||
" `property`,"
|
||||
" `value`"
|
||||
") "
|
||||
"SELECT ?1, `property`, `value` FROM "
|
||||
"`entity_property_values` "
|
||||
"WHERE `entity` = ?1",
|
||||
entity->id
|
||||
))
|
||||
|
||||
TRY(entity->project->db->execute(
|
||||
"INSERT INTO `entity_blueprint_links`(`entity`, "
|
||||
"`blueprint`)"
|
||||
"SELECT ?1, `property`, `value` FROM "
|
||||
"`entity_property_values` "
|
||||
"WHERE `entity` = ?1",
|
||||
entity->id
|
||||
))
|
||||
|
||||
return cppbase::ok(entity->project->db->last_insert_rowid());
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
return new IkarusEntity{entity->project, id, entity->name};
|
||||
}
|
||||
|
||||
IkarusProject *
|
||||
ikarus_entity_get_project(IkarusEntity * entity, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(entity, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(entity->project, nullptr);
|
||||
|
||||
return entity->project;
|
||||
}
|
||||
|
||||
char const *
|
||||
ikarus_entity_get_name(IkarusEntity * entity, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(entity, nullptr);
|
||||
|
||||
return entity->name.data();
|
||||
}
|
||||
|
||||
void ikarus_entity_set_name(
|
||||
IkarusEntity * entity,
|
||||
char const * name,
|
||||
IkarusErrorData * error_out
|
||||
IkarusEntity * entity,
|
||||
char const * name,
|
||||
IkarusEntitySetNameFlags flags,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
ikarus::util::object_set_name(entity, name, error_out);
|
||||
IKARUS_FAIL_IF_NULL(entity, );
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, );
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"failed to set name for entity: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->execute(
|
||||
"UPDATE `entities` SET `name` = ? WHERE `id` = ?",
|
||||
name,
|
||||
entity->id
|
||||
)
|
||||
);
|
||||
|
||||
entity->name = name;
|
||||
}
|
||||
|
||||
bool ikarus_entity_is_linked_to_blueprint(
|
||||
IkarusEntity const * entity,
|
||||
struct IkarusBlueprint const * blueprint,
|
||||
IkarusErrorData * error_out
|
||||
char const * ikarus_entity_get_value(
|
||||
IkarusEntity * entity,
|
||||
char const * name,
|
||||
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
|
||||
)
|
||||
);
|
||||
|
||||
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(
|
||||
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;
|
||||
}
|
||||
|
||||
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<int64_t>(
|
||||
"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(`blueprint`) FROM `entity_blueprint_links` WHERE "
|
||||
"`entity` = ?",
|
||||
entity->id
|
||||
)
|
||||
);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
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;
|
||||
IKARUS_FAIL_IF_NULL(entity, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(name, nullptr);
|
||||
}
|
||||
|
||||
void ikarus_entity_set_value(
|
||||
IkarusEntity * entity,
|
||||
char const * name,
|
||||
struct IkarusValue const * value,
|
||||
IkarusErrorData * error_out
|
||||
IkarusEntity * entity,
|
||||
char const * name,
|
||||
char 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 has_property,
|
||||
false,
|
||||
"unable to check whether entity has property: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->query_one<bool>(
|
||||
"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 has_property;
|
||||
}
|
||||
|
||||
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<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 properties from entity: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
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];
|
||||
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);
|
||||
|
||||
// given that values are loaded lazily we can't just check
|
||||
// `entity_property_values` here
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
size_t const count,
|
||||
0,
|
||||
"unable to fetch property count from entity: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
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 count;
|
||||
}
|
||||
|
||||
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);
|
||||
IKARUS_FAIL_IF_OBJECT_MISSING(property, nullptr);
|
||||
|
||||
auto * value = fetch_value_from_db(
|
||||
entity->project,
|
||||
error_out,
|
||||
"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
|
||||
);
|
||||
|
||||
IKARUS_FAIL_IF_ERROR(nullptr);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void ikarus_entity_set_property_value(
|
||||
IkarusEntity * entity,
|
||||
struct IkarusProperty const * property,
|
||||
struct IkarusValue * 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, );
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to set entity property value: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
entity->project->db->execute(
|
||||
"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,
|
||||
boost::json::serialize(value->to_json())
|
||||
)
|
||||
);
|
||||
IKARUS_FAIL_IF_NULL(entity, );
|
||||
IKARUS_FAIL_IF_NULL(name, );
|
||||
IKARUS_FAIL_IF_NULL(value, );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/object.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct IkarusEntity : public IkarusObject {
|
||||
public:
|
||||
inline IkarusEntity(struct IkarusProject * project, int64_t id);
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
IkarusEntity(IkarusEntity const &) = default;
|
||||
IkarusEntity(IkarusEntity &&) = default;
|
||||
struct IkarusEntity {
|
||||
constinit static inline auto object_name = "entity";
|
||||
constinit static inline auto table_name = "entities";
|
||||
|
||||
IkarusEntity & operator=(IkarusEntity const &) = default;
|
||||
IkarusEntity & operator=(IkarusEntity &&) = default;
|
||||
IkarusEntity(
|
||||
struct IkarusProject * project,
|
||||
int64_t id,
|
||||
std::string_view name
|
||||
);
|
||||
|
||||
~IkarusEntity() override = default;
|
||||
struct IkarusProject * project;
|
||||
int64_t id;
|
||||
std::string name;
|
||||
|
||||
public:
|
||||
std::string_view get_table_name() const noexcept override;
|
||||
std::vector<std::pair<std::string_view, IkarusValue *>> values_ordered;
|
||||
std::unordered_map<std::string, IkarusValue> values;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
#include "object.hpp"
|
||||
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
|
||||
IkarusObject::IkarusObject(IkarusProject * project, int64_t id):
|
||||
project{project},
|
||||
id{id} {}
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
class IkarusObject {
|
||||
public:
|
||||
IkarusObject(struct IkarusProject * project, int64_t 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;
|
||||
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);
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
#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, int64_t 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);
|
||||
}
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/values/number_value.hpp>
|
||||
#include <ikarus/values/value_type.h>
|
||||
|
||||
struct IkarusNumberProperty : public IkarusProperty {
|
||||
public:
|
||||
using value_type = IkarusNumberValue;
|
||||
constexpr auto static PropertyType = IkarusValueType_Number;
|
||||
|
||||
public:
|
||||
IkarusNumberProperty(struct IkarusProject * project, int64_t id);
|
||||
};
|
||||
|
|
@ -1,143 +0,0 @@
|
|||
#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/values/value_type.h>
|
||||
#include <ikarus/objects/util.hpp>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
IkarusProperty::IkarusProperty(IkarusProject * project, int64_t 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);
|
||||
}
|
||||
|
||||
int64_t ikarus_property_get_id(IkarusProperty const * property, IkarusErrorData * error_out) {
|
||||
return ikarus::util::object_get_id(property, error_out);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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,
|
||||
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<IkarusValueType>(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<int64_t>("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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <variant>
|
||||
|
||||
#include <ikarus/objects/object.hpp>
|
||||
|
||||
struct IkarusProperty : public IkarusObject {
|
||||
public:
|
||||
using Data = std::variant<struct IkarusToggleProperty *, struct IkarusNumberProperty *, struct IkarusTextProperty *>;
|
||||
|
||||
public:
|
||||
IkarusProperty(struct IkarusProject * project, int64_t 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;
|
||||
};
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
#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, int64_t 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);
|
||||
}
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/values/value_type.h>
|
||||
#include <ikarus/values/text_value.hpp>
|
||||
|
||||
struct IkarusTextProperty : public IkarusProperty {
|
||||
public:
|
||||
using value_type = IkarusTextValue;
|
||||
constexpr auto static PropertyType = IkarusValueType_Text;
|
||||
|
||||
public:
|
||||
IkarusTextProperty(struct IkarusProject * project, int64_t id);
|
||||
};
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
#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, int64_t 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);
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/values/toggle_value.hpp>
|
||||
#include <ikarus/values/value_type.h>
|
||||
|
||||
struct IkarusToggleProperty {
|
||||
public:
|
||||
using value_type = IkarusToggleValue;
|
||||
constexpr auto static PropertyType = IkarusValueType_Toggle;
|
||||
|
||||
public:
|
||||
IkarusToggleProperty(struct IkarusProject * project, int64_t id);
|
||||
|
||||
public:
|
||||
IkarusProperty * property;
|
||||
};
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
#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_NAME_INVALID(name, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
int64_t const id,
|
||||
nullptr,
|
||||
"failed to create property: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
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(
|
||||
"INSERT INTO `properties`(`id`, `name`, `type`, `source`) VALUES(?, ?, ?, ?)",
|
||||
id,
|
||||
name,
|
||||
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
|
||||
15
src/ikarus/objects/property.cpp
Normal file
15
src/ikarus/objects/property.cpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#include "property.hpp"
|
||||
|
||||
#include <ikarus/errors.h>
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/objects/property.h>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
|
||||
IkarusProperty::IkarusProperty(
|
||||
struct IkarusProject * project,
|
||||
int64_t id,
|
||||
std::string_view name
|
||||
):
|
||||
project{project},
|
||||
id{id},
|
||||
name{name} {}
|
||||
23
src/ikarus/objects/property.hpp
Normal file
23
src/ikarus/objects/property.hpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
struct IkarusProperty {
|
||||
consteval static inline auto OBJECT_NAME() -> std::string_view {
|
||||
return "property";
|
||||
}
|
||||
|
||||
consteval static inline auto TABLE_NAME() -> std::string_view {
|
||||
return "properties";
|
||||
}
|
||||
|
||||
IkarusProperty(
|
||||
struct IkarusProject * project,
|
||||
int64_t id,
|
||||
std::string_view name
|
||||
);
|
||||
|
||||
struct IkarusProject * project;
|
||||
int64_t id;
|
||||
std::string name;
|
||||
};
|
||||
|
|
@ -1,63 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <cppbase/strings.hpp>
|
||||
|
||||
#include <ikarus/errors.h>
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
|
||||
namespace ikarus::util {
|
||||
|
||||
template<typename O>
|
||||
[[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);
|
||||
|
||||
return object->id;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
#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(name, );
|
||||
|
||||
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
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <cppbase/assets.hpp>
|
||||
#include <cppbase/result.hpp>
|
||||
|
||||
|
|
@ -9,7 +7,10 @@
|
|||
|
||||
namespace ikarus {
|
||||
|
||||
CPPBASE_ASSET(m0_initial_layout, "ikarus/persistence/migrations/m0_initial_layout.sql");
|
||||
CPPBASE_ASSET(
|
||||
m0_initial_layout,
|
||||
"ikarus/persistence/migrations/m0_initial_layout.sql"
|
||||
);
|
||||
|
||||
class Migration : public sqlitecpp::Migration {
|
||||
public:
|
||||
|
|
@ -27,7 +28,11 @@ public:
|
|||
std::string_view sql;
|
||||
};
|
||||
|
||||
#define DECLARE_MIGRATION(name) std::make_unique<ikarus::Migration>(static_cast<char const *>(name()), name##_size())
|
||||
#define DECLARE_MIGRATION(name) \
|
||||
std::make_unique<ikarus::Migration>( \
|
||||
static_cast<char const *>(name()), \
|
||||
name##_size() \
|
||||
)
|
||||
|
||||
constexpr std::string_view DB_VERSION_KEY = "IKARUS_DB_VERSION";
|
||||
std::vector<std::unique_ptr<sqlitecpp::Migration>> const MIGRATIONS = []() {
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@ CREATE TABLE `entities`
|
|||
|
||||
CREATE TABLE `entity_values`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY,
|
||||
`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;
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE (`entity`, `name`)
|
||||
) STRICT;
|
||||
|
||||
CREATE TABLE `blueprints`
|
||||
(
|
||||
|
|
@ -25,14 +26,11 @@ CREATE TABLE `properties`
|
|||
`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,
|
||||
`schema` TEXT NOT NULL,
|
||||
`default_value` TEXT NOT NULL,
|
||||
`settings` TEXT NOT NULL
|
||||
) STRICT;
|
||||
|
||||
CREATE INDEX `properties_type` ON `properties` (`type`);
|
||||
|
||||
CREATE TABLE `entity_blueprint_links`
|
||||
(
|
||||
`entity` INTEGER NOT NULL REFERENCES `entities` (`id`) ON DELETE CASCADE,
|
||||
|
|
@ -43,9 +41,11 @@ CREATE TABLE `entity_blueprint_links`
|
|||
|
||||
CREATE TABLE `entity_property_values`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY,
|
||||
`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;
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE (`entity`, `property`)
|
||||
) STRICT;
|
||||
|
|
|
|||
|
|
@ -1,339 +1,348 @@
|
|||
#include "ikarus/persistence/project.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/uuid.hpp>
|
||||
|
||||
#include <cppbase/strings.hpp>
|
||||
|
||||
#include <ikarus/errors.h>
|
||||
#include <ikarus/errors.hpp>
|
||||
#include <ikarus/macros.h>
|
||||
#include <ikarus/objects/blueprint.hpp>
|
||||
#include <ikarus/objects/entity.hpp>
|
||||
#include <ikarus/objects/properties/number_property.hpp>
|
||||
#include <ikarus/objects/properties/property.hpp>
|
||||
#include <ikarus/objects/properties/text_property.hpp>
|
||||
#include <ikarus/objects/properties/toggle_property.hpp>
|
||||
#include <ikarus/objects/util.hpp>
|
||||
#include <ikarus/persistence/migrations.hpp>
|
||||
#include <ikarus/persistence/project.hpp>
|
||||
|
||||
IkarusProject::IkarusProject(std::string_view name, std::string_view path, std::unique_ptr<sqlitecpp::Connection> && db):
|
||||
name{name},
|
||||
path{std::move(path)},
|
||||
db{std::move(db)},
|
||||
_blueprints{},
|
||||
_properties{},
|
||||
_entities{} {}
|
||||
constexpr char const * DB_PROJECT_ID_KEY = "project_id";
|
||||
constexpr char const * DB_PROJECT_NAME_KEY = "project_name";
|
||||
|
||||
IkarusBlueprint * IkarusProject::get_blueprint(int64_t id) {
|
||||
return get_cached_object<IkarusBlueprint>(id, this->_blueprints);
|
||||
auto create_impl(
|
||||
std::string_view path,
|
||||
std::string_view name,
|
||||
IkarusErrorData * error_out
|
||||
) -> std::unique_ptr<sqlitecpp::Connection> {
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto db,
|
||||
nullptr,
|
||||
"failed to create project db: {}",
|
||||
IkarusErrorInfo_Database_ConnectionFailed,
|
||||
sqlitecpp::Connection::open(path)
|
||||
);
|
||||
|
||||
auto close_db = [&]() {
|
||||
if (db) {
|
||||
LOG_INFO("closing project db");
|
||||
auto res = db->close();
|
||||
|
||||
if (res.is_error()) {
|
||||
LOG_ERROR("failed to close project db: {}", res.unwrap_error());
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to migrate project db: {}",
|
||||
IkarusErrorInfo_Database_MigrationFailed,
|
||||
db->migrate(ikarus::MIGRATIONS).on_error(close_db)
|
||||
);
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to set project id",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
db->execute(
|
||||
"INSERT INTO `metadata`(`key`, `value`) VALUES(?, ?)",
|
||||
DB_PROJECT_ID_KEY,
|
||||
boost::uuids::to_string(boost::uuids::random_generator()())
|
||||
)
|
||||
.on_error(close_db)
|
||||
);
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to set project id",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
db->execute(
|
||||
"INSERT INTO `metadata`(`key`, `value`) VALUES(?, ?)",
|
||||
DB_PROJECT_NAME_KEY,
|
||||
name
|
||||
)
|
||||
.on_error(close_db)
|
||||
);
|
||||
|
||||
return std::move(db);
|
||||
}
|
||||
|
||||
auto IkarusProject::uncache(IkarusBlueprint * blueprint) -> void {
|
||||
remove_cached_object(blueprint, _blueprints);
|
||||
}
|
||||
|
||||
auto IkarusProject::get_entity(int64_t id) -> IkarusEntity * {
|
||||
return get_cached_object<IkarusEntity>(id, this->_entities);
|
||||
}
|
||||
|
||||
auto IkarusProject::uncache(IkarusEntity * entity) -> void {
|
||||
remove_cached_object(entity, _entities);
|
||||
}
|
||||
|
||||
auto IkarusProject::get_property(int64_t id, IkarusValueType type) -> IkarusProperty * {
|
||||
auto const iter = _properties.find(id);
|
||||
|
||||
if (iter == _properties.cend()) {
|
||||
switch (type) {
|
||||
case IkarusValueType_Toggle:
|
||||
return _properties.emplace(id, std::make_unique<IkarusToggleProperty>(this, id)).first->second.get();
|
||||
case IkarusValueType_Number:
|
||||
return _properties.emplace(id, std::make_unique<IkarusNumberProperty>(this, id)).first->second.get();
|
||||
case IkarusValueType_Text: return _properties.emplace(id, std::make_unique<IkarusTextProperty>(this, id)).first->second.get();
|
||||
}
|
||||
}
|
||||
|
||||
return iter->second.get();
|
||||
}
|
||||
|
||||
auto IkarusProject::uncache(IkarusProperty * property) -> void {
|
||||
remove_cached_object(property, _properties);
|
||||
}
|
||||
|
||||
IkarusProject * ikarus_project_create(char const * path, char const * name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(path, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(name, nullptr);
|
||||
IKARUS_FAIL_IF(cppbase::is_empty_or_blank(path), nullptr, "path must not be empty", IkarusErrorInfo_Client_InvalidInput);
|
||||
IKARUS_FAIL_IF(cppbase::is_empty_or_blank(name), nullptr, "name must not be empty", IkarusErrorInfo_Client_InvalidInput);
|
||||
|
||||
boost::filesystem::path fs_path{path};
|
||||
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
bool const exists = fs::exists(fs_path, ec);
|
||||
IKARUS_FAIL_IF(
|
||||
ec && ec != boost::system::errc::no_such_file_or_directory,
|
||||
nullptr,
|
||||
fmt::format("unable to check whether path is occupied: {}", ec.message()),
|
||||
IkarusErrorInfo_Filesystem_AlreadyExists
|
||||
);
|
||||
IKARUS_FAIL_IF(exists, nullptr, "path is already occupied", IkarusErrorInfo_Filesystem_AlreadyExists);
|
||||
}
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto db,
|
||||
nullptr,
|
||||
"failed to create project db: {}",
|
||||
IkarusErrorInfo_Database_ConnectionFailed,
|
||||
sqlitecpp::Connection::open(path)
|
||||
);
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to migrate project db: {}",
|
||||
IkarusErrorInfo_Database_MigrationFailed,
|
||||
db->migrate(ikarus::MIGRATIONS)
|
||||
);
|
||||
|
||||
if (auto res = db->execute("INSERT INTO `metadata`(`key`, `value`) VALUES(?, ?)", DB_PROJECT_NAME_KEY, name); res.is_error()) {
|
||||
boost::system::error_code ec;
|
||||
fs::remove(fs_path, ec);
|
||||
|
||||
IKARUS_FAIL_IF(
|
||||
ec,
|
||||
nullptr,
|
||||
"failed to remove project db after being unable to insert name into metadata table: {}",
|
||||
IkarusErrorInfo_Filesystem_AccessIssue
|
||||
);
|
||||
|
||||
IKARUS_FAIL(nullptr, "failed to insert project name into metadata: {}", IkarusErrorInfo_Database_QueryFailed);
|
||||
}
|
||||
|
||||
return new IkarusProject{name, path, std::move(db)};
|
||||
}
|
||||
|
||||
IkarusProject * ikarus_project_create_in_memory(char const * name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(name, nullptr);
|
||||
IKARUS_FAIL_IF(cppbase::is_empty_or_blank(name), nullptr, "name must not be empty", IkarusErrorInfo_Client_InvalidInput);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto db,
|
||||
nullptr,
|
||||
"failed to create project db in memory: {}",
|
||||
IkarusErrorInfo_Database_ConnectionFailed,
|
||||
sqlitecpp::Connection::open_in_memory()
|
||||
);
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to migrate project db: {}",
|
||||
IkarusErrorInfo_Database_MigrationFailed,
|
||||
db->migrate(ikarus::MIGRATIONS)
|
||||
);
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to insert project name into metadata: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
db->execute("INSERT INTO `metadata`(`key`, `value`) VALUES(?, ?)", DB_PROJECT_NAME_KEY, name)
|
||||
);
|
||||
|
||||
return new IkarusProject{name, ":memory:", std::move(db)};
|
||||
}
|
||||
|
||||
IkarusProject * ikarus_project_open(char const * path, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(path, nullptr);
|
||||
IKARUS_FAIL_IF(cppbase::is_empty_or_blank(path), nullptr, "path must not be empty", IkarusErrorInfo_Client_InvalidInput);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto db,
|
||||
nullptr,
|
||||
"failed to open project db: {}",
|
||||
IkarusErrorInfo_Database_ConnectionFailed,
|
||||
sqlitecpp::Connection::open(path)
|
||||
);
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to migrate project db: {}",
|
||||
IkarusErrorInfo_Database_MigrationFailed,
|
||||
db->migrate(ikarus::MIGRATIONS)
|
||||
);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const & name,
|
||||
nullptr,
|
||||
"failed to retrieve project name from metadata: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
db->query_one<std::string>("SELECT `value` FROM `metadata` WHERE `key` = ?", DB_PROJECT_NAME_KEY)
|
||||
);
|
||||
|
||||
return new IkarusProject{name, path, std::move(db)};
|
||||
}
|
||||
|
||||
char const * ikarus_project_get_name(IkarusProject const * project, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
|
||||
return project->name.data();
|
||||
}
|
||||
|
||||
void ikarus_project_set_name(IkarusProject * project, char const * new_name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, );
|
||||
IKARUS_FAIL_IF_NULL(new_name, );
|
||||
|
||||
IKARUS_FAIL_IF(cppbase::is_empty_or_blank(new_name), , "name must not be empty", IkarusErrorInfo_Client_InvalidInput);
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"failed to update project name: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->execute("UPDATE `metadata` SET `value` = ? WHERE `key` = ?", new_name, DB_PROJECT_NAME_KEY)
|
||||
);
|
||||
}
|
||||
|
||||
char const * ikarus_project_get_path(IkarusProject const * project, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
|
||||
return project->path.data();
|
||||
}
|
||||
|
||||
// these take a mutable project right now because the get_cached-* function must be mutable
|
||||
// since we insert a backreference to the project into the objects, not ideal,
|
||||
// but fine for now since "mutability" is a vague concept for projects anyway
|
||||
|
||||
void ikarus_project_get_blueprints(
|
||||
IkarusProject * project,
|
||||
struct IkarusBlueprint ** blueprints_out,
|
||||
size_t blueprints_out_size,
|
||||
IkarusErrorData * error_out
|
||||
IkarusProject * ikarus_project_create(
|
||||
char const * path,
|
||||
char const * name,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, );
|
||||
IKARUS_FAIL_IF_NULL(blueprints_out, );
|
||||
IKARUS_FAIL_IF_NULL(path, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(name, nullptr);
|
||||
IKARUS_FAIL_IF(
|
||||
cppbase::is_empty_or_blank(path),
|
||||
nullptr,
|
||||
"path must not be empty",
|
||||
IkarusErrorInfo_Client_InvalidInput
|
||||
);
|
||||
IKARUS_FAIL_IF(
|
||||
cppbase::is_empty_or_blank(name),
|
||||
nullptr,
|
||||
"name must not be empty",
|
||||
IkarusErrorInfo_Client_InvalidInput
|
||||
);
|
||||
|
||||
if (blueprints_out_size == 0) {
|
||||
return;
|
||||
}
|
||||
boost::filesystem::path fs_path{path};
|
||||
|
||||
int64_t ids[blueprints_out_size];
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
bool const exists = boost::filesystem::exists(fs_path, ec);
|
||||
IKARUS_FAIL_IF(
|
||||
ec && ec != boost::system::errc::no_such_file_or_directory,
|
||||
nullptr,
|
||||
fmt::format(
|
||||
"unable to check whether path is occupied: {}",
|
||||
ec.message()
|
||||
),
|
||||
IkarusErrorInfo_Filesystem_AlreadyExists
|
||||
);
|
||||
IKARUS_FAIL_IF(
|
||||
exists,
|
||||
nullptr,
|
||||
"path is already occupied",
|
||||
IkarusErrorInfo_Filesystem_AlreadyExists
|
||||
);
|
||||
}
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to fetch project blueprints from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_many_buffered<int64_t>("SELECT `id` FROM `blueprints`", ids, blueprints_out_size)
|
||||
);
|
||||
if (auto db = create_impl(fs_path.c_str(), name, error_out); !db) {
|
||||
LOG_WARN("creating project failed, removing file at {}", path);
|
||||
|
||||
for (size_t i = 0; i < blueprints_out_size; ++i) {
|
||||
blueprints_out[i] = project->get_blueprint(ids[i]);
|
||||
}
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::remove(fs_path, ec);
|
||||
|
||||
IKARUS_FAIL_IF(
|
||||
ec,
|
||||
nullptr,
|
||||
"failed to remove project db",
|
||||
IkarusErrorInfo_Filesystem_MissingPermissions
|
||||
);
|
||||
|
||||
IKARUS_FAIL(
|
||||
nullptr,
|
||||
"failed to insert project name into metadata: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed
|
||||
);
|
||||
|
||||
return nullptr;
|
||||
} else {
|
||||
return new IkarusProject{name, path, std::move(db)};
|
||||
}
|
||||
}
|
||||
|
||||
size_t ikarus_project_get_blueprint_count(IkarusProject const * project, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, 0);
|
||||
IkarusProject * ikarus_project_create_in_memory(
|
||||
char const * name,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(name, nullptr);
|
||||
IKARUS_FAIL_IF(
|
||||
cppbase::is_empty_or_blank(name),
|
||||
nullptr,
|
||||
"name must not be empty",
|
||||
IkarusErrorInfo_Client_InvalidInput
|
||||
);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
0,
|
||||
"unable to fetch project blueprint count from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_one<int64_t>("SELECT COUNT(*) FROM `blueprints`")
|
||||
);
|
||||
if (auto db = create_impl(":memory:", name, error_out); !db) {
|
||||
return nullptr;
|
||||
} else {
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to migrate project db: {}",
|
||||
IkarusErrorInfo_Database_MigrationFailed,
|
||||
db->migrate(ikarus::MIGRATIONS)
|
||||
);
|
||||
|
||||
return ret;
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to insert project name into metadata: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
db->execute(
|
||||
"INSERT INTO `metadata`(`key`, `value`) VALUES(?, ?)",
|
||||
DB_PROJECT_NAME_KEY,
|
||||
name
|
||||
)
|
||||
);
|
||||
return new IkarusProject{name, ":memory:", std::move(db)};
|
||||
}
|
||||
}
|
||||
|
||||
IkarusProject *
|
||||
ikarus_project_open(char const * path, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(path, nullptr);
|
||||
IKARUS_FAIL_IF(
|
||||
cppbase::is_empty_or_blank(path),
|
||||
nullptr,
|
||||
"path must not be empty",
|
||||
IkarusErrorInfo_Client_InvalidInput
|
||||
);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto db,
|
||||
nullptr,
|
||||
"failed to open project db: {}",
|
||||
IkarusErrorInfo_Database_ConnectionFailed,
|
||||
sqlitecpp::Connection::open(path)
|
||||
);
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
nullptr,
|
||||
"failed to migrate project db: {}",
|
||||
IkarusErrorInfo_Database_MigrationFailed,
|
||||
db->migrate(ikarus::MIGRATIONS)
|
||||
);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const & name,
|
||||
nullptr,
|
||||
"failed to retrieve project name from metadata: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
db->query_one<std::string>(
|
||||
"SELECT `value` FROM `metadata` WHERE `key` = ?",
|
||||
DB_PROJECT_NAME_KEY
|
||||
)
|
||||
);
|
||||
|
||||
return new IkarusProject{name, path, std::move(db)};
|
||||
}
|
||||
|
||||
char const * ikarus_project_get_name(
|
||||
IkarusProject const * project,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
|
||||
return project->name.data();
|
||||
}
|
||||
|
||||
void ikarus_project_set_name(
|
||||
IkarusProject * project,
|
||||
char const * new_name,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, );
|
||||
IKARUS_FAIL_IF_NULL(new_name, );
|
||||
|
||||
IKARUS_FAIL_IF(
|
||||
cppbase::is_empty_or_blank(new_name),
|
||||
,
|
||||
"name must not be empty",
|
||||
IkarusErrorInfo_Client_InvalidInput
|
||||
);
|
||||
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"failed to update project name: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->execute(
|
||||
"UPDATE `metadata` SET `value` = ? WHERE `key` = ?",
|
||||
new_name,
|
||||
DB_PROJECT_NAME_KEY
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
char const * ikarus_project_get_path(
|
||||
IkarusProject const * project,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
|
||||
return project->path.c_str();
|
||||
}
|
||||
|
||||
void ikarus_project_get_entities(
|
||||
IkarusProject * project,
|
||||
struct IkarusEntity ** entities_out,
|
||||
size_t entities_out_size,
|
||||
IkarusErrorData * error_out
|
||||
struct IkarusProject const * project,
|
||||
int64_t * ids_out,
|
||||
uint64_t ids_out_size,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, );
|
||||
IKARUS_FAIL_IF_NULL(entities_out, );
|
||||
IKARUS_FAIL_IF_NULL(project, );
|
||||
IKARUS_FAIL_IF_NULL(ids_out, );
|
||||
|
||||
if (entities_out_size == 0) {
|
||||
return;
|
||||
}
|
||||
if (ids_out == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
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<int64_t>("SELECT `id` FROM `entities`", ids, entities_out_size)
|
||||
);
|
||||
|
||||
for (size_t i = 0; i < entities_out_size; ++i) {
|
||||
entities_out[i] = project->get_entity(ids[i]);
|
||||
}
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to fetch project entities from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_many_buffered<int64_t>(
|
||||
"SELECT `id` FROM `entities`",
|
||||
ids_out,
|
||||
ids_out_size
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
size_t ikarus_project_get_entity_count(IkarusProject const * project, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, 0);
|
||||
size_t ikarus_project_get_entity_count(
|
||||
IkarusProject const * project,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, 0);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
0,
|
||||
"unable to fetch project entity count from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_one<int64_t>("SELECT COUNT(*) FROM `entities`")
|
||||
);
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
0,
|
||||
"unable to fetch project entity count from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_one<int64_t>("SELECT COUNT(`id`) FROM `entities`")
|
||||
);
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct IkarusEntity * get_entity_by_name(IkarusProject * project, char const * name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(name, nullptr);
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
|
||||
void ikarus_project_get_blueprints(
|
||||
struct IkarusProject const * project,
|
||||
int64_t * ids_out,
|
||||
uint64_t ids_out_size,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, );
|
||||
IKARUS_FAIL_IF_NULL(ids_out, );
|
||||
|
||||
// TODO, 'InvalidInput' doesn't really make sense here, we'd need to adjust the macros to support distinguishing between different
|
||||
// errors. In this case `sqlitecpp::MissingRow` and database related errors. Same for the other functions.
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const id,
|
||||
nullptr,
|
||||
"unable to find entity in database: {}",
|
||||
IkarusErrorInfo_Client_InvalidInput,
|
||||
project->db->query_one<int64_t>("SELECT `id` FROM `entities` WHERE `name` = ?", name)
|
||||
);
|
||||
if (ids_out == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
return project->get_entity(id);
|
||||
IKARUS_TRYRV_OR_FAIL(
|
||||
,
|
||||
"unable to fetch project blueprints from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_many_buffered<int64_t>(
|
||||
"SELECT `id` FROM `blueprints`",
|
||||
ids_out,
|
||||
ids_out_size
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
struct IkarusProperty *
|
||||
get_property_by_name(IkarusProject * project, IkarusPropertyScope * scope, char const * name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(name, nullptr);
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
|
||||
size_t ikarus_project_get_blueprint_count(
|
||||
IkarusProject const * project,
|
||||
IkarusErrorData * error_out
|
||||
) {
|
||||
IKARUS_FAIL_IF_NULL(project, 0);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const id_and_type,
|
||||
nullptr,
|
||||
"unable to find property in database: {}",
|
||||
IkarusErrorInfo_Client_InvalidInput,
|
||||
project->db->query_one<int64_t, IkarusValueType>(
|
||||
"SELECT `id`, `type` FROM `properties` WHERE `name` = ? AND `scope` = ?",
|
||||
name,
|
||||
scope->get_id()
|
||||
)
|
||||
);
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
0,
|
||||
"unable to fetch project blueprint count from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_one<int64_t>("SELECT COUNT(`id`) FROM `blueprints`")
|
||||
);
|
||||
|
||||
auto const [id, type] = id_and_type;
|
||||
|
||||
return project->get_property(id, type);
|
||||
}
|
||||
|
||||
IkarusBlueprint * get_blueprints_by_name(IkarusProject * project, char const * name, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(project, nullptr);
|
||||
IKARUS_FAIL_IF_NULL(name, nullptr);
|
||||
IKARUS_FAIL_IF_NAME_INVALID(name, nullptr);
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const id,
|
||||
nullptr,
|
||||
"unable to find blueprint in database: {}",
|
||||
IkarusErrorInfo_Client_InvalidInput,
|
||||
project->db->query_one<int64_t>("SELECT `id` FROM `blueprints` WHERE `name` = ?", name)
|
||||
);
|
||||
|
||||
return project->get_blueprint(id);
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,58 +1,19 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <sqlitecpp/connection.hpp>
|
||||
|
||||
#include <ikarus/errors.h>
|
||||
#include <ikarus/values/value_type.h>
|
||||
#include <ikarus/objects/blueprint.hpp>
|
||||
#include <ikarus/objects/entity.hpp>
|
||||
#include <ikarus/objects/property.hpp>
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
/// \private
|
||||
struct IkarusProject {
|
||||
public:
|
||||
IkarusProject(std::string_view name, std::string_view path, std::unique_ptr<sqlitecpp::Connection> && db);
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto get_blueprint(int64_t id) -> struct IkarusBlueprint *;
|
||||
auto uncache(struct IkarusBlueprint * blueprint) -> void;
|
||||
|
||||
[[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(int64_t id, IkarusValueType type) -> struct IkarusProperty *;
|
||||
auto uncache(struct IkarusProperty * property) -> void;
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
[[nodiscard]] T * get_cached_object(int64_t id, auto & cache) {
|
||||
auto const iter = cache.find(id);
|
||||
|
||||
if (iter == cache.cend()) {
|
||||
return cache.emplace(id, std::make_unique<T>(this, id)).first->second.get();
|
||||
}
|
||||
|
||||
return iter->second.get();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void remove_cached_object(T * object, std::unordered_map<int64_t, std::unique_ptr<T>> & cache) {
|
||||
cache.erase(object->id);
|
||||
}
|
||||
|
||||
public:
|
||||
std::string name;
|
||||
std::string_view path;
|
||||
std::unique_ptr<sqlitecpp::Connection> db;
|
||||
|
||||
private:
|
||||
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;
|
||||
std::string name;
|
||||
boost::filesystem::path path;
|
||||
std::unique_ptr<sqlitecpp::Connection> db;
|
||||
};
|
||||
|
||||
constexpr std::string_view DB_PROJECT_NAME_KEY = "PROJECT_NAME";
|
||||
|
|
|
|||
217
src/ikarus/values/data.cpp
Normal file
217
src/ikarus/values/data.cpp
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
#include "data.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <cppbase/result.hpp>
|
||||
#include <cppbase/templates.hpp>
|
||||
|
||||
#include <ikarus/values/data.h>
|
||||
#include <ikarus/values/errors.hpp>
|
||||
#include <ikarus/values/schema.h>
|
||||
#include <ikarus/values/shared.hpp>
|
||||
|
||||
auto get_primitive_type(IkarusValueDataPrimitive const & primitive)
|
||||
-> IkarusValuePrimitiveType {
|
||||
return std::visit(
|
||||
cppbase::overloaded{
|
||||
[](IkarusValueDataPrimitiveToggle const &) {
|
||||
return IkarusValuePrimitiveType_Toggle;
|
||||
},
|
||||
[](IkarusValueDataPrimitiveNumber const &) {
|
||||
return IkarusValuePrimitiveType_Number;
|
||||
},
|
||||
[](IkarusValueDataPrimitiveString const &) {
|
||||
return IkarusValuePrimitiveType_Text;
|
||||
}
|
||||
},
|
||||
primitive
|
||||
);
|
||||
}
|
||||
|
||||
auto IkarusValueData::from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValueData, IkarusValueDataParseError> {
|
||||
if (!json.is_object()) {
|
||||
return cppbase::err(IkarusJsonError{IkarusJsonInvalidTypeError{}});
|
||||
}
|
||||
|
||||
IkarusValueData value{};
|
||||
|
||||
VTRY(
|
||||
auto type,
|
||||
deserialize_enum<IkarusValueDataType>(
|
||||
json,
|
||||
"type",
|
||||
IkarusValueDataType_Primitive,
|
||||
IkarusValueDataType_Tuple
|
||||
)
|
||||
);
|
||||
|
||||
switch (type) {
|
||||
case IkarusValueDataType_Primitive: {
|
||||
VTRY(
|
||||
auto primitive,
|
||||
deserialize_enum<IkarusValuePrimitiveType>(
|
||||
json,
|
||||
"primitive",
|
||||
IkarusValuePrimitiveType_Toggle,
|
||||
IkarusValuePrimitiveType_Text
|
||||
)
|
||||
);
|
||||
|
||||
switch (primitive) {
|
||||
case IkarusValuePrimitiveType_Toggle: {
|
||||
VTRY(auto data, deserialize_any<bool>(json, "data"));
|
||||
|
||||
value.variant = IkarusValueDataPrimitiveToggle{data};
|
||||
break;
|
||||
}
|
||||
case IkarusValuePrimitiveType_Number: {
|
||||
VTRY(auto data, deserialize_any<double>(json, "data"));
|
||||
|
||||
value.variant = IkarusValueDataPrimitiveNumber{data};
|
||||
break;
|
||||
}
|
||||
case IkarusValuePrimitiveType_Text: {
|
||||
VTRY(auto data, deserialize_any<std::string>(json, "data"));
|
||||
|
||||
value.variant = IkarusValueDataPrimitiveString{data};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case IkarusValueDataType_List: {
|
||||
std::vector<cppbase::owning_ptr<IkarusValueData>> values_data{};
|
||||
VTRY(
|
||||
auto data_json,
|
||||
deserialize_any<std::vector<nlohmann::json>>(json, "data")
|
||||
);
|
||||
|
||||
values_data.reserve(data_json.size());
|
||||
for (auto const & data_json : data_json) {
|
||||
VTRY(auto value_data, IkarusValueData::from_json(data_json));
|
||||
values_data.emplace_back(
|
||||
cppbase::make_owning<IkarusValueData>(std::move(value_data))
|
||||
);
|
||||
}
|
||||
|
||||
value.variant = IkarusValueDataList{values_data};
|
||||
break;
|
||||
}
|
||||
case IkarusValueDataType_Map: {
|
||||
std::vector<std::pair<
|
||||
cppbase::owning_ptr<IkarusValueData>,
|
||||
cppbase::owning_ptr<IkarusValueData>>>
|
||||
map_data{};
|
||||
|
||||
VTRY(
|
||||
auto map_data_json,
|
||||
deserialize_any<std::vector<nlohmann::json>>(json, "data")
|
||||
);
|
||||
|
||||
map_data.reserve(map_data_json.size());
|
||||
|
||||
for (auto const & pair_json : map_data_json) {
|
||||
VTRY(auto key_json, get_key(pair_json, "key"));
|
||||
VTRY(auto value_json, get_key(pair_json, "value"));
|
||||
VTRY(auto key, IkarusValueData::from_json(*key_json));
|
||||
VTRY(auto value, IkarusValueData::from_json(*value_json));
|
||||
|
||||
map_data.emplace_back(
|
||||
cppbase::make_owning<IkarusValueData>(key),
|
||||
cppbase::make_owning<IkarusValueData>(value)
|
||||
);
|
||||
}
|
||||
|
||||
value.variant = IkarusValueDataMap{map_data};
|
||||
break;
|
||||
}
|
||||
case IkarusValueDataType_Tuple: {
|
||||
std::vector<cppbase::owning_ptr<IkarusValueData>> values_data{};
|
||||
|
||||
VTRY(
|
||||
auto values_json,
|
||||
deserialize_any<std::vector<nlohmann::json>>(json, "data")
|
||||
);
|
||||
|
||||
values_data.reserve(values_json.size());
|
||||
|
||||
for (auto const & value_json : values_json) {
|
||||
VTRY(auto value_data, IkarusValueData::from_json(value_json));
|
||||
values_data.emplace_back(
|
||||
cppbase::make_owning<IkarusValueData>(value_data)
|
||||
);
|
||||
}
|
||||
|
||||
value.variant = IkarusValueDataTuple{values_data};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return cppbase::ok(value);
|
||||
}
|
||||
|
||||
auto IkarusValueData::to_json(
|
||||
nlohmann::json & json,
|
||||
IkarusValueData const & value
|
||||
) -> void {
|
||||
std::visit(
|
||||
cppbase::overloaded{
|
||||
[&](IkarusValueDataPrimitive const & primitive) {
|
||||
std::visit(
|
||||
cppbase::overloaded{
|
||||
[&](IkarusValueDataPrimitiveToggle const & toggle) {
|
||||
json["type"] = IkarusValueDataType_Primitive;
|
||||
json["primitive"] = IkarusValuePrimitiveType_Toggle;
|
||||
json["data"] = toggle.value;
|
||||
},
|
||||
[&](IkarusValueDataPrimitiveNumber const & number) {
|
||||
json["type"] = IkarusValueDataType_Primitive;
|
||||
json["primitive"] = IkarusValuePrimitiveType_Number;
|
||||
json["data"] = number.value;
|
||||
},
|
||||
[&](IkarusValueDataPrimitiveString const & string) {
|
||||
json["type"] = IkarusValueDataType_Primitive;
|
||||
json["primitive"] = IkarusValuePrimitiveType_Text;
|
||||
json["data"] = string.value;
|
||||
}
|
||||
},
|
||||
primitive
|
||||
);
|
||||
},
|
||||
[&](IkarusValueDataList const & list) {
|
||||
json["type"] = IkarusValueDataType_List;
|
||||
json["data"] = list.values |
|
||||
std::views::transform([](auto const & data) {
|
||||
nlohmann::json j;
|
||||
IkarusValueData::to_json(j, *data);
|
||||
return j;
|
||||
}) |
|
||||
std::ranges::to<std::vector<nlohmann::json>>();
|
||||
},
|
||||
[&](IkarusValueDataMap const & map) {
|
||||
json["type"] = IkarusValueDataType_Map;
|
||||
json["data"] =
|
||||
map.values | std::views::transform([](auto const & pair) {
|
||||
nlohmann::json j;
|
||||
IkarusValueData::to_json(j["key"], *pair.first);
|
||||
IkarusValueData::to_json(j["value"], *pair.second);
|
||||
return j;
|
||||
}) |
|
||||
std::ranges::to<std::vector<nlohmann::json>>();
|
||||
},
|
||||
[&](IkarusValueDataTuple const & tuple) {
|
||||
json["type"] = IkarusValueDataType_Tuple;
|
||||
json["data"] = tuple.values |
|
||||
std::views::transform([](auto const & data) {
|
||||
nlohmann::json j;
|
||||
IkarusValueData::to_json(j, *data);
|
||||
return j;
|
||||
}) |
|
||||
std::ranges::to<std::vector<nlohmann::json>>();
|
||||
}
|
||||
},
|
||||
value.variant
|
||||
);
|
||||
}
|
||||
65
src/ikarus/values/data.hpp
Normal file
65
src/ikarus/values/data.hpp
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#pragma once
|
||||
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <cppbase/pointer.hpp>
|
||||
#include <cppbase/result.hpp>
|
||||
|
||||
#include <ikarus/values/data.h>
|
||||
#include <ikarus/values/errors.hpp>
|
||||
#include <ikarus/values/schema.h>
|
||||
|
||||
struct IkarusValueData;
|
||||
|
||||
struct IkarusValueDataPrimitiveToggle {
|
||||
bool value;
|
||||
};
|
||||
|
||||
struct IkarusValueDataPrimitiveNumber {
|
||||
double value;
|
||||
};
|
||||
|
||||
struct IkarusValueDataPrimitiveString {
|
||||
std::string value;
|
||||
};
|
||||
|
||||
using IkarusValueDataPrimitive = std::variant<
|
||||
IkarusValueDataPrimitiveToggle,
|
||||
IkarusValueDataPrimitiveNumber,
|
||||
IkarusValueDataPrimitiveString>;
|
||||
|
||||
struct IkarusValueDataList {
|
||||
std::vector<cppbase::owning_ptr<IkarusValueData>> values;
|
||||
};
|
||||
|
||||
struct IkarusValueDataMap {
|
||||
std::vector<std::pair<
|
||||
cppbase::owning_ptr<IkarusValueData>,
|
||||
cppbase::owning_ptr<IkarusValueData>>>
|
||||
values;
|
||||
};
|
||||
|
||||
struct IkarusValueDataTuple {
|
||||
std::vector<cppbase::owning_ptr<IkarusValueData>> values;
|
||||
};
|
||||
|
||||
struct IkarusValueData {
|
||||
using IkarusValueDataVariant = std::variant<
|
||||
IkarusValueDataPrimitive,
|
||||
IkarusValueDataList,
|
||||
IkarusValueDataMap,
|
||||
IkarusValueDataTuple>;
|
||||
|
||||
static auto from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValueData, IkarusValueDataParseError>;
|
||||
static auto to_json(nlohmann::json & json, IkarusValueData const & value)
|
||||
-> void;
|
||||
|
||||
IkarusValueDataVariant variant;
|
||||
};
|
||||
|
||||
auto get_primitive_type(IkarusValueDataPrimitive const & primitive)
|
||||
-> IkarusValuePrimitiveType;
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
#include "entity_property_value.hpp"
|
||||
|
||||
#include <ikarus/errors.hpp>
|
||||
|
||||
IkarusEntity const * ikarus_entity_property_value_get_entity(IkarusEntityPropertyValue const * value, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(value, nullptr);
|
||||
|
||||
return value->entity;
|
||||
}
|
||||
|
||||
IkarusProperty const * ikarus_entity_property_value_get_property(IkarusEntityPropertyValue const * value, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(value, nullptr);
|
||||
|
||||
return value->property;
|
||||
}
|
||||
|
||||
IkarusValue const * ikarus_entity_property_value_get_value(IkarusEntityPropertyValue const * value, IkarusErrorData * error_out) {
|
||||
IKARUS_FAIL_IF_NULL(value, nullptr);
|
||||
|
||||
return value->value;
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
#pragma once
|
||||
36
src/ikarus/values/errors.hpp
Normal file
36
src/ikarus/values/errors.hpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include <variant>
|
||||
|
||||
struct IkarusJsonMissingKeyError {};
|
||||
|
||||
struct IkarusJsonInvalidTypeError {};
|
||||
|
||||
struct IkarusJsonEnumOutOfBoundsError {};
|
||||
|
||||
struct IkarusJsonUnknownError {};
|
||||
|
||||
using IkarusJsonError = std::variant<
|
||||
IkarusJsonMissingKeyError,
|
||||
IkarusJsonInvalidTypeError,
|
||||
IkarusJsonEnumOutOfBoundsError,
|
||||
IkarusJsonUnknownError>;
|
||||
|
||||
struct IkarusValueSchemaParseError {
|
||||
IkarusJsonError error;
|
||||
};
|
||||
|
||||
struct IkarusValueDataParseError {
|
||||
IkarusJsonError error;
|
||||
};
|
||||
|
||||
struct IkarusValueParseErrorDataSchemaMismatch {};
|
||||
|
||||
using IkarusValueParseError = std::variant<
|
||||
IkarusJsonError,
|
||||
IkarusValueSchemaParseError,
|
||||
IkarusValueDataParseError,
|
||||
IkarusValueParseErrorDataSchemaMismatch>;
|
||||
|
||||
using IkarusValuesParseError =
|
||||
std::variant<IkarusJsonMissingKeyError, IkarusValueParseError>;
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
#include "ikarus/values/number_value.h"
|
||||
|
||||
#include <boost/functional/overloaded_function.hpp>
|
||||
|
||||
#include <ikarus/values/number_value.hpp>
|
||||
#include <ikarus/values/value_base.hpp>
|
||||
|
||||
IkarusNumberValue::IkarusNumberValue():
|
||||
IkarusValueData{this} {}
|
||||
|
||||
IkarusNumberValue * ikarus_number_value_data_create() {
|
||||
return new IkarusNumberValue{};
|
||||
}
|
||||
|
||||
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_data_get_size(IkarusNumberValue const * value) {
|
||||
return ikarus_value_data_base_get_size(value);
|
||||
}
|
||||
|
||||
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_data_remove(IkarusNumberValue * value, size_t idx) {
|
||||
return ikarus_value_data_base_remove(value, idx);
|
||||
}
|
||||
|
||||
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_data_clear(IkarusNumberValue * value) {
|
||||
return ikarus_value_data_base_clear(value);
|
||||
}
|
||||
|
||||
bool ikarus_number_value_data_is_undefined(IkarusNumberValue const * value) {
|
||||
return ikarus_value_data_base_is_undefined(value);
|
||||
}
|
||||
|
||||
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_data_to_string(IkarusNumberValue const * value) {
|
||||
return ikarus_value_data_base_to_string(value, [](auto const & value) { return value; });
|
||||
}
|
||||
|
||||
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_data_copy(IkarusNumberValue const * value) {
|
||||
return ikarus_value_data_base_copy(value);
|
||||
}
|
||||
|
||||
struct IkarusValueData * ikarus_number_value_data_to_value(IkarusNumberValue * value) {
|
||||
return ikarus_value_data_base_to_value(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);
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
#include <boost/variant2.hpp>
|
||||
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
struct IkarusNumberValue : IkarusValueData {
|
||||
public:
|
||||
using DataType = double;
|
||||
|
||||
public:
|
||||
explicit IkarusNumberValue();
|
||||
|
||||
IkarusNumberValue(IkarusNumberValue const &) = default;
|
||||
IkarusNumberValue(IkarusNumberValue &&) = default;
|
||||
|
||||
IkarusNumberValue & operator=(IkarusNumberValue const &) = default;
|
||||
IkarusNumberValue & operator=(IkarusNumberValue &&) = default;
|
||||
|
||||
~IkarusNumberValue() override = default;
|
||||
|
||||
public:
|
||||
boost::variant2::variant<boost::variant2::monostate, boost::container::small_vector<DataType, SMALL_VEC_VALUE_SIZE>> data{};
|
||||
};
|
||||
177
src/ikarus/values/schema.cpp
Normal file
177
src/ikarus/values/schema.cpp
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
#include "schema.hpp"
|
||||
|
||||
#include <cppbase/templates.hpp>
|
||||
|
||||
#include <ikarus/values/data.hpp>
|
||||
#include <ikarus/values/schema.h>
|
||||
#include <ikarus/values/shared.hpp>
|
||||
|
||||
auto IkarusValueSchema::from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValueSchema, IkarusValueSchemaParseError> {
|
||||
if (!json.is_object()) {
|
||||
return cppbase::err(
|
||||
IkarusValueSchemaParseError{
|
||||
IkarusJsonError{IkarusJsonInvalidTypeError{}}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
IkarusValueSchema schema{};
|
||||
|
||||
VTRY(
|
||||
auto type,
|
||||
deserialize_enum<IkarusValueSchemaType>(
|
||||
json,
|
||||
"type",
|
||||
IkarusValueSchemaType_Primitive,
|
||||
IkarusValueSchemaType_Tuple
|
||||
)
|
||||
);
|
||||
|
||||
switch (type) {
|
||||
case IkarusValueSchemaType_Primitive: {
|
||||
VTRY(
|
||||
auto primitive,
|
||||
deserialize_enum<IkarusValuePrimitiveType>(
|
||||
json,
|
||||
"primitive",
|
||||
IkarusValuePrimitiveType_Toggle,
|
||||
IkarusValuePrimitiveType_Text
|
||||
)
|
||||
);
|
||||
schema.variant = IkarusValueSchemaPrimitive{primitive};
|
||||
break;
|
||||
}
|
||||
case IkarusValueSchemaType_List: {
|
||||
VTRY(auto sub_schema, IkarusValueSchema::from_json(json["schema"]));
|
||||
schema.variant = IkarusValueSchemaList{
|
||||
cppbase::make_owning<IkarusValueSchema>(std::move(sub_schema))
|
||||
};
|
||||
break;
|
||||
}
|
||||
case IkarusValueSchemaType_Map: {
|
||||
VTRY(auto key_schema, IkarusValueSchema::from_json(json["key_schema"]));
|
||||
VTRY(
|
||||
auto value_schema,
|
||||
IkarusValueSchema::from_json(json["value_schema"])
|
||||
);
|
||||
schema.variant = IkarusValueSchemaMap{
|
||||
cppbase::make_owning<IkarusValueSchema>(std::move(key_schema)),
|
||||
cppbase::make_owning<IkarusValueSchema>(std::move(value_schema))
|
||||
};
|
||||
break;
|
||||
}
|
||||
case IkarusValueSchemaType_Tuple: {
|
||||
VTRY(
|
||||
auto sub_schemas_json,
|
||||
deserialize_any<std::vector<nlohmann::json>>(json, "schemas")
|
||||
);
|
||||
|
||||
std::vector<cppbase::owning_ptr<IkarusValueSchema>> sub_schemas{};
|
||||
sub_schemas.reserve(sub_schemas_json.size());
|
||||
|
||||
for (auto const & sub_schema_json : sub_schemas_json) {
|
||||
VTRY(auto schema, IkarusValueSchema::from_json(sub_schema_json));
|
||||
sub_schemas.emplace_back(
|
||||
cppbase::make_owning<IkarusValueSchema>(std::move(schema))
|
||||
);
|
||||
}
|
||||
|
||||
schema.variant = IkarusValueSchemaTuple{sub_schemas};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return cppbase::ok(std::move(schema));
|
||||
}
|
||||
|
||||
auto IkarusValueSchema::to_json(
|
||||
nlohmann::json & json,
|
||||
IkarusValueSchema const & schema
|
||||
) -> void {
|
||||
std::visit(
|
||||
cppbase::overloaded{
|
||||
[&json](IkarusValueSchemaPrimitive const & schema) {
|
||||
json["type"] = IkarusValueSchemaType_Primitive;
|
||||
json["primitive"] = schema.type;
|
||||
},
|
||||
[&json](IkarusValueSchemaList const & schema) {
|
||||
json["type"] = IkarusValueSchemaType_List;
|
||||
IkarusValueSchema::to_json(json["schema"], *schema.sub_schema);
|
||||
},
|
||||
[&json](IkarusValueSchemaMap const & schema) {
|
||||
json["type"] = IkarusValueSchemaType_Map;
|
||||
IkarusValueSchema::to_json(
|
||||
json["key_schema"],
|
||||
*schema.key_schema
|
||||
);
|
||||
IkarusValueSchema::to_json(
|
||||
json["value_schema"],
|
||||
*schema.value_schema
|
||||
);
|
||||
},
|
||||
[&json](IkarusValueSchemaTuple const & schema) {
|
||||
json["type"] = IkarusValueSchemaType_Tuple;
|
||||
|
||||
std::vector<nlohmann::json> sub_schemas{};
|
||||
sub_schemas.reserve(schema.sub_schemas.size());
|
||||
|
||||
for (auto const & sub_schema : schema.sub_schemas) {
|
||||
nlohmann::json sub_schema_json{};
|
||||
IkarusValueSchema::to_json(sub_schema_json, *sub_schema);
|
||||
sub_schemas.push_back(sub_schema_json);
|
||||
}
|
||||
|
||||
json["schemas"] = sub_schemas;
|
||||
}
|
||||
},
|
||||
schema.variant
|
||||
);
|
||||
}
|
||||
|
||||
auto IkarusValueSchema::validate(IkarusValueData const & data) const -> bool {
|
||||
return std::visit(
|
||||
cppbase::overloaded{
|
||||
[](IkarusValueSchemaPrimitive const & schema,
|
||||
IkarusValueDataPrimitive const & data) {
|
||||
return get_primitive_type(data) == schema.type;
|
||||
},
|
||||
[](IkarusValueSchemaList const & schema,
|
||||
IkarusValueDataList const & data) {
|
||||
for (auto const & value : data.values) {
|
||||
if (!schema.sub_schema->validate(*value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
},
|
||||
[](IkarusValueSchemaMap const & schema,
|
||||
IkarusValueDataMap const & data) {
|
||||
for (auto const & pair : data.values) {
|
||||
if (!schema.key_schema->validate(*pair.first) ||
|
||||
!schema.value_schema->validate(*pair.second)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
},
|
||||
[](IkarusValueSchemaTuple const & schema,
|
||||
IkarusValueDataTuple const & data) {
|
||||
if (schema.sub_schemas.size() != data.values.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < schema.sub_schemas.size(); ++i) {
|
||||
if (!schema.sub_schemas[i]->validate(*data.values[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
[](auto const &, auto const &) { return false; }
|
||||
},
|
||||
variant,
|
||||
data.variant
|
||||
);
|
||||
}
|
||||
51
src/ikarus/values/schema.hpp
Normal file
51
src/ikarus/values/schema.hpp
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <cppbase/pointer.hpp>
|
||||
#include <cppbase/result.hpp>
|
||||
|
||||
#include <ikarus/values/errors.hpp>
|
||||
#include <ikarus/values/schema.h>
|
||||
|
||||
struct IkarusValue;
|
||||
struct IkarusValueData;
|
||||
struct IkarusValueSchema;
|
||||
|
||||
struct IkarusValueSchemaPrimitive {
|
||||
IkarusValuePrimitiveType type;
|
||||
};
|
||||
|
||||
struct IkarusValueSchemaList {
|
||||
cppbase::owning_ptr<IkarusValueSchema> sub_schema;
|
||||
};
|
||||
|
||||
struct IkarusValueSchemaMap {
|
||||
cppbase::owning_ptr<IkarusValueSchema> key_schema;
|
||||
cppbase::owning_ptr<IkarusValueSchema> value_schema;
|
||||
};
|
||||
|
||||
struct IkarusValueSchemaTuple {
|
||||
std::vector<cppbase::owning_ptr<IkarusValueSchema>> sub_schemas;
|
||||
};
|
||||
|
||||
struct IkarusValueSchema {
|
||||
using IkarusValueSchemaVariant = std::variant<
|
||||
IkarusValueSchemaPrimitive,
|
||||
IkarusValueSchemaList,
|
||||
IkarusValueSchemaMap,
|
||||
IkarusValueSchemaTuple>;
|
||||
|
||||
static auto from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValueSchema, IkarusValueSchemaParseError>;
|
||||
static auto to_json(nlohmann::json & json, IkarusValueSchema const & value)
|
||||
-> void;
|
||||
|
||||
auto validate(IkarusValueData const & data) const -> bool;
|
||||
|
||||
IkarusValueSchemaVariant variant;
|
||||
};
|
||||
65
src/ikarus/values/shared.hpp
Normal file
65
src/ikarus/values/shared.hpp
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#pragma once
|
||||
|
||||
#include "errors.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <cppbase/result.hpp>
|
||||
#include <cppbase/templates.hpp>
|
||||
|
||||
inline auto get_key(nlohmann::json const & json, std::string_view key)
|
||||
-> cppbase::
|
||||
Result<nlohmann::json::const_iterator, IkarusJsonMissingKeyError> {
|
||||
auto iter = json.find(key);
|
||||
if (iter == json.end()) {
|
||||
return cppbase::err(IkarusJsonMissingKeyError{});
|
||||
}
|
||||
|
||||
return cppbase::ok(iter);
|
||||
}
|
||||
|
||||
template<cppbase::Enum E>
|
||||
auto serialize_enum(nlohmann::json & json, std::string_view key, E value)
|
||||
-> void {
|
||||
json[key] = static_cast<std::underlying_type_t<E>>(value);
|
||||
}
|
||||
|
||||
template<cppbase::Enum E>
|
||||
requires std::is_integral_v<std::underlying_type_t<E>>
|
||||
auto deserialize_enum(
|
||||
nlohmann::json const & json,
|
||||
std::string_view key,
|
||||
E min,
|
||||
E max
|
||||
) -> cppbase::Result<E, IkarusJsonError> {
|
||||
VTRY(auto iter, get_key(json, key));
|
||||
|
||||
if (!iter->is_number_integer()) {
|
||||
return cppbase::err(IkarusJsonError{});
|
||||
}
|
||||
|
||||
auto value = iter->get<std::underlying_type_t<E>>();
|
||||
|
||||
if (value < static_cast<std::underlying_type_t<E>>(min) ||
|
||||
value > static_cast<std::underlying_type_t<E>>(max)) {
|
||||
return cppbase::err(IkarusJsonError{});
|
||||
}
|
||||
|
||||
return cppbase::ok(static_cast<E>(value));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
auto deserialize_any(nlohmann::json const & json, std::string_view key)
|
||||
-> cppbase::Result<T, IkarusJsonError> {
|
||||
VTRY(auto iter, get_key(json, key));
|
||||
|
||||
try {
|
||||
return cppbase::ok(iter->get<T>());
|
||||
} catch (nlohmann::json::type_error const &) {
|
||||
return cppbase::err(IkarusJsonInvalidTypeError{});
|
||||
} catch (nlohmann::json::out_of_range const &) {
|
||||
return cppbase::err(IkarusJsonEnumOutOfBoundsError{});
|
||||
} catch (nlohmann::json::exception const &) {
|
||||
return cppbase::err(IkarusJsonUnknownError{});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
#include "ikarus/values/text_value.h"
|
||||
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/functional/overloaded_function.hpp>
|
||||
|
||||
#include <ikarus/values/text_value.hpp>
|
||||
#include <ikarus/values/value_base.hpp>
|
||||
|
||||
IkarusTextValue::IkarusTextValue():
|
||||
IkarusValueData{this} {}
|
||||
|
||||
IkarusTextValue * ikarus_text_value_data_create() {
|
||||
return new IkarusTextValue{};
|
||||
}
|
||||
|
||||
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_data_get_size(IkarusTextValue const * value) {
|
||||
return ikarus_value_data_base_get_size(value);
|
||||
}
|
||||
|
||||
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_data_remove(IkarusTextValue * value, size_t idx) {
|
||||
return ikarus_value_data_base_remove(value, idx);
|
||||
}
|
||||
|
||||
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_data_clear(IkarusTextValue * value) {
|
||||
return ikarus_value_data_base_clear(value);
|
||||
}
|
||||
|
||||
bool ikarus_text_value_data_is_undefined(IkarusTextValue const * value) {
|
||||
return ikarus_value_data_base_is_undefined(value);
|
||||
}
|
||||
|
||||
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_data_to_string(IkarusTextValue const * value) {
|
||||
return ikarus_value_data_base_to_string(value, [](auto const & value) { return value; });
|
||||
}
|
||||
|
||||
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_data_copy(IkarusTextValue const * value) {
|
||||
return ikarus_value_data_base_copy(value);
|
||||
}
|
||||
|
||||
struct IkarusValueData * ikarus_text_value_data_to_value(IkarusTextValue * value) {
|
||||
return ikarus_value_data_base_to_value(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);
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
struct IkarusTextValue : IkarusValueData {
|
||||
public:
|
||||
using DataType = std::string;
|
||||
|
||||
public:
|
||||
explicit IkarusTextValue();
|
||||
|
||||
IkarusTextValue(IkarusTextValue const &) = default;
|
||||
IkarusTextValue(IkarusTextValue &&) = default;
|
||||
|
||||
IkarusTextValue & operator=(IkarusTextValue const &) = default;
|
||||
IkarusTextValue & operator=(IkarusTextValue &&) = default;
|
||||
|
||||
~IkarusTextValue() override = default;
|
||||
|
||||
public:
|
||||
boost::variant2::variant<boost::variant2::monostate, boost::container::small_vector<DataType, SMALL_VEC_VALUE_SIZE>> data{};
|
||||
};
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
#include "ikarus/values/toggle_value.h"
|
||||
|
||||
#include <boost/functional/overloaded_function.hpp>
|
||||
#include <boost/range/adaptors.hpp>
|
||||
|
||||
#include <ikarus/values/toggle_value.hpp>
|
||||
#include <ikarus/values/value_base.hpp>
|
||||
|
||||
IkarusToggleValue::IkarusToggleValue():
|
||||
IkarusValueData{this} {}
|
||||
|
||||
IkarusToggleValue * ikarus_toggle_value_data_create() {
|
||||
return new IkarusToggleValue{};
|
||||
}
|
||||
|
||||
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_data_get_size(IkarusToggleValue const * value) {
|
||||
return ikarus_value_data_base_get_size(value);
|
||||
}
|
||||
|
||||
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_data_remove(IkarusToggleValue * value, size_t idx) {
|
||||
return ikarus_value_data_base_remove(value, idx);
|
||||
}
|
||||
|
||||
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_data_clear(IkarusToggleValue * value) {
|
||||
return ikarus_value_data_base_clear(value);
|
||||
}
|
||||
|
||||
bool ikarus_toggle_value_data_is_undefined(IkarusToggleValue const * value) {
|
||||
return ikarus_value_data_base_is_undefined(value);
|
||||
}
|
||||
|
||||
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_data_to_string(IkarusToggleValue const * value) {
|
||||
return ikarus_value_data_base_to_string(value, [](auto const & value) { return value ? "✓" : "✗"; });
|
||||
}
|
||||
|
||||
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_data_copy(IkarusToggleValue const * value) {
|
||||
return ikarus_value_data_base_copy(value);
|
||||
}
|
||||
|
||||
struct IkarusValueData * ikarus_toggle_value_data_to_value(IkarusToggleValue * value) {
|
||||
return ikarus_value_data_base_to_value(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);
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include <ikarus/values/value.hpp>
|
||||
|
||||
struct IkarusToggleValue : IkarusValueData {
|
||||
public:
|
||||
using DataType = bool;
|
||||
|
||||
public:
|
||||
explicit IkarusToggleValue();
|
||||
|
||||
IkarusToggleValue(IkarusToggleValue const &) = default;
|
||||
IkarusToggleValue(IkarusToggleValue &&) = default;
|
||||
|
||||
IkarusToggleValue & operator=(IkarusToggleValue const &) = default;
|
||||
IkarusToggleValue & operator=(IkarusToggleValue &&) = default;
|
||||
|
||||
~IkarusToggleValue() override = default;
|
||||
|
||||
public:
|
||||
boost::variant2::variant<boost::variant2::monostate, boost::container::small_vector<DataType, SMALL_VEC_VALUE_SIZE>> data{};
|
||||
};
|
||||
|
|
@ -1,134 +1,58 @@
|
|||
#include "ikarus/values/value.h"
|
||||
#include "value.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <ikarus/values/data.h>
|
||||
#include <ikarus/values/data.hpp>
|
||||
#include <ikarus/values/schema.h>
|
||||
#include <ikarus/values/schema.hpp>
|
||||
#include <ikarus/values/shared.hpp>
|
||||
#include <ikarus/values/value.h>
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
#include <boost/json/src.hpp>
|
||||
auto IkarusValue::from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValue, IkarusValueParseError> {
|
||||
if (!json.is_object()) {
|
||||
return cppbase::err(IkarusJsonError{IkarusJsonInvalidTypeError{}});
|
||||
}
|
||||
|
||||
// required for header-only inclusion
|
||||
#include <cppbase/templates.hpp>
|
||||
IkarusValue value{};
|
||||
|
||||
#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>
|
||||
VTRY(value.schema, IkarusValueSchema::from_json(json["schema"]));
|
||||
VTRY(value.data, IkarusValueData::from_json(json["data"]));
|
||||
|
||||
IkarusValue::IkarusValue(Data data, IkarusValueCardinality cardinality):
|
||||
data{data},
|
||||
cardinality{cardinality} {}
|
||||
if (!value.schema.validate(value.data)) {
|
||||
return cppbase::err(IkarusValueParseErrorDataSchemaMismatch{});
|
||||
}
|
||||
|
||||
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 {
|
||||
int64_t const * type;
|
||||
int64_t const * cardinality;
|
||||
boost::json::value const * data;
|
||||
|
||||
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 (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{});
|
||||
}
|
||||
|
||||
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{};
|
||||
} else {
|
||||
auto res =
|
||||
boost::json::try_value_to<boost::container::small_vector<typename T::DataType, IkarusValue::SMALL_VEC_VALUE_SIZE>>(*data
|
||||
);
|
||||
|
||||
if (res.has_error()) {
|
||||
return cppbase::err(FromJsonError{});
|
||||
}
|
||||
|
||||
ret = new T{};
|
||||
ret->data = std::move(res.value());
|
||||
}
|
||||
|
||||
return cppbase::ok(ret);
|
||||
};
|
||||
|
||||
switch (*type) {
|
||||
case IkarusValueType_Toggle: {
|
||||
return create_value.operator()<IkarusToggleValue>();
|
||||
}
|
||||
case IkarusValueType_Number: {
|
||||
return create_value.operator()<IkarusNumberValue>();
|
||||
}
|
||||
case IkarusValueType_Text: {
|
||||
return create_value.operator()<IkarusTextValue>();
|
||||
}
|
||||
default: {
|
||||
return cppbase::err(FromJsonError{});
|
||||
}
|
||||
}
|
||||
}
|
||||
return cppbase::ok(std::move(value));
|
||||
}
|
||||
|
||||
boost::json::value IkarusValue::to_json() const {
|
||||
auto type = std::visit(
|
||||
cppbase::overloaded{
|
||||
[]([[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 = std::visit(
|
||||
[]<typename T>(T const * value) -> boost::json::value {
|
||||
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); }
|
||||
},
|
||||
value->data
|
||||
);
|
||||
},
|
||||
data
|
||||
);
|
||||
|
||||
return {
|
||||
{ IKARUS_VALUE_JSON_TYPE_KEY, type},
|
||||
{IKARUS_VALUE_JSON_CARDINALITY_KEY, cardinality},
|
||||
{ IKARUS_VALUE_JSON_DATA_KEY, data_json}
|
||||
};
|
||||
auto IkarusValue::to_json(nlohmann::json & json, IkarusValue const & value)
|
||||
-> void {
|
||||
IkarusValueSchema::to_json(json["schema"], value.schema);
|
||||
IkarusValueData::to_json(json["data"], value.data);
|
||||
}
|
||||
|
||||
void ikarus_value_visit(
|
||||
IkarusValue * value,
|
||||
void (*toggle_visitor)(IkarusToggleValue *, void *),
|
||||
void (*number_visitor)(IkarusNumberValue *, void *),
|
||||
void (*text_visitor)(IkarusTextValue *, void *),
|
||||
void * data
|
||||
) {
|
||||
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); },
|
||||
[text_visitor, data](IkarusTextValue * text_value) { text_visitor(text_value, data); }
|
||||
},
|
||||
value->data
|
||||
);
|
||||
auto IkarusValues::from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValues, IkarusValuesParseError> {
|
||||
IkarusValues values{};
|
||||
|
||||
for (auto const & json_entry : json) {
|
||||
VTRY(auto name_json, get_key(json_entry, "name"));
|
||||
|
||||
if (!name_json->is_string()) {
|
||||
return cppbase::err(IkarusJsonInvalidTypeError{});
|
||||
}
|
||||
|
||||
VTRY(auto value_json, get_key(json_entry, "value"));
|
||||
|
||||
VTRY(auto value, IkarusValue::from_json(*value_json));
|
||||
|
||||
values.values.emplace_back(
|
||||
std::make_pair(
|
||||
std::move(name_json->get<std::string_view>()),
|
||||
std::move(value)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return cppbase::ok(std::move(values));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,98 +1,28 @@
|
|||
#pragma once
|
||||
|
||||
#include <boost/json.hpp>
|
||||
#include <nlohmann/json.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";
|
||||
#include <ikarus/values/data.hpp>
|
||||
#include <ikarus/values/errors.hpp>
|
||||
#include <ikarus/values/schema.hpp>
|
||||
|
||||
struct IkarusValue {
|
||||
public:
|
||||
using Data = std::variant<struct IkarusToggleValue *, struct IkarusNumberValue *, struct IkarusTextValue *>;
|
||||
constexpr static auto SMALL_VEC_VALUE_SIZE = 8;
|
||||
IkarusValueSchema schema;
|
||||
IkarusValueData data;
|
||||
|
||||
public:
|
||||
explicit IkarusValue(Data data, IkarusValueCardinality cardinality);
|
||||
|
||||
IkarusValue(IkarusValue const &) = default;
|
||||
IkarusValue(IkarusValue &&) noexcept = default;
|
||||
|
||||
IkarusValue & operator=(IkarusValue const &) = default;
|
||||
IkarusValue & operator=(IkarusValue &&) noexcept = default;
|
||||
|
||||
virtual ~IkarusValue() = default;
|
||||
|
||||
public:
|
||||
struct FromJsonError {};
|
||||
|
||||
[[nodiscard]] static cppbase::Result<IkarusValue *, FromJsonError> from_json(boost::json::value json);
|
||||
[[nodiscard]] boost::json::value to_json() const;
|
||||
|
||||
public:
|
||||
Data data;
|
||||
IkarusValueCardinality cardinality;
|
||||
static auto from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValue, IkarusValueParseError>;
|
||||
static auto to_json(nlohmann::json & json, IkarusValue const & value)
|
||||
-> void;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct fmt::formatter<IkarusValue::FromJsonError> {
|
||||
template<typename FormatParseContext>
|
||||
constexpr static auto parse(FormatParseContext & ctx) {
|
||||
return ctx.end();
|
||||
}
|
||||
struct IkarusValues {
|
||||
static auto from_json(nlohmann::json const & json)
|
||||
-> cppbase::Result<IkarusValues, IkarusValuesParseError>;
|
||||
static auto to_json(nlohmann::json & json, IkarusValues const & values)
|
||||
-> void;
|
||||
|
||||
constexpr static auto format([[maybe_unused]] IkarusValue::FromJsonError const & error, fmt::format_context & ctx) {
|
||||
return fmt::format_to(ctx.out(), "unable to parse ikarus value JSON");
|
||||
}
|
||||
std::vector<std::pair<std::string, IkarusValue>> values;
|
||||
};
|
||||
|
||||
template<typename... Args>
|
||||
IkarusValue * fetch_value_from_db(IkarusProject * project, IkarusErrorData * error_out, std::string_view query, Args &&... args) {
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const value_str,
|
||||
nullptr,
|
||||
"unable to fetch entity property value from database: {}",
|
||||
IkarusErrorInfo_Database_QueryFailed,
|
||||
project->db->query_one<std::string>(query, std::forward<Args>(args)...)
|
||||
);
|
||||
|
||||
boost::json::error_code ec{};
|
||||
boost::json::value json_value = boost::json::parse(value_str, ec);
|
||||
|
||||
if (ec) {
|
||||
IKARUS_SET_ERROR(fmt::format("invalid json is stored in database: {}", ec.message()), IkarusErrorInfo_Database_InvalidState);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
IKARUS_VTRYRV_OR_FAIL(
|
||||
auto const ret,
|
||||
nullptr,
|
||||
"unable to fetch entity property value: {}",
|
||||
IkarusErrorInfo_LibIkarus_CannotPerformOperation,
|
||||
IkarusValue::from_json(std::move(json_value))
|
||||
);
|
||||
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -1,126 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <ranges>
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include <cppbase/templates.hpp>
|
||||
|
||||
template<typename V>
|
||||
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];
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
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();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
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_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_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_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_data_base_is_undefined(V const * value) {
|
||||
return boost::variant2::holds_alternative<boost::variant2::monostate>(value->data);
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
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, IkarusValueData::SMALL_VEC_VALUE_SIZE>{};
|
||||
}
|
||||
}
|
||||
|
||||
template<typename V, std::invocable<typename V::DataType> F>
|
||||
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; },
|
||||
[&transformer](auto const & data) -> char const * {
|
||||
auto buffer = fmt::memory_buffer{};
|
||||
|
||||
fmt::format_to(
|
||||
std::back_inserter(buffer),
|
||||
"{}",
|
||||
fmt::join(data | std::views::transform(std::forward<F>(transformer)), ", ")
|
||||
);
|
||||
|
||||
return buffer.data();
|
||||
}
|
||||
},
|
||||
value->data
|
||||
);
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
bool ikarus_value_data_base_is_equal(V const * lhs, V const * rhs) {
|
||||
return lhs->data == rhs->data;
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
V * ikarus_value_data_base_copy(V const * value) {
|
||||
return new V{*value};
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
struct IkarusValueData * ikarus_value_data_base_to_value(V * value) {
|
||||
return static_cast<IkarusValueData *>(value);
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
struct IkarusValueData const * ikarus_value_data_base_to_value_data_const(V const * value) {
|
||||
return static_cast<IkarusValueData const *>(value);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue