From 630bfa881004e3a403cb419d819d51b714f505b9 Mon Sep 17 00:00:00 2001 From: AWE Henry Date: Wed, 5 Feb 2025 12:43:47 +0800 Subject: [PATCH] New API for auxiliary pointer --- ext/utility/src/assert.cpp | 4 +-- include/asbind20/bind.hpp | 65 ++++++++++++++++++++++++++++++-------- test/test_bind/global.cpp | 9 +++--- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/ext/utility/src/assert.cpp b/ext/utility/src/assert.cpp index 8e6f809..342b98d 100644 --- a/ext/utility/src/assert.cpp +++ b/ext/utility/src/assert.cpp @@ -83,7 +83,7 @@ std::function register_script_assert( use_generic, "void assert(bool pred)", fp<&script_assert_impl::assert_simple>, - impl + auxiliary(impl) ); if(str_factory) @@ -105,7 +105,7 @@ std::function register_script_assert( .function( string_concat("void assert(bool pred, const ", string_t_decl, " &in msg)").c_str(), &script_assert_impl::assert_msg_wrapper, - impl + auxiliary(impl) ); } } diff --git a/include/asbind20/bind.hpp b/include/asbind20/bind.hpp index 32af658..2b3e29d 100644 --- a/include/asbind20/bind.hpp +++ b/include/asbind20/bind.hpp @@ -16,6 +16,42 @@ namespace asbind20 { +template +class auxiliary_wrapper +{ +public: + auxiliary_wrapper() = delete; + auxiliary_wrapper(const auxiliary_wrapper&) = default; + + explicit auxiliary_wrapper(T& aux) noexcept + : m_aux(std::addressof(aux)) {} + + auxiliary_wrapper(T&& aux) = delete; + + void* to_address() const noexcept + { + return (void*)m_aux; + } + +private: + T* m_aux; +}; + +template +auxiliary_wrapper auxiliary(T& aux) noexcept +{ + return auxiliary_wrapper(aux); +} + +template +auxiliary_wrapper auxiliary(const T& aux) noexcept +{ + return auxiliary_wrapper(aux); +} + +template +auxiliary_wrapper auxiliary(T&& aux) = delete; + class [[nodiscard]] namespace_ { public: @@ -750,7 +786,7 @@ class global final : public register_helper_base global& function( const char* decl, AS_NAMESPACE_QUALIFIER asGENFUNC_t gfn, - T& instance + auxiliary_wrapper aux ) { [[maybe_unused]] @@ -759,18 +795,19 @@ class global final : public register_helper_base decl, to_asSFuncPtr(gfn), AS_NAMESPACE_QUALIFIER asCALL_GENERIC, - (void*)std::addressof(instance) + aux.to_address() ); assert(r >= 0); return *this; } - template + template + requires(std::is_member_function_pointer_v) global& function( const char* decl, - Return (Class::*fn)(Args...), - T& instance + Fn&& fn, + auxiliary_wrapper aux ) requires(!ForceGeneric) { [[maybe_unused]] @@ -779,7 +816,7 @@ class global final : public register_helper_base decl, to_asSFuncPtr(fn), AS_NAMESPACE_QUALIFIER asCALL_THISCALL_ASGLOBAL, - (void*)std::addressof(instance) + (void*)std::addressof(aux) ); assert(r >= 0); @@ -793,7 +830,7 @@ class global final : public register_helper_base use_generic_t, const char* decl, fp_wrapper_t, - T& instance + auxiliary_wrapper aux ) { static_assert( @@ -804,7 +841,7 @@ class global final : public register_helper_base function( decl, to_asGENFUNC_t(fp, call_conv), - instance + aux ); return *this; @@ -816,7 +853,7 @@ class global final : public register_helper_base global& function( const char* decl, fp_wrapper_t, - T& instance + auxiliary_wrapper aux ) { static_assert( @@ -825,9 +862,9 @@ class global final : public register_helper_base ); if constexpr(ForceGeneric) - function(use_generic, decl, fp, instance); + function(use_generic, decl, fp, aux); else - function(decl, Function, instance); + function(decl, Function, aux); return *this; } @@ -1167,7 +1204,7 @@ class class_register_helper_base : public register_helper_base } template - void property_impl(const char* decl, T Class::*mp) + void property_impl(const char* decl, T Class::* mp) { property_impl(decl, member_offset(mp)); } @@ -2566,7 +2603,7 @@ class value_class final : public class_register_helper_base } template - value_class& property(const char* decl, T Class::*mp) + value_class& property(const char* decl, T Class::* mp) { this->template property_impl(decl, mp); @@ -3281,7 +3318,7 @@ class reference_class : public class_register_helper_base } template - reference_class& property(const char* decl, T Class::*mp) + reference_class& property(const char* decl, T Class::* mp) { this->template property_impl(decl, mp); diff --git a/test/test_bind/global.cpp b/test/test_bind/global.cpp index 01b8da3..f528294 100644 --- a/test/test_bind/global.cpp +++ b/test/test_bind/global.cpp @@ -27,7 +27,7 @@ static void register_global_funcs( asIScriptEngine* engine, test_bind::class_wrapper& wrapper, std::string& global_val ) { - using asbind20::fp; + using asbind20::fp, asbind20::auxiliary; asbind20::global(engine) .function("void set_int(int&out)", fp<&test_bind::set_int>) @@ -39,7 +39,7 @@ static void register_global_funcs( .function( "void set_val(int val)", &test_bind::class_wrapper::set_val, - wrapper + auxiliary(wrapper) ) .property("string val", global_val); } @@ -48,7 +48,8 @@ static void register_global_funcs( asbind20::use_generic_t, asIScriptEngine* engine, test_bind::class_wrapper& wrapper, std::string& global_val ) { - using asbind20::fp; + using asbind20::fp, asbind20::auxiliary; + asbind20::global(engine) .function("void set_int(int&out)", fp<&test_bind::set_int>) .function( @@ -58,7 +59,7 @@ static void register_global_funcs( asbind20::set_generic_return(gen, 42); } ) - .function("void set_val(int val)", fp<&test_bind::class_wrapper::set_val>, wrapper) + .function("void set_val(int val)", fp<&test_bind::class_wrapper::set_val>, auxiliary(wrapper)) .property("string val", global_val); }