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/example/proxy.cpp | 49 +++++++++++++++++++++++++------------------------ src/example/savemgr.cpp | 7 +++++-- 2 files changed, 30 insertions(+), 26 deletions(-) (limited to 'src/example') 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