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 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'src/example/proxy.cpp') 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; } -- cgit v1.2.3