update sqlitecpp & merge property settings into properties

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
Folling 2023-11-27 11:24:55 +01:00 committed by Folling
parent 3dd30d74c5
commit 7a7f7462a4
No known key found for this signature in database
39 changed files with 412 additions and 253 deletions

View file

@ -0,0 +1,18 @@
#include "function_context.hpp"
FunctionContext::FunctionContext(IkarusProject * project):
_project{project} {}
FunctionContext::~FunctionContext() {
if (_project->_function_contexts.size() == 1) {
if (_project->error_message_buffer.empty()) {
_project->error_message_buffer.push_back('\0');
} else {
_project->error_message_buffer[0] = '\0';
}
_project->error_infos = {};
}
_project->_function_contexts.pop();
}

View file

@ -0,0 +1,53 @@
#pragma once
#include <ranges>
#include <string_view>
#include <type_traits>
#include <fmt/format.h>
#include <cppbase/logger.hpp>
#include <ikarus/errors.h>
#include <persistence/project.hpp>
struct FunctionContext {
public:
explicit FunctionContext(struct IkarusProject * project);
FunctionContext(FunctionContext const&) noexcept = default;
FunctionContext(FunctionContext&&) noexcept = default;
auto operator=(FunctionContext const&) noexcept -> FunctionContext& = default;
auto operator=(FunctionContext&&) noexcept -> FunctionContext& = default;
~FunctionContext();
public:
template<typename... Infos>
requires(std::is_same_v<IkarusErrorInfo, Infos> && ...) && (sizeof...(Infos) <= MAXIMUM_ERROR_INFOS)
auto set_error(std::string_view error_message, bool log_error, Infos... infos) -> void {
if (error_message.size() > _project->error_message_buffer.size()) {
_project->error_message_buffer.resize(error_message.size() + 1);
}
for (int i = 0; i < error_message.size(); ++i) {
_project->error_message_buffer[i] = error_message[i];
}
_project->error_message_buffer[error_message.size()] = '\0';
_project->error_infos = {infos...};
if (log_error) {
LOG_ERROR(
"Error({}): {}",
fmt::join(_project->error_infos | std::views::transform(get_error_info_name), ", "),
error_message
);
}
}
private:
struct IkarusProject * _project;
};

View file

@ -0,0 +1,49 @@
#include "project.hpp"
#include "ikarus/persistence/project.h"
#include <persistence/function_context.hpp>
auto IkarusProject::get_name() const -> std::string_view {
return _name;
}
auto IkarusProject::get_path() const -> std::filesystem::path const& {
return _path;
}
auto IkarusProject::get_db() -> sqlitecpp::Connection * {
return _db.get();
}
auto IkarusProject::get_db() const -> sqlitecpp::Connection const * {
return _db.get();
}
auto IkarusProject::get_function_context() -> FunctionContext * {
return &_function_contexts.emplace(this);
}
IkarusBlueprint * IkarusProject::get_blueprint(IkarusId id) {
return get_cached_object(id, this->_blueprints);
}
auto IkarusProject::uncache_blueprint(IkarusBlueprint * blueprint) -> void {
remove_cached_object(blueprint, _blueprints);
}
auto IkarusProject::get_entity(IkarusId id) -> IkarusEntity * {
return get_cached_object(id, this->_entities);
}
auto IkarusProject::uncache_entity(IkarusEntity * entity) -> void {
remove_cached_object(entity, _entities);
}
auto IkarusProject::get_property(IkarusId id) -> IkarusProperty * {
return get_cached_object(id, this->_properties);
}
auto IkarusProject::uncache_property(IkarusProperty * property) -> void {
remove_cached_object(property, _properties);
}

View file

