Skip to content

Commit

Permalink
New API for auxiliary pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Feb 5, 2025
1 parent 55c3c22 commit 630bfa8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
4 changes: 2 additions & 2 deletions ext/utility/src/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ std::function<assert_handler_t> register_script_assert(
use_generic,
"void assert(bool pred)",
fp<&script_assert_impl::assert_simple>,
impl
auxiliary(impl)
);

if(str_factory)
Expand All @@ -105,7 +105,7 @@ std::function<assert_handler_t> 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)
);
}
}
Expand Down
65 changes: 51 additions & 14 deletions include/asbind20/bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,42 @@

namespace asbind20
{
template <typename T>
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 <typename T>
auxiliary_wrapper<T> auxiliary(T& aux) noexcept
{
return auxiliary_wrapper<T>(aux);
}

template <typename T>
auxiliary_wrapper<const T> auxiliary(const T& aux) noexcept
{
return auxiliary_wrapper<const T>(aux);
}

template <typename T>
auxiliary_wrapper<T> auxiliary(T&& aux) = delete;

class [[nodiscard]] namespace_
{
public:
Expand Down Expand Up @@ -750,7 +786,7 @@ class global final : public register_helper_base<ForceGeneric>
global& function(
const char* decl,
AS_NAMESPACE_QUALIFIER asGENFUNC_t gfn,
T& instance
auxiliary_wrapper<T> aux
)
{
[[maybe_unused]]
Expand All @@ -759,18 +795,19 @@ class global final : public register_helper_base<ForceGeneric>
decl,
to_asSFuncPtr(gfn),
AS_NAMESPACE_QUALIFIER asCALL_GENERIC,
(void*)std::addressof(instance)
aux.to_address()
);
assert(r >= 0);

return *this;
}

template <typename T, typename Return, typename Class, typename... Args>
template <typename Fn, typename T>
requires(std::is_member_function_pointer_v<Fn>)
global& function(
const char* decl,
Return (Class::*fn)(Args...),
T& instance
Fn&& fn,
auxiliary_wrapper<T> aux
) requires(!ForceGeneric)
{
[[maybe_unused]]
Expand All @@ -779,7 +816,7 @@ class global final : public register_helper_base<ForceGeneric>
decl,
to_asSFuncPtr(fn),
AS_NAMESPACE_QUALIFIER asCALL_THISCALL_ASGLOBAL,
(void*)std::addressof(instance)
(void*)std::addressof(aux)
);
assert(r >= 0);

Expand All @@ -793,7 +830,7 @@ class global final : public register_helper_base<ForceGeneric>
use_generic_t,
const char* decl,
fp_wrapper_t<Function>,
T& instance
auxiliary_wrapper<T> aux
)
{
static_assert(
Expand All @@ -804,7 +841,7 @@ class global final : public register_helper_base<ForceGeneric>
function(
decl,
to_asGENFUNC_t(fp<Function>, call_conv<AS_NAMESPACE_QUALIFIER asCALL_THISCALL_ASGLOBAL>),
instance
aux
);

return *this;
Expand All @@ -816,7 +853,7 @@ class global final : public register_helper_base<ForceGeneric>
global& function(
const char* decl,
fp_wrapper_t<Function>,
T& instance
auxiliary_wrapper<T> aux
)
{
static_assert(
Expand All @@ -825,9 +862,9 @@ class global final : public register_helper_base<ForceGeneric>
);

if constexpr(ForceGeneric)
function(use_generic, decl, fp<Function>, instance);
function(use_generic, decl, fp<Function>, aux);
else
function(decl, Function, instance);
function(decl, Function, aux);

return *this;
}
Expand Down Expand Up @@ -1167,7 +1204,7 @@ class class_register_helper_base : public register_helper_base<ForceGeneric>
}

template <typename T, typename Class>
void property_impl(const char* decl, T Class::*mp)
void property_impl(const char* decl, T Class::* mp)
{
property_impl(decl, member_offset(mp));
}
Expand Down Expand Up @@ -2566,7 +2603,7 @@ class value_class final : public class_register_helper_base<ForceGeneric>
}

template <typename T>
value_class& property(const char* decl, T Class::*mp)
value_class& property(const char* decl, T Class::* mp)
{
this->template property_impl<T, Class>(decl, mp);

Expand Down Expand Up @@ -3281,7 +3318,7 @@ class reference_class : public class_register_helper_base<ForceGeneric>
}

template <typename T>
reference_class& property(const char* decl, T Class::*mp)
reference_class& property(const char* decl, T Class::* mp)
{
this->template property_impl<T, Class>(decl, mp);

Expand Down
9 changes: 5 additions & 4 deletions test/test_bind/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Expand All @@ -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);
}
Expand All @@ -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<true>(engine)
.function("void set_int(int&out)", fp<&test_bind::set_int>)
.function(
Expand All @@ -58,7 +59,7 @@ static void register_global_funcs(
asbind20::set_generic_return<int>(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);
}

Expand Down

0 comments on commit 630bfa8

Please sign in to comment.