Skip to content

Commit

Permalink
Migrate register helper for asCALL_CDECL to new semantic
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Jan 31, 2025
1 parent 914399e commit db865c4
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 71 deletions.
16 changes: 8 additions & 8 deletions ext/container/src/stdstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,23 +623,23 @@ void register_string_utils_impl(asIScriptEngine* engine)
global<UseGeneric> g(engine);

g
.template function<&as_bool_to_string>("string to_string(bool val)")
.template function<&as_int_to_string<int>>("string to_string(int val, int base=10)")
.template function<&as_int_to_string<asUINT>>("string to_string(uint val, int base=10)")
.template function<&as_int_to_string<std::int64_t>>("string to_string(int64 val, int base=10)")
.template function<&as_int_to_string<std::uint64_t>>("string to_string(uint64 val, int base=10)")
.function("string to_string(bool val)", fp<&as_bool_to_string>)
.function("string to_string(int val, int base=10)", fp<&as_int_to_string<int>>)
.function("string to_string(uint val, int base=10)", fp<&as_int_to_string<asUINT>>)
.function("string to_string(int64 val, int base=10)", fp<&as_int_to_string<std::int64_t>>)
.function("string to_string(uint64 val, int base=10)", fp<&as_int_to_string<std::uint64_t>>)
.enum_type("float_format")
.enum_value("float_format", std::chars_format::scientific, "scientific")
.enum_value("float_format", std::chars_format::hex, "hex")
.enum_value("float_format", std::chars_format::general, "general")
.enum_value("float_format", std::chars_format::fixed, "fixed")
.template function<&as_float_to_string<float>>("string to_string(float val, float_format fmt=float_format::general)")
.template function<&as_float_to_string<double>>("string to_string(double val, float_format fmt=float_format::general)");
.function("string to_string(float val, float_format fmt=float_format::general)", fp<&as_float_to_string<float>>)
.function("string to_string(double val, float_format fmt=float_format::general)", fp<&as_float_to_string<double>>);

if(engine->GetEngineProperty(asEP_USE_CHARACTER_LITERALS))
{
g
.template function<&as_chr>("string chr(uint ch)");
.function("string chr(uint ch)", fp<&as_chr>);
}
}

Expand Down
20 changes: 10 additions & 10 deletions ext/utility/src/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ static void register_script_hash_impl(asIScriptEngine* engine)
global<UseGeneric> g(engine);
g
.typedef_("uint64", "hash_result_t")
.template function<&std_hash_wrapper<std::int8_t>>("uint64 hash(int8)")
.template function<&std_hash_wrapper<std::int16_t>>("uint64 hash(int16)")
.template function<&std_hash_wrapper<std::int32_t>>("uint64 hash(int)")
.template function<&std_hash_wrapper<std::int64_t>>("uint64 hash(int64)")
.template function<&std_hash_wrapper<std::uint8_t>>("uint64 hash(uint8)")
.template function<&std_hash_wrapper<std::uint16_t>>("uint64 hash(uint16)")
.template function<&std_hash_wrapper<std::uint32_t>>("uint64 hash(uint)")
.template function<&std_hash_wrapper<std::uint64_t>>("uint64 hash(uint64)")
.template function<&std_hash_wrapper<float>>("uint64 hash(float)")
.template function<&std_hash_wrapper<double>>("uint64 hash(double)");
.function("uint64 hash(int8)", fp<&std_hash_wrapper<std::int8_t>>)
.function("uint64 hash(int16)", fp<&std_hash_wrapper<std::int16_t>>)
.function("uint64 hash(int)", fp<&std_hash_wrapper<std::int32_t>>)
.function("uint64 hash(int64)", fp<&std_hash_wrapper<std::int64_t>>)
.function("uint64 hash(uint8)", fp<&std_hash_wrapper<std::uint8_t>>)
.function("uint64 hash(uint16)", fp<&std_hash_wrapper<std::uint16_t>>)
.function("uint64 hash(uint)", fp<&std_hash_wrapper<std::uint32_t>>)
.function("uint64 hash(uint64)", fp<&std_hash_wrapper<std::uint64_t>>)
.function("uint64 hash(float)", fp<&std_hash_wrapper<float>>)
.function("uint64 hash(double)", fp<&std_hash_wrapper<double>>);
}

