diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 5 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.cpp | 160 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.h | 54 |
4 files changed, 221 insertions, 0 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 0bb1263..abc96ab 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -11,6 +11,7 @@ target_sources(crepe PUBLIC Texture.cpp AssetManager.cpp Sprite.cpp + SaveManager.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -28,4 +29,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Texture.h AssetManager.h AssetManager.hpp + SaveManager.h ) 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 new file mode 100644 index 0000000..87e634b --- /dev/null +++ b/src/crepe/api/SaveManager.cpp @@ -0,0 +1,160 @@ +#include "../DB.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 <typename T> +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<uint64_t>(value); } +template <> int8_t SaveManager::deserialize(const string & value) { return deserialize<int64_t>(value); } +template <> uint16_t SaveManager::deserialize(const string & value) { return deserialize<uint64_t>(value); } +template <> int16_t SaveManager::deserialize(const string & value) { return deserialize<int64_t>(value); } +template <> uint32_t SaveManager::deserialize(const string & value) { return deserialize<uint64_t>(value); } +template <> int32_t SaveManager::deserialize(const string & value) { return deserialize<int64_t>(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 <typename T> +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 <typename T> +ValueBroker<T> SaveManager::get(const string & key, const T & default_value) { + if (!this->has(key)) + this->set<T>(key, default_value); + return this->get<T>(key); +} +template ValueBroker<uint8_t> SaveManager::get(const string &, const uint8_t &); +template ValueBroker<int8_t> SaveManager::get(const string &, const int8_t &); +template ValueBroker<uint16_t> SaveManager::get(const string &, const uint16_t &); +template ValueBroker<int16_t> SaveManager::get(const string &, const int16_t &); +template ValueBroker<uint32_t> SaveManager::get(const string &, const uint32_t &); +template ValueBroker<int32_t> SaveManager::get(const string &, const int32_t &); +template ValueBroker<uint64_t> SaveManager::get(const string &, const uint64_t &); +template ValueBroker<int64_t> SaveManager::get(const string &, const int64_t &); +template ValueBroker<float> SaveManager::get(const string &, const float &); +template ValueBroker<double> SaveManager::get(const string &, const double &); +template ValueBroker<string> SaveManager::get(const string &, const string &); + +template <typename T> +ValueBroker<T> SaveManager::get(const string & key) { + T value; + return { + [this, key] (const T & target) { this->set<T>(key, target); }, + [this, key, value] () mutable -> const T & { + value = this->deserialize<T>(this->get_db().get(key)); + return value; + }, + }; +} +template ValueBroker<uint8_t> SaveManager::get(const string &); +template ValueBroker<int8_t> SaveManager::get(const string &); +template ValueBroker<uint16_t> SaveManager::get(const string &); +template ValueBroker<int16_t> SaveManager::get(const string &); +template ValueBroker<uint32_t> SaveManager::get(const string &); +template ValueBroker<int32_t> SaveManager::get(const string &); +template ValueBroker<uint64_t> SaveManager::get(const string &); +template ValueBroker<int64_t> SaveManager::get(const string &); +template ValueBroker<float> SaveManager::get(const string &); +template ValueBroker<double> SaveManager::get(const string &); +template ValueBroker<string> SaveManager::get(const string &); + diff --git a/src/crepe/api/SaveManager.h b/src/crepe/api/SaveManager.h new file mode 100644 index 0000000..a1f239d --- /dev/null +++ b/src/crepe/api/SaveManager.h @@ -0,0 +1,54 @@ +#pragma once + +#include <memory> + +#include "../ValueBroker.h" + +namespace crepe { +class DB; +} + +namespace crepe::api { + +class SaveManager { +public: + //! Get a reference to a value and initialize it with a value if it does not yet exist + template <typename T> + ValueBroker<T> get(const std::string & key, const T & default_value); + + //! Get a reference to a value + template <typename T> + ValueBroker<T> get(const std::string & key); + + //! Set a value directly + template <typename T> + void set(const std::string & key, const T & value); + + //! Check if the save file has a value for this \c key + bool has(const std::string & key); + +private: + SaveManager(); + virtual ~SaveManager() = default; + +private: + template <typename T> + std::string serialize(const T &); + + template <typename T> + T deserialize(const std::string &); + +public: + // singleton + static SaveManager & get_instance(); + SaveManager(const SaveManager &) = delete; + SaveManager(SaveManager &&) = delete; + SaveManager & operator = (const SaveManager &) = delete; + SaveManager & operator = (SaveManager &&) = delete; + +private: + static DB & get_db(); +}; + +} + |