From ecc5761fa78ccb57db958467c3fc999aceadd409 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 27 Oct 2024 16:50:20 +0100 Subject: fix valuebroker/proxy system --- src/crepe/CMakeLists.txt | 4 ++-- src/crepe/Proxy.h | 23 ----------------------- src/crepe/Proxy.hpp | 19 ------------------- src/crepe/ProxyHandler.h | 13 ------------- src/crepe/ValueBroker.h | 27 +++++++++++++++++++++++++++ src/crepe/ValueBroker.hpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/crepe/api/SaveManager.h | 10 +++++----- src/crepe/util/CMakeLists.txt | 2 ++ src/crepe/util/Proxy.h | 23 +++++++++++++++++++++++ src/crepe/util/Proxy.hpp | 22 ++++++++++++++++++++++ 10 files changed, 121 insertions(+), 62 deletions(-) delete mode 100644 src/crepe/Proxy.h delete mode 100644 src/crepe/Proxy.hpp delete mode 100644 src/crepe/ProxyHandler.h create mode 100644 src/crepe/ValueBroker.h create mode 100644 src/crepe/ValueBroker.hpp create mode 100644 src/crepe/util/Proxy.h create mode 100644 src/crepe/util/Proxy.hpp (limited to 'src/crepe') 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 - -#include "ProxyHandler.h" - -namespace crepe { - -template -class Proxy { -public: - Proxy & operator = (const T &); - operator const T & () const; - -public: - Proxy(std::unique_ptr> handler) : val(std::move(handler)) {} -private: - std::unique_ptr> val = nullptr; -}; - -} - -#include "Proxy.hpp" diff --git a/src/crepe/Proxy.hpp b/src/crepe/Proxy.hpp deleted file mode 100644 index 7f23716..0000000 --- a/src/crepe/Proxy.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "Proxy.h" - -namespace crepe { - -template -Proxy & Proxy::operator = (const T & val) { - this->val->set(val); - return *this; -} - -template -Proxy::operator const T & () const { - return this->val->get(); -} - -} - 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 -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 + +namespace crepe { + +template +class ValueBroker { +public: + virtual void set(const T &); + virtual const T & get(); + + typedef std::function setter_t; + typedef std::function 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 + +#include "ValueBroker.h" + +namespace crepe { + +template +ValueBroker::ValueBroker(T & value) : + value(value), + setter([] (T & value, const T & target) { + value = std::move(target); + }), + getter([] (T & value) -> const int & { + return value; + }) + { +} + +template +ValueBroker::ValueBroker(T & value, const setter_t & setter, const getter_t & getter) : + value(value), + setter(setter), + getter(getter) + { +} + +template +const T & ValueBroker::get() { + return this->getter(this->value); +} + +template +void ValueBroker::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 - Proxy & get(const char * key, const T & default_value); + ValueBroker & get(const char * key, const T & default_value); //! Get a reference to a value template - Proxy & get(const char * key); + ValueBroker & get(const char * key); //! Set a value directly template @@ -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 +class Proxy { +public: + Proxy & operator = (const T &); + operator const T & () const; + +public: + Proxy(ValueBroker &); + +private: + ValueBroker & broker; +}; + +} + +#include "Proxy.hpp" + diff --git a/src/crepe/util/Proxy.hpp b/src/crepe/util/Proxy.hpp new file mode 100644 index 0000000..5738d9c --- /dev/null +++ b/src/crepe/util/Proxy.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "Proxy.h" + +namespace crepe::util { + +template +Proxy::Proxy(ValueBroker & broker) : broker(broker) { } + +template +Proxy & Proxy::operator = (const T & val) { + this->broker.set(val); + return *this; +} + +template +Proxy::operator const T & () const { + return this->broker.get(); +} + +} + -- cgit v1.2.3