From 6fd7cec7d4bbf5aeb361b3f1337671bb0f9af61b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 10:13:11 +0100 Subject: manager mediator refactor --- src/crepe/api/SaveManager.cpp | 173 ------------------------------------------ 1 file changed, 173 deletions(-) delete mode 100644 src/crepe/api/SaveManager.cpp (limited to 'src/crepe/api/SaveManager.cpp') diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp deleted file mode 100644 index c5f43ea..0000000 --- a/src/crepe/api/SaveManager.cpp +++ /dev/null @@ -1,173 +0,0 @@ -#include "../facade/DB.h" -#include "../util/Log.h" - -#include "Config.h" -#include "SaveManager.h" -#include "ValueBroker.h" - -using namespace std; -using namespace crepe; - -template <> -string SaveManager::serialize(const string & value) const noexcept { - return value; -} -template -string SaveManager::serialize(const T & value) const noexcept { - return to_string(value); -} -template string SaveManager::serialize(const uint8_t &) const noexcept; -template string SaveManager::serialize(const int8_t &) const noexcept; -template string SaveManager::serialize(const uint16_t &) const noexcept; -template string SaveManager::serialize(const int16_t &) const noexcept; -template string SaveManager::serialize(const uint32_t &) const noexcept; -template string SaveManager::serialize(const int32_t &) const noexcept; -template string SaveManager::serialize(const uint64_t &) const noexcept; -template string SaveManager::serialize(const int64_t &) const noexcept; -template string SaveManager::serialize(const float &) const noexcept; -template string SaveManager::serialize(const double &) const noexcept; - -template <> -uint64_t SaveManager::deserialize(const string & value) const noexcept { - try { - return stoul(value); - } catch (std::invalid_argument &) { - return 0; - } -} -template <> -int64_t SaveManager::deserialize(const string & value) const noexcept { - try { - return stol(value); - } catch (std::invalid_argument &) { - return 0; - } -} -template <> -float SaveManager::deserialize(const string & value) const noexcept { - try { - return stof(value); - } catch (std::invalid_argument &) { - return 0; - } - return stof(value); -} -template <> -double SaveManager::deserialize(const string & value) const noexcept { - try { - return stod(value); - } catch (std::invalid_argument &) { - return 0; - } -} -template <> -string SaveManager::deserialize(const string & value) const noexcept { - return value; -} - -template <> -uint8_t SaveManager::deserialize(const string & value) const noexcept { - return deserialize(value); -} -template <> -int8_t SaveManager::deserialize(const string & value) const noexcept { - return deserialize(value); -} -template <> -uint16_t SaveManager::deserialize(const string & value) const noexcept { - return deserialize(value); -} -template <> -int16_t SaveManager::deserialize(const string & value) const noexcept { - return deserialize(value); -} -template <> -uint32_t SaveManager::deserialize(const string & value) const noexcept { - return deserialize(value); -} -template <> -int32_t SaveManager::deserialize(const string & value) const noexcept { - return deserialize(value); -} - -SaveManager::SaveManager() { dbg_trace(); } - -SaveManager & SaveManager::get_instance() { - dbg_trace(); - static SaveManager instance; - return instance; -} - -DB & SaveManager::get_db() { - Config & cfg = Config::get_instance(); - // TODO: make this path relative to XDG_DATA_HOME on Linux and whatever the - // default equivalent is on Windows using some third party library - static DB db(cfg.savemgr.location); - return db; -} - -bool SaveManager::has(const string & key) { - DB & db = this->get_db(); - return db.has(key); -} - -template <> -void SaveManager::set(const string & key, const string & value) { - DB & db = this->get_db(); - db.set(key, value); -} -template -void SaveManager::set(const string & key, const T & value) { - DB & db = this->get_db(); - db.set(key, std::to_string(value)); -} -template void SaveManager::set(const string &, const uint8_t &); -template void SaveManager::set(const string &, const int8_t &); -template void SaveManager::set(const string &, const uint16_t &); -template void SaveManager::set(const string &, const int16_t &); -template void SaveManager::set(const string &, const uint32_t &); -template void SaveManager::set(const string &, const int32_t &); -template void SaveManager::set(const string &, const uint64_t &); -template void SaveManager::set(const string &, const int64_t &); -template void SaveManager::set(const string &, const float &); -template void SaveManager::set(const string &, const double &); - -template -ValueBroker SaveManager::get(const string & key, const T & default_value) { - if (!this->has(key)) this->set(key, default_value); - return this->get(key); -} -template ValueBroker SaveManager::get(const string &, const uint8_t &); -template ValueBroker SaveManager::get(const string &, const int8_t &); -template ValueBroker SaveManager::get(const string &, const uint16_t &); -template ValueBroker SaveManager::get(const string &, const int16_t &); -template ValueBroker SaveManager::get(const string &, const uint32_t &); -template ValueBroker SaveManager::get(const string &, const int32_t &); -template ValueBroker SaveManager::get(const string &, const uint64_t &); -template ValueBroker SaveManager::get(const string &, const int64_t &); -template ValueBroker SaveManager::get(const string &, const float &); -template ValueBroker SaveManager::get(const string &, const double &); -template ValueBroker SaveManager::get(const string &, const string &); - -template -ValueBroker SaveManager::get(const string & key) { - T value; - return { - [this, key](const T & target) { this->set(key, target); }, - [this, key, value]() mutable -> const T & { - value = this->deserialize(this->get_db().get(key)); - return value; - }, - }; -} -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -template ValueBroker SaveManager::get(const string &); -- cgit v1.2.3