diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-27 16:50:20 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-27 16:50:20 +0100 |
commit | ecc5761fa78ccb57db958467c3fc999aceadd409 (patch) | |
tree | 8e5f24133473de3f122d59262727a9f0a3a806c9 | |
parent | e0b1ddae296037376948a4c6f200d89e6d1ba81e (diff) |
fix valuebroker/proxy system
-rw-r--r-- | src/crepe/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/crepe/Proxy.h | 23 | ||||
-rw-r--r-- | src/crepe/ProxyHandler.h | 13 | ||||
-rw-r--r-- | src/crepe/ValueBroker.h | 27 | ||||
-rw-r--r-- | src/crepe/ValueBroker.hpp | 40 | ||||
-rw-r--r-- | src/crepe/api/SaveManager.h | 10 | ||||
-rw-r--r-- | src/crepe/util/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/util/Proxy.h | 23 | ||||
-rw-r--r-- | src/crepe/util/Proxy.hpp (renamed from src/crepe/Proxy.hpp) | 9 | ||||
-rw-r--r-- | src/example/proxy.cpp | 49 | ||||
-rw-r--r-- | src/example/savemgr.cpp | 7 |
11 files changed, 135 insertions, 72 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 8e3a81c..97f987a 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -31,8 +31,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h SDLContext.h RenderSystem.h - Proxy.h - Proxy.hpp + ValueBroker.h + ValueBroker.hpp ) add_subdirectory(api) diff --git a/src/crepe/Proxy.h b/src/crepe/Proxy.h deleted file mode 100644 index 97b8afa..0000000 --- a/src/crepe/Proxy.h +++ /dev/null @@ -1,23 +0,0 @@ -#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/ProxyHandler.h b/src/crepe/ProxyHandler.h deleted file mode 100644 index ff2be70..0000000 --- a/src/crepe/ProxyHandler.h +++ /dev/null @@ -1,13 +0,0 @@ -#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/ValueBroker.h b/src/crepe/ValueBroker.h new file mode 100644 index 0000000..9d30a2e --- /dev/null +++ b/src/crepe/ValueBroker.h @@ -0,0 +1,27 @@ +#pragma once + +#include <functional> + +namespace crepe { + +template <typename T> +class ValueBroker { +public: + virtual void set(const T &); + virtual const T & get(); + + typedef std::function<void(T & value, const T & target)> setter_t; + typedef std::function<const T & (T & value)> getter_t; +private: + T & value; + setter_t setter; + getter_t getter; +public: + ValueBroker(T &, const setter_t &, const getter_t &); + ValueBroker(T &); +}; + +} + +#include "ValueBroker.hpp" + diff --git a/src/crepe/ValueBroker.hpp b/src/crepe/ValueBroker.hpp new file mode 100644 index 0000000..ef31c17 --- /dev/null +++ b/src/crepe/ValueBroker.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include <memory> + +#include "ValueBroker.h" + +namespace crepe { + +template <typename T> +ValueBroker<T>::ValueBroker(T & value) : + value(value), + setter([] (T & value, const T & target) { + value = std::move(target); + }), + getter([] (T & value) -> const int & { + return value; + }) + { +} + +template <typename T> +ValueBroker<T>::ValueBroker(T & value, const setter_t & setter, const getter_t & getter) : + value(value), + setter(setter), + getter(getter) + { +} + +template <typename T> +const T & ValueBroker<T>::get() { + return this->getter(this->value); +} + +template <typename T> +void ValueBroker<T>::set(const T & value) { + this->setter(this->value, value); +} + +} + diff --git a/src/crepe/api/SaveManager.h b/src/crepe/api/SaveManager.h index 37463ca..78cd4ba 100644 --- a/src/crepe/api/SaveManager.h +++ b/src/crepe/api/SaveManager.h @@ -1,6 +1,6 @@ #pragma once -#include "../Proxy.h" +#include "../ValueBroker.h" namespace crepe::api { @@ -8,11 +8,11 @@ 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); + ValueBroker<T> & get(const char * key, const T & default_value); //! Get a reference to a value template <typename T> - Proxy<T> & get(const char * key); + ValueBroker<T> & get(const char * key); //! Set a value directly template <typename T> @@ -22,8 +22,8 @@ public: bool has(const char * key); private: - SaveManager(); - virtual ~SaveManager(); + SaveManager() = default; + virtual ~SaveManager() = default; public: // singleton diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index bbeaad9..01d8f22 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 color.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..f8eb1f2 --- /dev/null +++ b/src/crepe/util/Proxy.h @@ -0,0 +1,23 @@ +#pragma once + +#include "ValueBroker.h" + +namespace crepe::util { + +template <typename T> +class Proxy { +public: + Proxy & operator = (const T &); + operator const T & () const; + +public: + Proxy(ValueBroker<T> &); + +private: + ValueBroker<T> & broker; +}; + +} + +#include "Proxy.hpp" + diff --git a/src/crepe/Proxy.hpp b/src/crepe/util/Proxy.hpp index 7f23716..5738d9c 100644 --- a/src/crepe/Proxy.hpp +++ b/src/crepe/util/Proxy.hpp @@ -2,17 +2,20 @@ #include "Proxy.h" -namespace crepe { +namespace crepe::util { + +template <typename T> +Proxy<T>::Proxy(ValueBroker<T> & broker) : broker(broker) { } template <typename T> Proxy<T> & Proxy<T>::operator = (const T & val) { - this->val->set(val); + this->broker.set(val); return *this; } template <typename T> Proxy<T>::operator const T & () const { - return this->val->get(); + return this->broker.get(); } } diff --git a/src/example/proxy.cpp b/src/example/proxy.cpp index 7e2efa5..7c2cb8d 100644 --- a/src/example/proxy.cpp +++ b/src/example/proxy.cpp @@ -3,44 +3,45 @@ * Standalone example for usage of the proxy type */ +#include "ValueBroker.h" #include <crepe/api/Config.h> #include <crepe/util/log.h> -#include <crepe/Proxy.h> +#include <crepe/util/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) { } +void test_ro_ref(const int & val) { } +void test_rw_ref(int & val) { } +void test_ro_val(int val) { } int main() { auto & cfg = api::Config::get_instance(); cfg.log.level = util::LogLevel::DEBUG; - Proxy<int> val(make_unique<MyProxyHandler<int>>()); + int real_value = 0; + + ValueBroker<int> broker { + real_value, + [] (int & value, const int & target) { + dbg_logf("set %s to %s", to_string(value).c_str(), to_string(target).c_str()); + value = target; + }, + [] (int & value) -> const int & { + dbg_logf("get %s", to_string(value).c_str()); + return value; + }, + }; + + Proxy<int> proxy { broker }; - val = 54; + broker.set(54); + proxy = 84; - test_ro(val); // this is allowed - // test_rw(val); // this should throw a compile error + test_ro_ref(proxy); // this is allowed + // test_rw_ref(proxy); // this should throw a compile error + test_ro_val(proxy); return 0; } diff --git a/src/example/savemgr.cpp b/src/example/savemgr.cpp index 810947d..5c034c4 100644 --- a/src/example/savemgr.cpp +++ b/src/example/savemgr.cpp @@ -5,6 +5,7 @@ #include <cassert> #include <crepe/util/log.h> +#include <crepe/util/Proxy.h> #include <crepe/api/SaveManager.h> using namespace crepe; @@ -15,9 +16,11 @@ int main() { const char * key = "mygame.test"; SaveManager & mgr = SaveManager::get_instance(); - auto & prop = mgr.get<unsigned int>(key, 1); - prop = 2; + ValueBroker<unsigned int> & prop = mgr.get<unsigned int>(key, 0); + Proxy<unsigned int> val = mgr.get<unsigned int>(key, 0); + prop.set(1); + val = 2; mgr.set<unsigned int>(key, 3); assert(true == mgr.has(key)); |