From c224deee985cfc27306cdcc7fe97433f36a8a01c Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Mon, 4 Nov 2024 16:58:35 +0800 Subject: [PATCH] Implement PRO_DEF_FREE_AS_MEM_DISPATCH (#189) --- proxy.h | 15 +++++++++++++++ tests/proxy_dispatch_tests.cpp | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/proxy.h b/proxy.h index 0623c2d..099c8ad 100644 --- a/proxy.h +++ b/proxy.h @@ -1582,6 +1582,21 @@ struct conversion_dispatch { #define PRO_DEF_FREE_DISPATCH(__NAME, ...) \ ___PRO_EXPAND_MACRO(___PRO_DEF_FREE_DISPATCH, __NAME, __VA_ARGS__) +#define ___PRO_DEF_FREE_AS_MEM_DISPATCH_IMPL(__NAME, __FUNC, __FNAME) \ + struct __NAME { \ + template \ + decltype(auto) operator()(__T&& __self, __Args&&... __args) \ + ___PRO_DIRECT_FUNC_IMPL(__FUNC(::std::forward<__T>(__self), \ + ::std::forward<__Args>(__args)...)) \ + ___PRO_DEF_MEM_ACCESSOR_TEMPLATE(___PRO_DEF_MEM_ACCESSOR, __FNAME) \ + } +#define ___PRO_DEF_FREE_AS_MEM_DISPATCH_2(__NAME, __FUNC) \ + ___PRO_DEF_FREE_AS_MEM_DISPATCH_IMPL(__NAME, __FUNC, __FUNC) +#define ___PRO_DEF_FREE_AS_MEM_DISPATCH_3(__NAME, __FUNC, __FNAME) \ + ___PRO_DEF_FREE_AS_MEM_DISPATCH_IMPL(__NAME, __FUNC, __FNAME) +#define PRO_DEF_FREE_AS_MEM_DISPATCH(__NAME, ...) \ + ___PRO_EXPAND_MACRO(___PRO_DEF_FREE_AS_MEM_DISPATCH, __NAME, __VA_ARGS__) + #define PRO_DEF_WEAK_DISPATCH(__NAME, __D, __FUNC) \ struct __NAME : __D { \ using __D::operator(); \ diff --git a/tests/proxy_dispatch_tests.cpp b/tests/proxy_dispatch_tests.cpp index 95ccc68..901d135 100644 --- a/tests/proxy_dispatch_tests.cpp +++ b/tests/proxy_dispatch_tests.cpp @@ -32,6 +32,9 @@ struct PtrToMemTester { private: int value_; }; + +PRO_DEF_FREE_AS_MEM_DISPATCH(FreeMemToString, std::to_string, ToString); + } // namespace TEST(ProxyDispatchTests, TestOpPlus) { @@ -613,3 +616,10 @@ TEST(ProxyDispatchTests, TestImplciitConversion) { int converted = *p; ASSERT_EQ(converted, 12); } + +TEST(ProxyDispatchTests, TestFreeAsMemDispatch) { + struct TestFacade : pro::facade_builder::add_convention::build {}; + int v = 123; + pro::proxy p = &v; + ASSERT_EQ(p->ToString(), "123"); +}