diff options
author | Michael Lutz <michi@icosahedron.de> | 2021-12-19 15:17:10 +0100 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2021-12-20 19:25:10 +0100 |
commit | 3fede159409bdddf73bfceec2145d8d419389486 (patch) | |
tree | 78301b62a6633691c5175e6d5c91e06cc13fda32 | |
parent | e08a6e2014233abab22ca9a4991e3e21140241dc (diff) | |
download | openttd-3fede159409bdddf73bfceec2145d8d419389486.tar.xz |
Codechange: [Script] Prettify squirrel call helpers by using C++17.
-rw-r--r-- | src/script/squirrel_helper.hpp | 618 |
1 files changed, 47 insertions, 571 deletions
diff --git a/src/script/squirrel_helper.hpp b/src/script/squirrel_helper.hpp index 7308006fa..e6ce02786 100644 --- a/src/script/squirrel_helper.hpp +++ b/src/script/squirrel_helper.hpp @@ -35,38 +35,6 @@ namespace SQConvert { } }; - template <bool Y> struct YesT { - static const bool Yes = Y; - static const bool No = !Y; - }; - - /** - * Helper class to recognize if the given type is void. Usage: 'IsVoidT<T>::Yes' - */ - template <typename T> struct IsVoidT : YesT<false> {}; - template <> struct IsVoidT<void> : YesT<true> {}; - - /** - * Helper class to recognize if the function/method return type is void. - */ - template <typename Tfunc> struct HasVoidReturnT; - /* functions */ - template <typename Tretval> struct HasVoidReturnT<Tretval (*)()> : IsVoidT<Tretval> {}; - template <typename Tretval, typename Targ1> struct HasVoidReturnT<Tretval (*)(Targ1)> : IsVoidT<Tretval> {}; - template <typename Tretval, typename Targ1, typename Targ2> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2)> : IsVoidT<Tretval> {}; - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3)> : IsVoidT<Tretval> {}; - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3, Targ4)> : IsVoidT<Tretval> {}; - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5)> : IsVoidT<Tretval> {}; - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> struct HasVoidReturnT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10)> : IsVoidT<Tretval> {}; - /* methods */ - template <class Tcls, typename Tretval> struct HasVoidReturnT<Tretval (Tcls::*)()> : IsVoidT<Tretval> {}; - template <class Tcls, typename Tretval, typename Targ1> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1)> : IsVoidT<Tretval> {}; - template <class Tcls, typename Tretval, typename Targ1, typename Targ2> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2)> : IsVoidT<Tretval> {}; - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3)> : IsVoidT<Tretval> {}; - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4)> : IsVoidT<Tretval> {}; - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5)> : IsVoidT<Tretval> {}; - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> struct HasVoidReturnT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10)> : IsVoidT<Tretval> {}; - /** * Special class to make it possible for the compiler to pick the correct GetParam(). @@ -161,568 +129,76 @@ namespace SQConvert { * for SQ callback. The partial specializations for the second arg (Tis_void_retval) are not possible * on the function. Therefore the class is used instead. */ - template <typename Tfunc, bool Tis_void_retval = HasVoidReturnT<Tfunc>::Yes> struct HelperT; - - /** - * The real C++ caller for function with return value and 0 params. - */ - template <typename Tretval> - struct HelperT<Tretval (*)(), false> { - static int SQCall(void *instance, Tretval (*func)(), HSQUIRRELVM vm) - { - return Return(vm, (*func)()); - } - }; - - /** - * The real C++ caller for function with no return value and 0 params. - */ - template <typename Tretval> - struct HelperT<Tretval (*)(), true> { - static int SQCall(void *instance, Tretval (*func)(), HSQUIRRELVM vm) - { - (*func)(); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 0 params. - */ - template <class Tcls, typename Tretval> - struct HelperT<Tretval (Tcls::*)(), false> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(), HSQUIRRELVM vm) - { - return Return(vm, (instance->*func)()); - } - }; - - /** - * The real C++ caller for method with no return value and 0 params. - */ - template <class Tcls, typename Tretval> - struct HelperT<Tretval (Tcls::*)(), true> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(), HSQUIRRELVM vm) - { - (instance->*func)(); - return 0; - } - - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(), HSQUIRRELVM vm) - { - return new Tcls(); - } - }; - - /** - * The real C++ caller for function with return value and 1 param. - */ - template <typename Tretval, typename Targ1> - struct HelperT<Tretval (*)(Targ1), false> { - static int SQCall(void *instance, Tretval (*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 1 param. - */ - template <typename Tretval, typename Targ1> - struct HelperT<Tretval (*)(Targ1), true> { - static int SQCall(void *instance, Tretval (*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr) - ); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 1 param. - */ - template <class Tcls, typename Tretval, typename Targ1> - struct HelperT<Tretval (Tcls::*)(Targ1), false> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 1 param. - */ - template <class Tcls, typename Tretval, typename Targ1> - struct HelperT<Tretval (Tcls::*)(Targ1), true> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr) - ); - return 0; - } - - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tcls *inst = new Tcls( - GetParam(ForceType<Targ1>(), vm, 2, &ptr) - ); - - return inst; - } - }; - - /** - * The real C++ caller for function with return value and 2 params. - */ - template <typename Tretval, typename Targ1, typename Targ2> - struct HelperT<Tretval (*)(Targ1, Targ2), false> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr) - ); - return Return(vm, ret); - } - }; + template <typename Tfunc> struct HelperT; /** - * The real C++ caller for function with no return value and 2 params. + * The real C++ caller for functions. */ - template <typename Tretval, typename Targ1, typename Targ2> - struct HelperT<Tretval (*)(Targ1, Targ2), true> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2), HSQUIRRELVM vm) + template <typename Tretval, typename... Targs> + struct HelperT<Tretval (*)(Targs...)> { + static int SQCall(void *instance, Tretval(*func)(Targs...), HSQUIRRELVM vm) { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr) - ); - return 0; + return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{}); } - }; - /** - * The real C++ caller for method with return value and 2 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2), false> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2), HSQUIRRELVM vm) + private: + template <size_t... i> + static int SQCall(void *instance, Tretval(*func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>) { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 2 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2), true> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr) - ); - return 0; - } - - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tcls *inst = new Tcls( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr) - ); - - return inst; - } - }; - - /** - * The real C++ caller for function with return value and 3 params. - */ - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3> - struct HelperT<Tretval (*)(Targ1, Targ2, Targ3), false> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 3 params. - */ - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3> - struct HelperT<Tretval (*)(Targ1, Targ2, Targ3), true> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr) - ); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 3 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3), false> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 3 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3), true> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr) - ); - return 0; - } - - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tcls *inst = new Tcls( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr) - ); - - return inst; - } - }; - - /** - * The real C++ caller for function with return value and 4 params. - */ - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> - struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4), false> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 4 params. - */ - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> - struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4), true> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr) - ); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 4 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4), false> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 4 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4), true> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr) - ); - return 0; - } - - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tcls *inst = new Tcls( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr) - ); - - return inst; - } - }; - - /** - * The real C++ caller for function with return value and 5 params. - */ - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> - struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5), false> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 5 params. - */ - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> - struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5), true> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr) - ); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 5 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5), false> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 5 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5), true> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr) - ); - return 0; - } - - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tcls *inst = new Tcls( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr) - ); - - return inst; - } - }; - - /** - * The real C++ caller for function with return value and 10 params. - */ - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> - struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), false> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr), - GetParam(ForceType<Targ6>(), vm, 7, &ptr), - GetParam(ForceType<Targ7>(), vm, 8, &ptr), - GetParam(ForceType<Targ8>(), vm, 9, &ptr), - GetParam(ForceType<Targ9>(), vm, 10, &ptr), - GetParam(ForceType<Targ10>(), vm, 11, &ptr) - ); - return Return(vm, ret); + [[maybe_unused]] SQAutoFreePointers ptr; + if constexpr (std::is_void_v<Tretval>) { + (*func)( + GetParam(ForceType<Targs>(), vm, 2 + i, &ptr)... + ); + return 0; + } else { + Tretval ret = (*func)( + GetParam(ForceType<Targs>(), vm, 2 + i, &ptr)... + ); + return Return(vm, ret); + } } }; /** - * The real C++ caller for function with no return value and 10 params. + * The real C++ caller for methods. */ - template <typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> - struct HelperT<Tretval (*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), true> { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm) + template <class Tcls, typename Tretval, typename... Targs> + struct HelperT<Tretval(Tcls:: *)(Targs...)> { + static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm) { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr), - GetParam(ForceType<Targ6>(), vm, 7, &ptr), - GetParam(ForceType<Targ7>(), vm, 8, &ptr), - GetParam(ForceType<Targ8>(), vm, 9, &ptr), - GetParam(ForceType<Targ9>(), vm, 10, &ptr), - GetParam(ForceType<Targ10>(), vm, 11, &ptr) - ); - return 0; + return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{}); } - }; - /** - * The real C++ caller for method with return value and 10 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), false> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm) + static Tcls *SQConstruct(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm) { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr), - GetParam(ForceType<Targ6>(), vm, 7, &ptr), - GetParam(ForceType<Targ7>(), vm, 8, &ptr), - GetParam(ForceType<Targ8>(), vm, 9, &ptr), - GetParam(ForceType<Targ9>(), vm, 10, &ptr), - GetParam(ForceType<Targ10>(), vm, 11, &ptr) - ); - return Return(vm, ret); + return SQConstruct(instance, func, vm, std::index_sequence_for<Targs...>{}); } - }; - /** - * The real C++ caller for method with no return value and 10 params. - */ - template <class Tcls, typename Tretval, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5, typename Targ6, typename Targ7, typename Targ8, typename Targ9, typename Targ10> - struct HelperT<Tretval (Tcls::*)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), true> { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm) + private: + template <size_t... i> + static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>) { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr), - GetParam(ForceType<Targ6>(), vm, 7, &ptr), - GetParam(ForceType<Targ7>(), vm, 8, &ptr), - GetParam(ForceType<Targ8>(), vm, 9, &ptr), - GetParam(ForceType<Targ9>(), vm, 10, &ptr), - GetParam(ForceType<Targ10>(), vm, 11, &ptr) - ); - return 0; + [[maybe_unused]] SQAutoFreePointers ptr; + if constexpr (std::is_void_v<Tretval>) { + (instance->*func)( + GetParam(ForceType<Targs>(), vm, 2 + i, &ptr)... + ); + return 0; + } else { + Tretval ret = (instance->*func)( + GetParam(ForceType<Targs>(), vm, 2 + i, &ptr)... + ); + return Return(vm, ret); + } } - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm) + template <size_t... i> + static Tcls *SQConstruct(Tcls *, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>) { - SQAutoFreePointers ptr; + [[maybe_unused]] SQAutoFreePointers ptr; Tcls *inst = new Tcls( - GetParam(ForceType<Targ1>(), vm, 2, &ptr), - GetParam(ForceType<Targ2>(), vm, 3, &ptr), - GetParam(ForceType<Targ3>(), vm, 4, &ptr), - GetParam(ForceType<Targ4>(), vm, 5, &ptr), - GetParam(ForceType<Targ5>(), vm, 6, &ptr), - GetParam(ForceType<Targ6>(), vm, 7, &ptr), - GetParam(ForceType<Targ7>(), vm, 8, &ptr), - GetParam(ForceType<Targ8>(), vm, 9, &ptr), - GetParam(ForceType<Targ9>(), vm, 10, &ptr), - GetParam(ForceType<Targ10>(), vm, 11, &ptr) + GetParam(ForceType<Targs>(), vm, 2 + i, &ptr)... ); return inst; |