@ -1,6 +1,5 @@
#pragma once
#include <concepts>
#include <filesystem>
#include <ranges>
#include <stack>
@ -9,83 +8,45 @@
#include <sqlitecpp/connection.hpp>
#include <ikarus/errors.h>
#include <ikarus/id.h>
constexpr inline size_t MAXIMUM_ERROR_INFOS = 8;
constexpr inline size_t MAXIMUM_ERROR_MESSAGE_LENGTH = 256;
/// \private
class FunctionContext {
public:
explicit FunctionContext(struct IkarusProject * project);
FunctionContext(FunctionContext const&) noexcept = default;
FunctionContext(FunctionContext&&) noexcept = default;
auto operator=(FunctionContext const&) noexcept -> FunctionContext& = default;
auto operator=(FunctionContext&&) noexcept -> FunctionContext& = default;
~FunctionContext();
template<typename... Infos>
requires(std::is_same_v<IkarusErrorInfo, Infos> && ...) && (sizeof...(Infos) <= MAXIMUM_ERROR_INFOS)
auto set_error(std::string_view error_message, bool log_error, Infos... infos) -> void;
private:
struct IkarusProject * _project;
};
constexpr inline auto MAXIMUM_ERROR_INFOS = 8;
/// \private
struct IkarusProject {
public:
[[nodiscard]] inline auto name() const -> std::string_view {
return _name;
}
[[nodiscard]] auto get_name() const -> std::string_view;
[[nodiscard]] inline auto path() const -> std::filesystem::path const& {
return _path;
}
[[nodiscard]] auto get_path() const -> std::filesystem::path const&;
[[nodiscard]] inline auto db() -> sqlitecpp::Connection * {
return _db.get();
}
[[nodiscard]] auto get_db() -> sqlitecpp::Connection *;
[[nodiscard]] auto get_db() const -> sqlitecpp::Connection const *;
inline auto function_context() -> FunctionContext * {
return &_function_contexts.emplace(this);
}
public:
[[nodiscard]] auto get_function_context() -> struct FunctionContext *;
[[nodiscard]] IkarusBlueprint * get_blueprint(IkarusId id) {
return get_cached_object(id, this->_blueprints);
}
public:
[[nodiscard]] auto get_blueprint(IkarusId id) -> struct IkarusBlueprint *;
auto uncache_blueprint(struct IkarusBlueprint * blueprint) -> void;
auto remove_blueprint(IkarusBlueprint * blueprint) -> void {
remove_cached_object(blueprint, _blueprints);
}
[[nodiscard]] auto get_entity(IkarusId id) -> struct IkarusEntity *;
auto uncache_entity(struct IkarusEntity * entity) -> void;
[[nodiscard]] auto get_entity(IkarusId id) -> IkarusEntity * {
return get_cached_object(id, this->_entities);
}
auto remove_entity(IkarusEntity * entity) -> void {
remove_cached_object(entity, _entities);
}
[[nodiscard]] auto get_property(IkarusId id) -> IkarusProperty * {
return get_cached_object(id, this->_properties);
}
auto remove_property(IkarusProperty * property) -> void {
remove_cached_object(property, _properties);
}
[[nodiscard]] auto get_property(IkarusId id) -> struct IkarusProperty *;
auto uncache_property(struct IkarusProperty * property) -> void;
private:
template<typename T>
[[nodiscard]] T * get_cached_object(IkarusId id, std::unordered_map<IkarusId, std::unique_ptr<T>>& cache) {
if (auto iter = cache.find(id); iter == cache.cend()) {
auto const iter = cache.find(id);
if (iter == cache.cend()) {
auto [ret_iter, _] = cache.emplace(id, std::make_unique<T>(this, id));
return ret_iter->second.get();
} else {
return iter->second.get();
}
return iter->second.get();
}
template<typename T>
@ -94,7 +55,7 @@ private:
}
private:
friend class FunctionContext;
friend struct FunctionContext;
std::string _name;
std::filesystem::path _path;
@ -109,41 +70,3 @@ private:
std::stack<FunctionContext> _function_contexts;
};
FunctionContext::FunctionContext(struct IkarusProject * project):
_project{project} {}
FunctionContext::~FunctionContext() {
if (_project->_function_contexts.size() == 1) {
if (_project->error_message_buffer.empty()) {
_project->error_message_buffer.push_back('\0');
} else {
_project->error_message_buffer[0] = '\0';
}
_project->error_infos = {};
}
_project->_function_contexts.pop();
}
template<typename... Infos>
requires(std::is_same_v<IkarusErrorInfo, Infos> && ...) && (sizeof...(Infos) <= MAXIMUM_ERROR_INFOS)
auto FunctionContext::set_error(std::string_view error_message, bool log_error, Infos... infos) -> void {
if (error_message.size() > _project->error_message_buffer.size()) {
_project->error_message_buffer.resize(error_message.size() + 1);
}
for (int i = 0; i < error_message.size(); ++i) {
_project->error_message_buffer[i] = error_message[i];
}
_project->error_message_buffer[error_message.size()] = '\0';
_project->error_infos = {infos...};
if (log_error) {
LOG_ERROR(
"Error({}): {}", fmt::join(_project->error_infos | std::views::transform(get_error_info_name), ", "), error_message
);
}
}