From a2607bffb1c0f8699021b1b4b3e54fa372e3ed0a Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 3 Nov 2024 15:09:05 +0100 Subject: more WIP savemanager --- src/crepe/DB.cpp | 4 +- src/crepe/DB.h | 2 +- src/crepe/ValueBroker.h | 7 +- src/crepe/ValueBroker.hpp | 12 ++-- src/crepe/api/CMakeLists.txt | 1 - src/crepe/api/Config.h | 5 ++ src/crepe/api/SaveManager.cpp | 144 +++++++++++++++++++++++++++++++++++++++++- src/crepe/api/SaveManager.h | 18 ++++-- src/crepe/api/SaveManager.hpp | 4 -- src/crepe/util/Proxy.h | 4 +- src/crepe/util/Proxy.hpp | 2 +- 11 files changed, 173 insertions(+), 30 deletions(-) delete mode 100644 src/crepe/api/SaveManager.hpp (limited to 'src/crepe') diff --git a/src/crepe/DB.cpp b/src/crepe/DB.cpp index b8448a7..b188637 100644 --- a/src/crepe/DB.cpp +++ b/src/crepe/DB.cpp @@ -7,7 +7,7 @@ using namespace std; using namespace crepe; -DB::DB(const char * path) { +DB::DB(const string & path) { dbg_trace(); int ret; @@ -18,7 +18,7 @@ DB::DB(const char * path) { this->db = { db, [] (libdb::DB * db) { db->close(db, 0); } }; // load or create database file - if ((ret = this->db->open(this->db.get(), NULL, path, NULL, libdb::DB_BTREE, DB_CREATE, 0)) != 0) { + if ((ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, libdb::DB_BTREE, DB_CREATE, 0)) != 0) { throw nullptr; } diff --git a/src/crepe/DB.h b/src/crepe/DB.h index 010ef42..c331f95 100644 --- a/src/crepe/DB.h +++ b/src/crepe/DB.h @@ -14,7 +14,7 @@ namespace crepe { class DB { public: - DB(const char * path); + DB(const std::string & path); virtual ~DB() = default; public: diff --git a/src/crepe/ValueBroker.h b/src/crepe/ValueBroker.h index 9d30a2e..c3359a1 100644 --- a/src/crepe/ValueBroker.h +++ b/src/crepe/ValueBroker.h @@ -10,14 +10,13 @@ public: virtual void set(const T &); virtual const T & get(); - typedef std::function setter_t; - typedef std::function getter_t; + typedef std::function setter_t; + typedef std::function getter_t; private: - T & value; setter_t setter; getter_t getter; public: - ValueBroker(T &, const setter_t &, const getter_t &); + ValueBroker(const setter_t &, const getter_t &); ValueBroker(T &); }; diff --git a/src/crepe/ValueBroker.hpp b/src/crepe/ValueBroker.hpp index ef31c17..61f7f6c 100644 --- a/src/crepe/ValueBroker.hpp +++ b/src/crepe/ValueBroker.hpp @@ -8,19 +8,17 @@ namespace crepe { template ValueBroker::ValueBroker(T & value) : - value(value), - setter([] (T & value, const T & target) { + setter([&value] (const T & target) { value = std::move(target); }), - getter([] (T & value) -> const int & { + getter([&value] () -> const int & { return value; }) { } template -ValueBroker::ValueBroker(T & value, const setter_t & setter, const getter_t & getter) : - value(value), +ValueBroker::ValueBroker(const setter_t & setter, const getter_t & getter) : setter(setter), getter(getter) { @@ -28,12 +26,12 @@ ValueBroker::ValueBroker(T & value, const setter_t & setter, const getter_t & template const T & ValueBroker::get() { - return this->getter(this->value); + return this->getter(); } template void ValueBroker::set(const T & value) { - this->setter(this->value, value); + this->setter(value); } } diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 97bc6b1..abc96ab 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -30,5 +30,4 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES AssetManager.h AssetManager.hpp SaveManager.h - SaveManager.hpp ) diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 8a7f268..aef4d54 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -35,6 +35,11 @@ public: */ bool color = true; } log; + + //! Save manager + struct { + std::string location = "save.crepe.db"; + } savemgr; }; } // namespace crepe::api diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp index 83ff0fa..87e634b 100644 --- a/src/crepe/api/SaveManager.cpp +++ b/src/crepe/api/SaveManager.cpp @@ -1,15 +1,80 @@ #include "../DB.h" -#include "util/log.h" +#include "../util/log.h" +#include "Config.h" +#include "ValueBroker.h" #include "SaveManager.h" using namespace std; using namespace crepe; using namespace crepe::api; +template <> +string SaveManager::serialize(const string & value) { + return value; +} +template +string SaveManager::serialize(const T & value) { + return to_string(value); +} +template string SaveManager::serialize(const uint8_t &); +template string SaveManager::serialize(const int8_t &); +template string SaveManager::serialize(const uint16_t &); +template string SaveManager::serialize(const int16_t &); +template string SaveManager::serialize(const uint32_t &); +template string SaveManager::serialize(const int32_t &); +template string SaveManager::serialize(const uint64_t &); +template string SaveManager::serialize(const int64_t &); +template string SaveManager::serialize(const float &); +template string SaveManager::serialize(const double &); + +template <> +uint64_t SaveManager::deserialize(const string & value) { + try { + return stoul(value); + } catch (std::invalid_argument &) { + return 0; + } +} +template <> +int64_t SaveManager::deserialize(const string & value) { + try { + return stol(value); + } catch (std::invalid_argument &) { + return 0; + } +} +template <> +float SaveManager::deserialize(const string & value) { + try { + return stof(value); + } catch (std::invalid_argument &) { + return 0; + } + return stof(value); +} +template <> +double SaveManager::deserialize(const string & value) { + try { + return stod(value); + } catch (std::invalid_argument &) { + return 0; + } +} +template <> +string SaveManager::deserialize(const string & value) { + return value; +} + +template <> uint8_t SaveManager::deserialize(const string & value) { return deserialize(value); } +template <> int8_t SaveManager::deserialize(const string & value) { return deserialize(value); } +template <> uint16_t SaveManager::deserialize(const string & value) { return deserialize(value); } +template <> int16_t SaveManager::deserialize(const string & value) { return deserialize(value); } +template <> uint32_t SaveManager::deserialize(const string & value) { return deserialize(value); } +template <> int32_t SaveManager::deserialize(const string & value) { return deserialize(value); } + SaveManager::SaveManager() { dbg_trace(); - this->db = make_unique("./save.crepe.db"); } SaveManager & SaveManager::get_instance() { @@ -18,3 +83,78 @@ SaveManager & SaveManager::get_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 &); + diff --git a/src/crepe/api/SaveManager.h b/src/crepe/api/SaveManager.h index 110735d..a1f239d 100644 --- a/src/crepe/api/SaveManager.h +++ b/src/crepe/api/SaveManager.h @@ -14,23 +14,30 @@ class SaveManager { public: //! Get a reference to a value and initialize it with a value if it does not yet exist template - ValueBroker & get(const char * key, const T & default_value); + ValueBroker get(const std::string & key, const T & default_value); //! Get a reference to a value template - ValueBroker & get(const char * key); + ValueBroker get(const std::string & key); //! Set a value directly template - void set(const char * key, const T & value); + void set(const std::string & key, const T & value); //! Check if the save file has a value for this \c key - bool has(const char * key); + bool has(const std::string & key); private: SaveManager(); virtual ~SaveManager() = default; +private: + template + std::string serialize(const T &); + + template + T deserialize(const std::string &); + public: // singleton static SaveManager & get_instance(); @@ -40,9 +47,8 @@ public: SaveManager & operator = (SaveManager &&) = delete; private: - std::unique_ptr db = nullptr; + static DB & get_db(); }; } -#include "SaveManager.hpp" diff --git a/src/crepe/api/SaveManager.hpp b/src/crepe/api/SaveManager.hpp deleted file mode 100644 index 44f58ed..0000000 --- a/src/crepe/api/SaveManager.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#include "SaveManager.h" - diff --git a/src/crepe/util/Proxy.h b/src/crepe/util/Proxy.h index f8eb1f2..89cb3c3 100644 --- a/src/crepe/util/Proxy.h +++ b/src/crepe/util/Proxy.h @@ -11,10 +11,10 @@ public: operator const T & () const; public: - Proxy(ValueBroker &); + Proxy(ValueBroker); private: - ValueBroker & broker; + ValueBroker broker; }; } diff --git a/src/crepe/util/Proxy.hpp b/src/crepe/util/Proxy.hpp index 5738d9c..c2cae93 100644 --- a/src/crepe/util/Proxy.hpp +++ b/src/crepe/util/Proxy.hpp @@ -5,7 +5,7 @@ namespace crepe::util { template -Proxy::Proxy(ValueBroker & broker) : broker(broker) { } +Proxy::Proxy(ValueBroker broker) : broker(broker) { } template Proxy & Proxy::operator = (const T & val) { -- cgit v1.2.3