summaryrefslogtreecommitdiffstats
path: root/src/Bindings/LuaState.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/Bindings/LuaState.h')
-rw-r--r--src/Bindings/LuaState.h53
1 files changed, 46 insertions, 7 deletions
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h
index 60ae840b0..cb68b9a98 100644
--- a/src/Bindings/LuaState.h
+++ b/src/Bindings/LuaState.h
@@ -309,14 +309,39 @@ 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
{
} ;
-
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
@@ -471,15 +496,15 @@ public:
Push(std::forward<Arg2>(a_Arg2), std::forward<Args>(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);
void Push(const AStringVector & a_Vector);
void Push(const cCraftingGrid * a_Grid);
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);
@@ -508,6 +533,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 +575,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.
@@ -597,6 +634,7 @@ public:
template <typename FnT, typename... Args>
bool Call(const FnT & a_Function, Args &&... args)
{
+ m_NumCurrentFunctionArgs = -1;
if (!PushFunction(std::forward<const FnT &>(a_Function)))
{
// Pushing the function failed
@@ -606,14 +644,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 */
@@ -758,6 +796,7 @@ protected:
inline bool PushCallPop(T && a_Param, Args &&... args)
{
Push(std::forward<T>(a_Param));
+ m_NumCurrentFunctionArgs += 1;
return PushCallPop(std::forward<Args>(args)...);
}