diff options
Diffstat (limited to 'src/Bindings/LuaState.h')
-rw-r--r-- | src/Bindings/LuaState.h | 39 |
1 files changed, 35 insertions, 4 deletions
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<cTableRef> 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 <typename T> + 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 <typename T> + bool GetStackValue(int a_StackPos, cOptionalParam<T> && 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 <typename T, typename... Args> - inline bool GetStackValues(int a_StartStackPos, T & a_Ret, Args &&... args) + template <typename Arg1, typename... Args> + inline bool GetStackValues(int a_StartStackPos, Arg1 && a_Arg1, Args &&... args) { - if (!GetStackValue(a_StartStackPos, a_Ret)) + if (!GetStackValue(a_StartStackPos, std::forward<Arg1>(a_Arg1))) { return false; } - return GetStackValues(a_StartStackPos + 1, args...); + return GetStackValues(a_StartStackPos + 1, std::forward<Args>(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 */ |