From 89c9c6fe46ce4a273a15b7f882c3d0935a868145 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 16 Aug 2016 11:55:12 +0200 Subject: cLuaState: Added support for optional params and AStringMap values. --- src/Bindings/LuaState.h | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'src/Bindings/LuaState.h') diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 60ae840b0..303a59327 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -309,6 +309,24 @@ public: typedef UniquePtr cTableRefPtr; + /** Represents a parameter that is optional - calling a GetStackValue() with this object will not fail if the value on the Lua stack is nil. + Note that the GetStackValue() will still fail if the param is present but of a different type. + The class itself is just a marker so that the template magic will select the correct GetStackValue() overload. */ + template + class cOptionalParam + { + public: + explicit cOptionalParam(T & a_Dest): + m_Dest(a_Dest) + { + } + + T & GetDest(void) { return m_Dest; } + + protected: + T & m_Dest; + }; + /** A dummy class that's used only to delimit function args from return values for cLuaState::Call() */ class cRet { @@ -475,6 +493,7 @@ public: // Push a const value onto the stack (keep alpha-sorted): void Push(const AString & a_String); + void Push(const AStringMap & a_Dictionary); void Push(const AStringVector & a_Vector); void Push(const cCraftingGrid * a_Grid); void Push(const cCraftingRecipe * a_Recipe); @@ -508,6 +527,7 @@ public: // Returns whether value was changed // Enum values are checked for their allowed values and fail if the value is not assigned. bool GetStackValue(int a_StackPos, AString & a_Value); + bool GetStackValue(int a_StackPos, AStringMap & a_Value); bool GetStackValue(int a_StackPos, bool & a_Value); bool GetStackValue(int a_StackPos, cCallback & a_Callback); bool GetStackValue(int a_StackPos, cCallbackPtr & a_Callback); @@ -549,6 +569,17 @@ public: return true; } + /** Retrieves an optional value on the stack - doesn't fail if the stack contains nil instead of the value. */ + template + bool GetStackValue(int a_StackPos, cOptionalParam && a_ReturnedVal) + { + if (lua_isnoneornil(m_LuaState, a_StackPos)) + { + return true; + } + return GetStackValue(a_StackPos, a_ReturnedVal.GetDest()); + } + /** Pushes the named value in the table at the top of the stack. a_Name may be a path containing multiple table levels, such as "cChatColor.Blue". If the value is found, it is pushed on top of the stack and the returned cStackValue is valid. @@ -606,14 +637,14 @@ public: } /** Retrieves a list of values from the Lua stack, starting at the specified index. */ - template - inline bool GetStackValues(int a_StartStackPos, T & a_Ret, Args &&... args) + template + inline bool GetStackValues(int a_StartStackPos, Arg1 && a_Arg1, Args &&... args) { - if (!GetStackValue(a_StartStackPos, a_Ret)) + if (!GetStackValue(a_StartStackPos, std::forward(a_Arg1))) { return false; } - return GetStackValues(a_StartStackPos + 1, args...); + return GetStackValues(a_StartStackPos + 1, std::forward(args)...); } /** Returns true if the specified parameters on the stack are of the specified usertable type; also logs warning if not. Used for static functions */ -- cgit v1.2.3 From 9493488e48c49a54e9b1ec892107735de6736c13 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 16 Aug 2016 13:02:08 +0200 Subject: cLuaState: Added direct support for pushing a nil constant. --- src/Bindings/LuaState.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/Bindings/LuaState.h') diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 303a59327..3c07ac5d2 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -327,14 +327,21 @@ public: T & m_Dest; }; + /** A dummy class that's used only to delimit function args from return values for cLuaState::Call() */ class cRet { } ; - static const cRet Return; // Use this constant to delimit function args from return values for cLuaState::Call() + /** A dummy class that's used only to push a constant nil as a function parameter in Call(). */ + class cNil + { + }; + static const cNil Nil; // Use this constant to give a function a nil parameter in Call() + + /** A RAII class for values pushed onto the Lua stack. Will pop the value off the stack in the destructor. */ class cStackValue @@ -489,8 +496,6 @@ public: Push(std::forward(a_Arg2), std::forward(a_Args)...); } - void PushNil(void); - // Push a const value onto the stack (keep alpha-sorted): void Push(const AString & a_String); void Push(const AStringMap & a_Dictionary); @@ -499,6 +504,7 @@ public: void Push(const cCraftingRecipe * a_Recipe); void Push(const char * a_Value); void Push(const cItems & a_Items); + void Push(const cNil & a_Nil); void Push(const cPlayer * a_Player); void Push(const cRef & a_Ref); void Push(const HTTPRequest * a_Request); -- cgit v1.2.3 From 11682d1386299d78bab39f77884797981950edee Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 16 Aug 2016 14:05:03 +0200 Subject: cLuaState: Moved function param counting to PushCallPop() template. The Push() functions can be used not only for function params, but also returns or temporaries, so it doesn't make sense to count the params there. --- src/Bindings/LuaState.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/Bindings/LuaState.h') diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 3c07ac5d2..cb68b9a98 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -634,6 +634,7 @@ public: template bool Call(const FnT & a_Function, Args &&... args) { + m_NumCurrentFunctionArgs = -1; if (!PushFunction(std::forward(a_Function))) { // Pushing the function failed @@ -795,6 +796,7 @@ protected: inline bool PushCallPop(T && a_Param, Args &&... args) { Push(std::forward(a_Param)); + m_NumCurrentFunctionArgs += 1; return PushCallPop(std::forward(args)...); } -- cgit v1.2.3