diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-07 18:57:04 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-07 18:57:04 +0100 |
commit | 9df087ede0b539ecbd2778236c7d1143362b384d (patch) | |
tree | 01a1c0c46da3a09137fcb9ca6e613fd1148f4df1 | |
parent | e36ea050972fcaaf3d85d672755bad4ebb2dcd80 (diff) |
check code standard
-rw-r--r-- | src/crepe/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/Exception.cpp | 19 | ||||
-rw-r--r-- | src/crepe/Exception.h | 23 | ||||
-rw-r--r-- | src/crepe/ValueBroker.h | 19 | ||||
-rw-r--r-- | src/crepe/ValueBroker.hpp | 13 | ||||
-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 | ||||
-rw-r--r-- | src/crepe/facade/DB.cpp | 21 | ||||
-rw-r--r-- | src/crepe/facade/DB.h | 43 | ||||
-rw-r--r-- | src/crepe/util/Proxy.h | 10 |
13 files changed, 238 insertions, 60 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 3b05742..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 @@ -14,6 +15,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES 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 index c3359a1..88988b4 100644 --- a/src/crepe/ValueBroker.h +++ b/src/crepe/ValueBroker.h @@ -4,10 +4,22 @@ 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; @@ -16,8 +28,11 @@ private: setter_t setter; getter_t getter; public: - ValueBroker(const setter_t &, const getter_t &); - ValueBroker(T &); + /** + * \param setter Function that sets the variable + * \param getter Function that retrieves the variable + */ + ValueBroker(const setter_t & setter, const getter_t & getter); }; } diff --git a/src/crepe/ValueBroker.hpp b/src/crepe/ValueBroker.hpp index 61f7f6c..0d08333 100644 --- a/src/crepe/ValueBroker.hpp +++ b/src/crepe/ValueBroker.hpp @@ -1,23 +1,10 @@ #pragma once -#include <memory> - #include "ValueBroker.h" namespace crepe { template <typename T> -ValueBroker<T>::ValueBroker(T & value) : - setter([&value] (const T & target) { - value = std::move(target); - }), - getter([&value] () -> const int & { - return value; - }) - { -} - -template <typename T> ValueBroker<T>::ValueBroker(const setter_t & setter, const getter_t & getter) : setter(setter), getter(getter) 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(); }; diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp index bd2f089..c885560 100644 --- a/src/crepe/facade/DB.cpp +++ b/src/crepe/facade/DB.cpp @@ -1,6 +1,7 @@ #include <cstring> #include "util/log.h" +#include "Exception.h" #include "DB.h" @@ -14,24 +15,22 @@ DB::DB(const string & path) { // init database struct libdb::DB * db; if ((ret = libdb::db_create(&db, NULL, 0)) != 0) - throw nullptr; // TODO: exception + 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 nullptr; - } + 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 nullptr; - } + 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 { +libdb::DBT DB::to_thing(const string & thing) const noexcept { libdb::DBT thang; memset(&thang, 0, sizeof(libdb::DBT)); thang.data = (void *) thing.data(); @@ -45,7 +44,8 @@ string DB::get(const string & key) { 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 nullptr; // TODO: proper exception + if (ret != 0) + throw Exception("cursor->get: %s", libdb::db_strerror(ret)); return { static_cast<char *>(db_val.data), db_val.size }; } @@ -53,7 +53,8 @@ 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 nullptr; // TODO: proper exception + if (ret != 0) + throw Exception("cursor->get: %s", libdb::db_strerror(ret)); } bool DB::has(const std::string & key) noexcept { diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h index 06442ad..b62a974 100644 --- a/src/crepe/facade/DB.h +++ b/src/crepe/facade/DB.h @@ -12,22 +12,63 @@ extern "C" { 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: - libdb::DBT to_thing(const std::string & thing) const; + /** + * \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/Proxy.h b/src/crepe/util/Proxy.h index 65db04d..fbfed0c 100644 --- a/src/crepe/util/Proxy.h +++ b/src/crepe/util/Proxy.h @@ -4,10 +4,20 @@ 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: |