From c70f3bfcf26b6d6297157c3ff7bb7f60fd99ace6 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sun, 29 Jul 2018 20:24:23 +0300 Subject: [PATCH] Update the ArgPop helper to allow sending extra parameters to the last argument. Fix how StackStrF is handled by Var helper. --- include/sqrat/sqratTypes.h | 195 +++++++++++++++++++++++++++---------- 1 file changed, 145 insertions(+), 50 deletions(-) diff --git a/include/sqrat/sqratTypes.h b/include/sqrat/sqratTypes.h index 9cfabe3b..7a8ed520 100644 --- a/include/sqrat/sqratTypes.h +++ b/include/sqrat/sqratTypes.h @@ -1000,25 +1000,28 @@ public: #endif ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -/// Used to get and push StackStrF types to and from the stack (StackStrF is always a reference) +/// Used to get and push StackStrF instances to and from the stack as references (StackStrF should not be a reference) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// template<> -struct Var { +struct Var { StackStrF value; ///< The actual value of get operations ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Attempts to get the value off the stack at idx as a string + /// Attempts to get the value off the stack at idx as an StackStrF /// /// \param vm Target VM /// \param idx Index trying to be read /// + /// \remarks + /// This function MUST have its Error handled if it occurred. + /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - Var(HSQUIRRELVM vm, SQInteger idx) : value(vm, idx, false) { + Var(HSQUIRRELVM vm, SQInteger idx, bool fmt = false) : value(vm, idx, fmt) { } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// Called by Sqrat::PushVar to put a string on the stack + /// Called by Sqrat::PushVar to put an StackStrF on the stack /// /// \param vm Target VM /// \param value Value to push on to the VM's stack @@ -1034,10 +1037,16 @@ struct Var { }; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -/// Used to get and push StackStrF types to and from the stack (StackStrF is always a reference) +/// Used to get and push StackStrF instances to and from the stack as references (StackStrF should not be a reference) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// template<> -struct Var : Var {Var(HSQUIRRELVM vm, SQInteger idx) : Var(vm, idx) {}}; +struct Var : Var {Var(HSQUIRRELVM vm, SQInteger idx, bool fmt = false) : Var(vm, idx, fmt) {}}; + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/// Used to get and push StackStrF instances to and from the stack as references (StackStrF should not be a reference) +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +template<> +struct Var : Var {Var(HSQUIRRELVM vm, SQInteger idx, bool fmt = false) : Var(vm, idx, fmt) {}}; // Non-referencable type definitions template struct is_referencable {static const bool value = true;}; @@ -1177,10 +1186,12 @@ template<> struct ArgPop<> { ArgPop(HSQUIRRELVM /*vm*/, SQInteger /*idx*/) { } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(); } }; @@ -1191,13 +1202,19 @@ struct ArgPop<> { template struct ArgPop { Var V1; - ArgPop(HSQUIRRELVM vm, SQInteger idx) - : V1(vm, idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) + : V1(vm, idx, a...) { } + // Retrieve the last popped variable + Var & Last() { return V1; } + const Var & Last() const { return V1; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value); } }; @@ -1209,14 +1226,20 @@ struct ArgPop : public ArgPop { using Base::V1; // Implement ours Var V2; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V2(vm, idx+1) + , V2(vm, idx+1, a...) { } + // Retrieve the last popped variable + Var & Last() { return V2; } + const Var & Last() const { return V2; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value); } }; @@ -1229,14 +1252,20 @@ struct ArgPop : public ArgPop { using Base::V2; // Implement ours Var V3; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V3(vm, idx+2) + , V3(vm, idx+2, a...) { } + // Retrieve the last popped variable + Var & Last() { return V3; } + const Var & Last() const { return V3; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value); } }; @@ -1250,14 +1279,20 @@ struct ArgPop : public ArgPop { using Base::V3; // Implement ours Var V4; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V4(vm, idx+3) + , V4(vm, idx+3, a...) { } + // Retrieve the last popped variable + Var & Last() { return V4; } + const Var & Last() const { return V4; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value); } }; @@ -1272,14 +1307,20 @@ struct ArgPop : public ArgPop { using Base::V4; // Implement ours Var V5; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V5(vm, idx+4) + , V5(vm, idx+4, a...) { } + // Retrieve the last popped variable + Var & Last() { return V5; } + const Var & Last() const { return V5; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value); } }; @@ -1295,14 +1336,20 @@ struct ArgPop : public ArgPop { using Base::V5; // Implement ours Var V6; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V6(vm, idx+5) + , V6(vm, idx+5, a...) { } + // Retrieve the last popped variable + Var & Last() { return V6; } + const Var & Last() const { return V6; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value); } }; @@ -1319,14 +1366,20 @@ struct ArgPop : public ArgPop { using Base::V6; // Implement ours Var V7; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V7(vm, idx+6) + , V7(vm, idx+6, a...) { } + // Retrieve the last popped variable + Var & Last() { return V7; } + const Var & Last() const { return V7; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value); } }; @@ -1344,14 +1397,20 @@ struct ArgPop : public ArgPop { using Base::V7; // Implement ours Var V8; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V8(vm, idx+7) + , V8(vm, idx+7, a...) { } + // Retrieve the last popped variable + Var & Last() { return V8; } + const Var & Last() const { return V8; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value); } }; @@ -1370,14 +1429,20 @@ struct ArgPop : public ArgPop V9; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V9(vm, idx+8) + , V9(vm, idx+8, a...) { } + // Retrieve the last popped variable + Var & Last() { return V9; } + const Var & Last() const { return V9; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value); } }; @@ -1397,14 +1462,20 @@ struct ArgPop : public ArgPop V10; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V10(vm, idx+9) + , V10(vm, idx+9, a...) { } + // Retrieve the last popped variable + Var & Last() { return V10; } + const Var & Last() const { return V10; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value); } }; @@ -1425,14 +1496,20 @@ struct ArgPop : public ArgPop V11; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V11(vm, idx+10) + , V11(vm, idx+10, a...) { } + // Retrieve the last popped variable + Var & Last() { return V11; } + const Var & Last() const { return V11; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value,V11.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value,V11.value); } }; @@ -1454,14 +1531,20 @@ struct ArgPop : public ArgPop V12; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V12(vm, idx+11) + , V12(vm, idx+11, a...) { } + // Retrieve the last popped variable + Var & Last() { return V12; } + const Var & Last() const { return V12; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value,V11.value,V12.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value,V11.value,V12.value); } }; @@ -1484,14 +1567,20 @@ struct ArgPop : public ArgPop V13; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V13(vm, idx+12) + , V13(vm, idx+12, a...) { } + // Retrieve the last popped variable + Var & Last() { return V13; } + const Var & Last() const { return V13; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value,V11.value,V12.value,V13.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value,V11.value,V12.value,V13.value); } }; @@ -1515,14 +1604,20 @@ struct ArgPop : public ArgPop V14; - ArgPop(HSQUIRRELVM vm, SQInteger idx) + // Base constructor. Can also pass extra parameters to the last popped argument. + template ArgPop(HSQUIRRELVM vm, SQInteger idx, A&&... a) : ArgPop(vm, idx) - , V14(vm, idx+13) + , V14(vm, idx+13, a...) { } + // Retrieve the last popped variable + Var & Last() { return V14; } + const Var & Last() const { return V14; } + // Forward the arguments to a functor that doesn't return anything template void Exec(F f) { f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value,V11.value,V12.value,V13.value,V14.value); } - template R Eval(F f) { + // Forward the arguments to a functor that returns a value + template R Eval(F f) { return f(V1.value,V2.value,V3.value,V4.value,V5.value,V6.value,V7.value,V8.value,V9.value,V10.value,V11.value,V12.value,V13.value,V14.value); } };