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 /src/crepe | |
parent | e0b1ddae296037376948a4c6f200d89e6d1ba81e (diff) |
fix valuebroker/proxy system
Diffstat (limited to 'src/crepe')
-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 |
9 files changed, 105 insertions, 46 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(); } } |