# HG changeset patch # User Michael Lutz # Date 2021-12-19 14:17:10 # Node ID 8b4c8558022f939c4a921e5a6f55ddde120b0a97 # Parent b35f21dabdeebd8d59d5c9de2e81f96f8ba70b41 Codechange: [Script] Prettify squirrel call helpers by using C++17. diff --git a/src/script/squirrel_helper.hpp b/src/script/squirrel_helper.hpp --- a/src/script/squirrel_helper.hpp +++ b/src/script/squirrel_helper.hpp @@ -35,38 +35,6 @@ namespace SQConvert { } }; - template struct YesT { - static const bool Yes = Y; - static const bool No = !Y; - }; - - /** - * Helper class to recognize if the given type is void. Usage: 'IsVoidT::Yes' - */ - template struct IsVoidT : YesT {}; - template <> struct IsVoidT : YesT {}; - - /** - * Helper class to recognize if the function/method return type is void. - */ - template struct HasVoidReturnT; - /* functions */ - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - /* methods */ - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - template struct HasVoidReturnT : IsVoidT {}; - /** * 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 ::Yes> struct HelperT; - - /** - * The real C++ caller for function with return value and 0 params. - */ - template - struct HelperT { - 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 - struct HelperT { - 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 - struct HelperT { - 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 - struct HelperT { - 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 - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType(), vm, 2, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 1 param. - */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType(), vm, 2, &ptr) - ); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 1 param. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 1 param. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr) - ); - return 0; - } - - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tcls *inst = new Tcls( - GetParam(ForceType(), vm, 2, &ptr) - ); - - return inst; - } - }; + template struct HelperT; /** - * The real C++ caller for function with return value and 2 params. + * The real C++ caller for functions. */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 2 params. - */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2), HSQUIRRELVM vm) + template + struct HelperT { + static int SQCall(void *instance, Tretval(*func)(Targs...), HSQUIRRELVM vm) { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr) - ); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 2 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 2 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr) - ); - return 0; + return SQCall(instance, func, vm, std::index_sequence_for{}); } - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tcls *inst = new Tcls( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr) - ); - - return inst; - } - }; - - /** - * The real C++ caller for function with return value and 3 params. - */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 3 params. - */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm) + private: + template + static int SQCall(void *instance, Tretval(*func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence) { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr) - ); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 3 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 3 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), 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(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr) - ); - - return inst; + [[maybe_unused]] SQAutoFreePointers ptr; + if constexpr (std::is_void_v) { + (*func)( + GetParam(ForceType(), vm, 2 + i, &ptr)... + ); + return 0; + } else { + Tretval ret = (*func)( + GetParam(ForceType(), vm, 2 + i, &ptr)... + ); + return Return(vm, ret); + } } }; /** - * The real C++ caller for function with return value and 4 params. + * The real C++ caller for methods. */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) + template + struct HelperT { + static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm) { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 4 params. - */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr) - ); - return 0; + return SQCall(instance, func, vm, std::index_sequence_for{}); } - }; - /** - * The real C++ caller for method with return value and 4 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) + static Tcls *SQConstruct(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm) { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 4 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr) - ); - return 0; + return SQConstruct(instance, func, vm, std::index_sequence_for{}); } - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tcls *inst = new Tcls( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr) - ); - - return inst; - } - }; - - /** - * The real C++ caller for function with return value and 5 params. - */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 5 params. - */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) + private: + template + static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence) { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr) - ); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 5 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 5 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr) - ); - return 0; + [[maybe_unused]] SQAutoFreePointers ptr; + if constexpr (std::is_void_v) { + (instance->*func)( + GetParam(ForceType(), vm, 2 + i, &ptr)... + ); + return 0; + } else { + Tretval ret = (instance->*func)( + GetParam(ForceType(), vm, 2 + i, &ptr)... + ); + return Return(vm, ret); + } } - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tcls *inst = new Tcls( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr) - ); - - return inst; - } - }; - - /** - * The real C++ caller for function with return value and 10 params. - */ - template - struct HelperT { - 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(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr), - GetParam(ForceType(), vm, 7, &ptr), - GetParam(ForceType(), vm, 8, &ptr), - GetParam(ForceType(), vm, 9, &ptr), - GetParam(ForceType(), vm, 10, &ptr), - GetParam(ForceType(), vm, 11, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for function with no return value and 10 params. - */ - template - struct HelperT { - static int SQCall(void *instance, Tretval (*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm) + template + static Tcls *SQConstruct(Tcls *, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence) { - SQAutoFreePointers ptr; - (*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr), - GetParam(ForceType(), vm, 7, &ptr), - GetParam(ForceType(), vm, 8, &ptr), - GetParam(ForceType(), vm, 9, &ptr), - GetParam(ForceType(), vm, 10, &ptr), - GetParam(ForceType(), vm, 11, &ptr) - ); - return 0; - } - }; - - /** - * The real C++ caller for method with return value and 10 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - Tretval ret = (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr), - GetParam(ForceType(), vm, 7, &ptr), - GetParam(ForceType(), vm, 8, &ptr), - GetParam(ForceType(), vm, 9, &ptr), - GetParam(ForceType(), vm, 10, &ptr), - GetParam(ForceType(), vm, 11, &ptr) - ); - return Return(vm, ret); - } - }; - - /** - * The real C++ caller for method with no return value and 10 params. - */ - template - struct HelperT { - static int SQCall(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; - (instance->*func)( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr), - GetParam(ForceType(), vm, 7, &ptr), - GetParam(ForceType(), vm, 8, &ptr), - GetParam(ForceType(), vm, 9, &ptr), - GetParam(ForceType(), vm, 10, &ptr), - GetParam(ForceType(), vm, 11, &ptr) - ); - return 0; - } - - static Tcls *SQConstruct(Tcls *instance, Tretval (Tcls::*func)(Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10), HSQUIRRELVM vm) - { - SQAutoFreePointers ptr; + [[maybe_unused]] SQAutoFreePointers ptr; Tcls *inst = new Tcls( - GetParam(ForceType(), vm, 2, &ptr), - GetParam(ForceType(), vm, 3, &ptr), - GetParam(ForceType(), vm, 4, &ptr), - GetParam(ForceType(), vm, 5, &ptr), - GetParam(ForceType(), vm, 6, &ptr), - GetParam(ForceType(), vm, 7, &ptr), - GetParam(ForceType(), vm, 8, &ptr), - GetParam(ForceType(), vm, 9, &ptr), - GetParam(ForceType(), vm, 10, &ptr), - GetParam(ForceType(), vm, 11, &ptr) + GetParam(ForceType(), vm, 2 + i, &ptr)... ); return inst;