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 +++++++++++++++++++ src/example/proxy.cpp | 49 ++++++++++++++++++++++--------------------- src/example/savemgr.cpp | 7 +++++-- 12 files changed, 151 insertions(+), 88 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 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(); +} + +} + 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 #include -#include +#include using namespace std; using namespace crepe; using namespace crepe::util; -template -class MyProxyHandler : public ProxyHandler { -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 val(make_unique>()); + int real_value = 0; + + ValueBroker 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 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 #include +#include #include using namespace crepe; @@ -15,9 +16,11 @@ int main() { const char * key = "mygame.test"; SaveManager & mgr = SaveManager::get_instance(); - auto & prop = mgr.get(key, 1); - prop = 2; + ValueBroker & prop = mgr.get(key, 0); + Proxy val = mgr.get(key, 0); + prop.set(1); + val = 2; mgr.set(key, 3); assert(true == mgr.has(key)); -- cgit v1.2.3