diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/api/Config.cpp | 9 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 18 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.cpp | 46 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.h | 74 |
5 files changed, 114 insertions, 34 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index abc96ab..f55bd9e 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -12,6 +12,7 @@ target_sources(crepe PUBLIC AssetManager.cpp Sprite.cpp SaveManager.cpp + Config.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES diff --git a/src/crepe/api/Config.cpp b/src/crepe/api/Config.cpp new file mode 100644 index 0000000..d6206da --- /dev/null +++ b/src/crepe/api/Config.cpp @@ -0,0 +1,9 @@ +#include "Config.h" + +using namespace crepe; + +Config & Config::get_instance() { + static Config instance; + return instance; +} + diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 4e6d1fa..bbcca87 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -7,16 +7,17 @@ namespace crepe { class Config { private: Config() = default; - public: ~Config() = default; public: //! Retrieve handle to global Config instance - static Config & get_instance() { - static Config instance; - return instance; - } + static Config & get_instance(); + // singleton + Config(const Config &) = delete; + Config(Config &&) = delete; + Config & operator = (const Config &) = delete; + Config & operator = (Config &&) = delete; public: //! Logging-related settings @@ -38,8 +39,15 @@ public: //! Save manager struct { + /** + * \brief Save file location + * + * This location is used by the constructor of SaveManager, and should be + * set before save manager functionality is attempted to be used. + */ std::string location = "save.crepe.db"; } savemgr; }; } // namespace crepe + diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp index d35fc7b..23587e4 100644 --- a/src/crepe/api/SaveManager.cpp +++ b/src/crepe/api/SaveManager.cpp @@ -9,26 +9,26 @@ using namespace std; using namespace crepe; template <> -string SaveManager::serialize(const string & value) { +string SaveManager::serialize(const string & value) const noexcept { return value; } template <typename T> -string SaveManager::serialize(const T & value) { +string SaveManager::serialize(const T & value) const noexcept { 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 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) { +uint64_t SaveManager::deserialize(const string & value) const noexcept { try { return stoul(value); } catch (std::invalid_argument &) { @@ -36,7 +36,7 @@ uint64_t SaveManager::deserialize(const string & value) { } } template <> -int64_t SaveManager::deserialize(const string & value) { +int64_t SaveManager::deserialize(const string & value) const noexcept { try { return stol(value); } catch (std::invalid_argument &) { @@ -44,7 +44,7 @@ int64_t SaveManager::deserialize(const string & value) { } } template <> -float SaveManager::deserialize(const string & value) { +float SaveManager::deserialize(const string & value) const noexcept { try { return stof(value); } catch (std::invalid_argument &) { @@ -53,7 +53,7 @@ float SaveManager::deserialize(const string & value) { return stof(value); } template <> -double SaveManager::deserialize(const string & value) { +double SaveManager::deserialize(const string & value) const noexcept { try { return stod(value); } catch (std::invalid_argument &) { @@ -61,16 +61,16 @@ double SaveManager::deserialize(const string & value) { } } template <> -string SaveManager::deserialize(const string & value) { +string SaveManager::deserialize(const string & value) const noexcept { 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); } +template <> uint8_t SaveManager::deserialize(const string & value) const noexcept { return deserialize<uint64_t>(value); } +template <> int8_t SaveManager::deserialize(const string & value) const noexcept { return deserialize<int64_t>(value); } +template <> uint16_t SaveManager::deserialize(const string & value) const noexcept { return deserialize<uint64_t>(value); } +template <> int16_t SaveManager::deserialize(const string & value) const noexcept { return deserialize<int64_t>(value); } +template <> uint32_t SaveManager::deserialize(const string & value) const noexcept { return deserialize<uint64_t>(value); } +template <> int32_t SaveManager::deserialize(const string & value) const noexcept { return deserialize<int64_t>(value); } SaveManager::SaveManager() { dbg_trace(); diff --git a/src/crepe/api/SaveManager.h b/src/crepe/api/SaveManager.h index 035e2b7..3073656 100644 --- a/src/crepe/api/SaveManager.h +++ b/src/crepe/api/SaveManager.h @@ -8,21 +8,58 @@ namespace crepe { class DB; +/** + * \brief Save data manager + * + * This class provides access to a simple key-value store that stores + * - integers (8-64 bit, signed or unsigned) + * - real numbers (float or double) + * - string (std::string) + * + * The underlying database is a key-value store. + */ class SaveManager { public: - //! Get a reference to a value and initialize it with a value if it does not yet exist + /** + * \brief Get a read/write reference to a value and initialize it if it does not yet exist + * + * \param key The value key + * \param default_value Value to initialize \c key with if it does not already exist in the database + * + * \return Read/write reference to the value + */ template <typename T> ValueBroker<T> get(const std::string & key, const T & default_value); - //! Get a reference to a value + /** + * \brief Get a read/write reference to a value + * + * \param key The value key + * + * \return Read/write reference to the value + * + * \note Attempting to read this value before it is initialized (i.e. set) + * will result in an exception + */ template <typename T> ValueBroker<T> get(const std::string & key); - //! Set a value directly + /** + * \brief Set a value directly + * + * \param key The value key + * \param value The value to store + */ template <typename T> void set(const std::string & key, const T & value); - //! Check if the save file has a value for this \c key + /** + * \brief Check if the save file has a value for this \c key + * + * \param key The value key + * + * \returns True if the key exists, or false if it does not + */ bool has(const std::string & key); private: @@ -30,11 +67,26 @@ private: virtual ~SaveManager() = default; private: + /** + * \brief Serialize an arbitrary value to STL string + * + * \tparam T Type of arbitrary value + * + * \returns String representation of value + */ template <typename T> - std::string serialize(const T &); + std::string serialize(const T &) const noexcept; + /** + * \brief Deserialize an STL string back to type \c T + * + * \tparam T Type of value + * \param value Serialized value + * + * \returns Deserialized value + */ template <typename T> - T deserialize(const std::string &); + T deserialize(const std::string & value) const noexcept; public: // singleton @@ -45,6 +97,16 @@ public: SaveManager & operator = (SaveManager &&) = delete; private: + /** + * \brief Create an instance of DB and return its reference + * + * \returns DB instance + * + * This function exists because DB is a facade class, which can't directly be + * used in the API without workarounds + * + * TODO: better solution + */ static DB & get_db(); }; |