diff options
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/crepe/Exception.cpp | 19 | ||||
-rw-r--r-- | src/crepe/Exception.h | 23 | ||||
-rw-r--r-- | src/crepe/ValueBroker.h | 41 | ||||
-rw-r--r-- | src/crepe/ValueBroker.hpp | 25 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/crepe/api/Config.cpp | 9 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 23 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.cpp | 159 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.h | 114 | ||||
-rw-r--r-- | src/crepe/facade/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/facade/DB.cpp | 68 | ||||
-rw-r--r-- | src/crepe/facade/DB.h | 75 | ||||
-rw-r--r-- | src/crepe/util/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/util/Proxy.h | 33 | ||||
-rw-r--r-- | src/crepe/util/Proxy.hpp | 22 |
16 files changed, 617 insertions, 5 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 8830e05..fc95bd3 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(crepe PUBLIC ComponentManager.cpp Component.cpp Collider.cpp + Exception.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -12,6 +13,9 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ComponentManager.hpp Component.h Collider.h + ValueBroker.h + ValueBroker.hpp + Exception.h ) add_subdirectory(api) diff --git a/src/crepe/Exception.cpp b/src/crepe/Exception.cpp new file mode 100644 index 0000000..f27d5a8 --- /dev/null +++ b/src/crepe/Exception.cpp @@ -0,0 +1,19 @@ +#include <cstdarg> + +#include "Exception.h" +#include "util/fmt.h" + +using namespace std; +using namespace crepe; + +const char * Exception::what() { + return error.c_str(); +} + +Exception::Exception(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + this->error = va_stringf(args, fmt); + va_end(args); +} + diff --git a/src/crepe/Exception.h b/src/crepe/Exception.h new file mode 100644 index 0000000..e4a7bb8 --- /dev/null +++ b/src/crepe/Exception.h @@ -0,0 +1,23 @@ +#pragma once + +#include <exception> +#include <string> + +namespace crepe { + +//! Exception class with printf-style constructor +class Exception : public std::exception { +public: + //! printf + Exception(const char * fmt, ...); + //! Get formatted error message + const char * what(); + +protected: + Exception() = default; + //! Formatted error message + std::string error; + +}; + +} diff --git a/src/crepe/ValueBroker.h b/src/crepe/ValueBroker.h new file mode 100644 index 0000000..88988b4 --- /dev/null +++ b/src/crepe/ValueBroker.h @@ -0,0 +1,41 @@ +#pragma once + +#include <functional> + +namespace crepe { + +/** + * \brief Give reference to value through custom set/get functions + * + * This class can be used to abstract direct access to any arbitrary value + * through a custom get and set function passed to its constructor. Consumers + * of this type may want to wrap it in a \c Proxy so it behaves like a regular + * variable. + * + * \tparam T Type of the underlying variable + */ +template <typename T> +class ValueBroker { +public: + //! Set the value + virtual void set(const T &); + //! Retrieve the value + virtual const T & get(); + + typedef std::function<void(const T & target)> setter_t; + typedef std::function<const T & ()> getter_t; +private: + setter_t setter; + getter_t getter; +public: + /** + * \param setter Function that sets the variable + * \param getter Function that retrieves the variable + */ + ValueBroker(const setter_t & setter, const getter_t & getter); +}; + +} + +#include "ValueBroker.hpp" + diff --git a/src/crepe/ValueBroker.hpp b/src/crepe/ValueBroker.hpp new file mode 100644 index 0000000..0d08333 --- /dev/null +++ b/src/crepe/ValueBroker.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "ValueBroker.h" + +namespace crepe { + +template <typename T> +ValueBroker<T>::ValueBroker(const setter_t & setter, const getter_t & getter) : + setter(setter), + getter(getter) + { +} + +template <typename T> +const T & ValueBroker<T>::get() { + return this->getter(); +} + +template <typename T> +void ValueBroker<T>::set(const T & value) { + this->setter(value); +} + +} + diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 321343a..3b20142 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -10,6 +10,8 @@ target_sources(crepe PUBLIC Texture.cpp AssetManager.cpp Sprite.cpp + SaveManager.cpp + Config.cpp Metadata.cpp Scene.cpp SceneManager.cpp @@ -31,6 +33,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Texture.h AssetManager.h AssetManager.hpp + SaveManager.h Scene.h Metadata.h SceneManager.h 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 88220a7..56e3af5 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 @@ -36,6 +37,17 @@ public: bool color = true; } log; + //! 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; + //! physics-related settings struct { /** @@ -48,3 +60,4 @@ public: }; } // namespace crepe + diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp new file mode 100644 index 0000000..23587e4 --- /dev/null +++ b/src/crepe/api/SaveManager.cpp @@ -0,0 +1,159 @@ +#include "../facade/DB.h" +#include "../util/log.h" + +#include "Config.h" +#include "ValueBroker.h" +#include "SaveManager.h" + +using namespace std; +using namespace crepe; + +template <> +string SaveManager::serialize(const string & value) const noexcept { + return value; +} +template <typename T> +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<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(); +} + +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..3073656 --- /dev/null +++ b/src/crepe/api/SaveManager.h @@ -0,0 +1,114 @@ +#pragma once + +#include <memory> + +#include "../ValueBroker.h" + +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: + /** + * \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); + + /** + * \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); + + /** + * \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); + + /** + * \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: + SaveManager(); + 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 &) 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 & value) const noexcept; + +public: + // singleton + static SaveManager & get_instance(); + SaveManager(const SaveManager &) = delete; + SaveManager(SaveManager &&) = delete; + SaveManager & operator = (const SaveManager &) = delete; + 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(); +}; + +} + diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index c79e16f..4cc53bc 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -2,11 +2,13 @@ target_sources(crepe PUBLIC Sound.cpp SoundContext.cpp SDLContext.cpp + DB.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES Sound.h SoundContext.h SDLContext.h + DB.h ) diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp new file mode 100644 index 0000000..c885560 --- /dev/null +++ b/src/crepe/facade/DB.cpp @@ -0,0 +1,68 @@ +#include <cstring> + +#include "util/log.h" +#include "Exception.h" + +#include "DB.h" + +using namespace std; +using namespace crepe; + +DB::DB(const string & path) { + dbg_trace(); + int ret; + + // init database struct + libdb::DB * db; + if ((ret = libdb::db_create(&db, NULL, 0)) != 0) + throw Exception("db_create: %s", libdb::db_strerror(ret)); + 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.c_str(), NULL, libdb::DB_BTREE, DB_CREATE, 0)) != 0) + throw Exception("db->open: %s", libdb::db_strerror(ret)); + + // create cursor + libdb::DBC * cursor; + if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0) + throw Exception("db->cursor: %s", libdb::db_strerror(ret)); + this->cursor = { cursor, [] (libdb::DBC * cursor) { cursor->close(cursor); } }; +} + + +libdb::DBT DB::to_thing(const string & thing) const noexcept { + libdb::DBT thang; + memset(&thang, 0, sizeof(libdb::DBT)); + thang.data = (void *) thing.data(); + thang.size = thing.size(); + return thang; +} + +string DB::get(const string & key) { + libdb::DBT db_key = this->to_thing(key); + libdb::DBT db_val; + memset(&db_val, 0, sizeof(libdb::DBT)); + + int ret = this->cursor->get(this->cursor.get(), &db_key, &db_val, DB_FIRST); + if (ret != 0) + throw Exception("cursor->get: %s", libdb::db_strerror(ret)); + return { static_cast<char *>(db_val.data), db_val.size }; +} + +void DB::set(const string & key, const string & value) { + libdb::DBT db_key = this->to_thing(key); + libdb::DBT db_val = this->to_thing(value); + int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0); + if (ret != 0) + throw Exception("cursor->get: %s", libdb::db_strerror(ret)); +} + +bool DB::has(const std::string & key) noexcept { + try { + this->get(key); + } catch (...) { + return false; + } + return true; +} + diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h new file mode 100644 index 0000000..b62a974 --- /dev/null +++ b/src/crepe/facade/DB.h @@ -0,0 +1,75 @@ +#pragma once + +#include <string> +#include <functional> +#include <memory> + +namespace libdb { +extern "C" { +#include <db.h> +} +} + +namespace crepe { + +/** + * \brief Berkeley DB facade + * + * Berkeley DB is a simple key-value database that stores arbitrary data as + * both key and value. This facade uses STL strings as keys/values. + */ +class DB { +public: + /** + * \param path The path of the database (created if nonexistant) + */ + DB(const std::string & path); + virtual ~DB() = default; + +public: + /** + * \brief Get a value from the database, or throw an exception + * + * \param key The value key + * + * \return The value + * + * \throws Exception if value is not found in DB or other error occurs + */ + std::string get(const std::string & key); + /** + * \brief Set (create or overwrite) a value in the database + * + * \param key The value key + * \param value The value to store + * + * \throws Exception if an error occurs + */ + void set(const std::string & key, const std::string & value); + /** + * \brief Check if a key exists in the database + * + * \param key The value key + * + * \returns True if the key exists, or false if it does not + */ + bool has(const std::string & key) noexcept; + +private: + //! RAII wrapper around \c DB struct + std::unique_ptr<libdb::DB, std::function<void(libdb::DB *)>> db; + //! RAII wrapper around \c DBC struct + std::unique_ptr<libdb::DBC, std::function<void(libdb::DBC *)>> cursor; + +private: + /** + * \brief Convert an STL string to DBT (data base thang) + * + * \param thing Input data + * \return \c DBT with the same data as input \c thing + */ + libdb::DBT to_thing(const std::string & thing) const noexcept; +}; + +} + diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index 3675bee..0fa4343 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -8,5 +8,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES LogColor.h log.h fmt.h + Proxy.h + Proxy.hpp ) diff --git a/src/crepe/util/Proxy.h b/src/crepe/util/Proxy.h new file mode 100644 index 0000000..fbfed0c --- /dev/null +++ b/src/crepe/util/Proxy.h @@ -0,0 +1,33 @@ +#pragma once + +#include "ValueBroker.h" + +namespace crepe { + +/** + * \brief Utility wrapper for \c ValueBroker + * + * This class can be used to to wrap a ValueBroker instance so it behaves like + * a regular variable. + * + * \tparam T Type of the underlying variable + */ +template <typename T> +class Proxy { +public: + //! Set operator + Proxy & operator = (const T &); + //! Get operator + operator const T & (); + +public: + Proxy(ValueBroker<T>); + +private: + ValueBroker<T> broker; +}; + +} + +#include "Proxy.hpp" + diff --git a/src/crepe/util/Proxy.hpp b/src/crepe/util/Proxy.hpp new file mode 100644 index 0000000..4aec9e9 --- /dev/null +++ b/src/crepe/util/Proxy.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "Proxy.h" + +namespace crepe { + +template <typename T> +Proxy<T>::Proxy(ValueBroker<T> broker) : broker(broker) { } + +template <typename T> +Proxy<T> & Proxy<T>::operator = (const T & val) { + this->broker.set(val); + return *this; +} + +template <typename T> +Proxy<T>::operator const T & () { + return this->broker.get(); +} + +} + |