void register_script_hash(asIScriptEngine* engine, bool generic)
Expand Down
90 changes: 45 additions & 45 deletions ext/utility/src/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,54 +87,54 @@ void register_math_function_impl(asIScriptEngine* engine)
global<UseGeneric> g(engine);

g
.template function<&script_math_min<int>>("int min(int a, int b)")
.template function<&script_math_max<int>>("int max(int a, int b)")
.template function<&script_math_clamp<int>>("int clamp(int val, int a, int b)")
.template function<static_cast<int (*)(int)>(&std::abs)>("int abs(int x)");
.function("int min(int a, int b)", fp<&script_math_min<int>>)
.function("int max(int a, int b)", fp<&script_math_max<int>>)
.function("int clamp(int val, int a, int b)", fp<&script_math_clamp<int>>)
.function("int abs(int x)", fp<static_cast<int (*)(int)>(&std::abs)>);

#define ASBIND20_EXT_MATH_UNARY_FUNC(name, float_t) \
template function<static_cast<float_t (*)(float_t)>(&std::name)>(#float_t " " #name "(" #float_t " x)")
function(#float_t " " #name "(" #float_t " x)", fp<static_cast<float_t (*)(float_t)>(&std::name)>)

#define ASBIND20_EXT_MATH_BINARY_FUNC(name, float_t) \
template function<static_cast<float_t (*)(float_t, float_t)>(&(std::name))>(#float_t " " #name "(" #float_t " x, " #float_t " y)")

#define ASBIND20_EXT_MATH_REGISTER_FUNCS(register_class, float_t) \
register_class \
.template function<&script_close_to_simple<float_t>>("bool close_to(" #float_t " a, " #float_t " b)") \
.template function<&close_to<float_t>>("bool close_to(" #float_t " a, " #float_t " b, " #float_t " epsilon)") \
.template function<&script_math_min<float_t>>(#float_t " min(" #float_t " a, " #float_t " b)") \
.template function<&script_math_max<float_t>>(#float_t " max(" #float_t " a, " #float_t " b)") \
.template function<&script_math_clamp<float_t>>(#float_t " clamp(" #float_t " val, " #float_t " a, " #float_t " b)") \
.template function<&script_math_lerp<float_t>>(#float_t " lerp(" #float_t " a, " #float_t " b, " #float_t " t)") \
.ASBIND20_EXT_MATH_UNARY_FUNC(ceil, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(floor, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(round, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(trunc, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(abs, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(sqrt, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(cbrt, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(exp, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(exp2, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(log, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(log2, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(log10, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(cos, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(sin, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(tan, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(acos, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(asin, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(atan, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(cosh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(sinh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(tanh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(acosh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(asinh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(atanh, float_t) \
.template function<&script_isfinite>("bool isfinite(" #float_t " x)") \
.template function<&script_isnan>("bool isnan(" #float_t " x)") \
.template function<&script_isinf>("bool isinf(" #float_t " x)") \
.ASBIND20_EXT_MATH_BINARY_FUNC(pow, float_t) \
.ASBIND20_EXT_MATH_BINARY_FUNC(atan2, float_t) \
function(#float_t " " #name "(" #float_t " x, " #float_t " y)", fp<static_cast<float_t (*)(float_t, float_t)>(&(std::name))>)

#define ASBIND20_EXT_MATH_REGISTER_FUNCS(register_class, float_t) \
register_class \
.function("bool close_to(" #float_t " a, " #float_t " b)", fp<&script_close_to_simple<float_t>>) \
.function("bool close_to(" #float_t " a, " #float_t " b, " #float_t " epsilon)", fp<&close_to<float_t>>) \
.function(#float_t " min(" #float_t " a, " #float_t " b)", fp<&script_math_min<float_t>>) \
.function(#float_t " max(" #float_t " a, " #float_t " b)", fp<&script_math_max<float_t>>) \
.function(#float_t " clamp(" #float_t " val, " #float_t " a, " #float_t " b)", fp<&script_math_clamp<float_t>>) \
.function(#float_t " lerp(" #float_t " a, " #float_t " b, " #float_t " t)", fp<&script_math_lerp<float_t>>) \
.ASBIND20_EXT_MATH_UNARY_FUNC(ceil, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(floor, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(round, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(trunc, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(abs, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(sqrt, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(cbrt, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(exp, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(exp2, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(log, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(log2, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(log10, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(cos, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(sin, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(tan, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(acos, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(asin, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(atan, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(cosh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(sinh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(tanh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(acosh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(asinh, float_t) \
.ASBIND20_EXT_MATH_UNARY_FUNC(atanh, float_t) \
.function("bool isfinite(" #float_t " x)", fp<&script_isfinite>) \
.function("bool isnan(" #float_t " x)", fp<&script_isnan>) \
.function("bool isinf(" #float_t " x)", fp<&script_isinf>) \
.ASBIND20_EXT_MATH_BINARY_FUNC(pow, float_t) \
.ASBIND20_EXT_MATH_BINARY_FUNC(atan2, float_t) \
.ASBIND20_EXT_MATH_BINARY_FUNC(hypot, float_t)

ASBIND20_EXT_MATH_REGISTER_FUNCS(g, float);
Expand Down Expand Up @@ -215,7 +215,7 @@ void register_math_complex_impl(asIScriptEngine* engine)

global<UseGeneric> g(engine);
g
.template function<&complex_abs<float>>("float abs(const complex&in)");
.function("float abs(const complex&in)", fp<&complex_abs<float>>);
}

void register_math_complex(asIScriptEngine* engine, bool generic)
Expand Down
50 changes: 50 additions & 0 deletions include/asbind20/bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ class global final : public register_helper_base<ForceGeneric>
template <
native_function auto Function,
asECallConvTypes CallConv = asCALL_CDECL>
[[deprecated("Use the version with fp<>")]]
global& function(use_generic_t, const char* decl)
{
function(
Expand All @@ -684,10 +685,30 @@ class global final : public register_helper_base<ForceGeneric>
return *this;
}

template <
auto Function,
asECallConvTypes CallConv = asCALL_CDECL>
requires(CallConv == asCALL_CDECL || CallConv == asCALL_STDCALL)
global& function(
use_generic_t,
const char* decl,
fp_wrapper_t<Function>,
call_conv_t<CallConv> = {}
)
{
this->function(
decl,
to_asGENFUNC_t(fp<Function>, call_conv<CallConv>)
);

return *this;
}

template <
native_function auto Function,
asECallConvTypes CallConv = asCALL_CDECL>
requires(!std::is_member_function_pointer_v<std::decay_t<decltype(Function)>>)
[[deprecated("Use the version with fp<>")]]
global& function(const char* decl)
{
if constexpr(ForceGeneric)
Expand All @@ -709,6 +730,35 @@ class global final : public register_helper_base<ForceGeneric>
return *this;
}

template <
auto Function,
asECallConvTypes CallConv = asCALL_CDECL>
requires(CallConv == asCALL_CDECL || CallConv == asCALL_STDCALL)
global& function(
const char* decl,
fp_wrapper_t<Function>,
call_conv_t<CallConv> = {}
)
{
if constexpr(ForceGeneric)
{
function(use_generic, decl, fp<Function>, call_conv<CallConv>);
}
else
{
[[maybe_unused]]
int r = 0;
r = m_engine->RegisterGlobalFunction(
decl,
to_asSFuncPtr(Function),
CallConv
);
assert(r >= 0);
}

return *this;
}

template <typename T>
global& function(
const char* decl,
Expand Down
4 changes: 2 additions & 2 deletions test/shared_test_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void asbind_test_suite::register_all()
);

global(m_engine)
.function<&test_print>(use_generic, "void print(const string &in msg)");
.function(use_generic, "void print(const string &in msg)", fp<&test_print>);
}

void asbind_test_suite_generic::register_all()
Expand All @@ -161,6 +161,6 @@ void asbind_test_suite_generic::register_all()
);

global(engine)
.function<&test_print>(use_generic, "void print(const string &in msg)");
.function(use_generic, "void print(const string &in msg)", fp<&test_print>);
}
} // namespace asbind_test
Loading

0 comments on commit db865c4

Please sign in to comment.