diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-26 15:58:39 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-26 15:58:39 +0200 |
commit | e0b1ddae296037376948a4c6f200d89e6d1ba81e (patch) | |
tree | f70e5a9b73e0b10e5c9fe263e146e9884474ac0f /src | |
parent | 2d67cf09b80297d13f642afd7db13dba53ca2b9b (diff) |
WIP save manager
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/Proxy.h | 23 | ||||
-rw-r--r-- | src/crepe/Proxy.hpp | 19 | ||||
-rw-r--r-- | src/crepe/ProxyHandler.h | 13 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.cpp | 9 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.h | 39 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.hpp | 4 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/example/proxy.cpp | 47 | ||||
-rw-r--r-- | src/example/savemgr.cpp | 27 |
11 files changed, 189 insertions, 0 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index d938eb8..8e3a81c 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -31,6 +31,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h SDLContext.h RenderSystem.h + Proxy.h + Proxy.hpp ) add_subdirectory(api) diff --git a/src/crepe/Proxy.h b/src/crepe/Proxy.h new file mode 100644 index 0000000..97b8afa --- /dev/null +++ b/src/crepe/Proxy.h @@ -0,0 +1,23 @@ +#pragma once + +#include <memory> + +#include "ProxyHandler.h" + +namespace crepe { + +template <typename T> +class Proxy { +public: + Proxy & operator = (const T &); + operator const T & () const; + +public: + Proxy(std::unique_ptr<ProxyHandler<T>> handler) : val(std::move(handler)) {} +private: + std::unique_ptr<ProxyHandler<T>> val = nullptr; +}; + +} + +#include "Proxy.hpp" diff --git a/src/crepe/Proxy.hpp b/src/crepe/Proxy.hpp new file mode 100644 index 0000000..7f23716 --- /dev/null +++ b/src/crepe/Proxy.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "Proxy.h" + +namespace crepe { + +template <typename T> +Proxy<T> & Proxy<T>::operator = (const T & val) { + this->val->set(val); + return *this; +} + +template <typename T> +Proxy<T>::operator const T & () const { + return this->val->get(); +} + +} + diff --git a/src/crepe/ProxyHandler.h b/src/crepe/ProxyHandler.h new file mode 100644 index 0000000..ff2be70 --- /dev/null +++ b/src/crepe/ProxyHandler.h @@ -0,0 +1,13 @@ +#pragma once + +namespace crepe { + +template <typename T> +class ProxyHandler { +public: + virtual void set(const T &) = 0; + virtual const T & get() = 0; +}; + +} + diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 0bb1263..97bc6b1 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,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Texture.h AssetManager.h AssetManager.hpp + SaveManager.h + SaveManager.hpp ) diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp new file mode 100644 index 0000000..a05e2c7 --- /dev/null +++ b/src/crepe/api/SaveManager.cpp @@ -0,0 +1,9 @@ +#include "SaveManager.h" + +using namespace crepe::api; + +SaveManager & SaveManager::get_instance() { + static SaveManager instance; + return instance; +} + diff --git a/src/crepe/api/SaveManager.h b/src/crepe/api/SaveManager.h new file mode 100644 index 0000000..37463ca --- /dev/null +++ b/src/crepe/api/SaveManager.h @@ -0,0 +1,39 @@ +#pragma once + +#include "../Proxy.h" + +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> + Proxy<T> & get(const char * key, const T & default_value); + + //! Get a reference to a value + template <typename T> + Proxy<T> & get(const char * key); + + //! Set a value directly + template <typename T> + void set(const char * key, const T & value); + + //! Check if the save file has a value for this \c key + bool has(const char * key); + +private: + SaveManager(); + virtual ~SaveManager(); + +public: + // singleton + static SaveManager & get_instance(); + SaveManager(const SaveManager &) = delete; + SaveManager(SaveManager &&) = delete; + SaveManager & operator = (const SaveManager &) = delete; + SaveManager & operator = (SaveManager &&) = delete; +}; + +} + +#include "SaveManager.hpp" diff --git a/src/crepe/api/SaveManager.hpp b/src/crepe/api/SaveManager.hpp new file mode 100644 index 0000000..44f58ed --- /dev/null +++ b/src/crepe/api/SaveManager.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "SaveManager.h" + diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index fea6f60..8158d60 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -24,3 +24,6 @@ add_example(rendering) add_example(asset_manager) add_example(particle) add_example(physics) +add_example(savemgr) +add_example(proxy) + diff --git a/src/example/proxy.cpp b/src/example/proxy.cpp new file mode 100644 index 0000000..7e2efa5 --- /dev/null +++ b/src/example/proxy.cpp @@ -0,0 +1,47 @@ +/** \file + * + * Standalone example for usage of the proxy type + */ + +#include <crepe/api/Config.h> +#include <crepe/util/log.h> +#include <crepe/Proxy.h> + +using namespace std; +using namespace crepe; +using namespace crepe::util; + +template <typename T> +class MyProxyHandler : public ProxyHandler<T> { +public: + virtual void set(const T & val) { + dbg_logf("set %s", to_string(val).c_str()); + this->val = val; + } + + virtual const T & get() { + dbg_logf("get %s", to_string(this->val).c_str()); + return this->val; + } + +private: + T val = 0; +}; + +void test_ro(const int & val) { } +void test_rw(int & val) { } + +int main() { + auto & cfg = api::Config::get_instance(); + cfg.log.level = util::LogLevel::DEBUG; + + Proxy<int> val(make_unique<MyProxyHandler<int>>()); + + val = 54; + + test_ro(val); // this is allowed + // test_rw(val); // this should throw a compile error + + return 0; +} + diff --git a/src/example/savemgr.cpp b/src/example/savemgr.cpp new file mode 100644 index 0000000..810947d --- /dev/null +++ b/src/example/savemgr.cpp @@ -0,0 +1,27 @@ +/** \file + * + * Standalone example for usage of the save manager + */ + +#include <cassert> +#include <crepe/util/log.h> +#include <crepe/api/SaveManager.h> + +using namespace crepe; +using namespace crepe::api; +using namespace crepe::util; + +int main() { + const char * key = "mygame.test"; + SaveManager & mgr = SaveManager::get_instance(); + + auto & prop = mgr.get<unsigned int>(key, 1); + prop = 2; + + mgr.set<unsigned int>(key, 3); + + assert(true == mgr.has(key)); + + return 0; +} + |