From d4aff474c29d3d9b1a524cfcd5fca925dab79c5b Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 21 Aug 2016 11:03:10 +0200 Subject: cLuaState: Added template to push multiple values in a single call. (#3331) --- src/Bindings/LuaState.h | 15 +++++++++++--- src/Bindings/ManualBindings.cpp | 31 ++++++----------------------- src/Bindings/ManualBindings_RankManager.cpp | 8 ++------ src/Bindings/ManualBindings_World.cpp | 13 +++--------- 4 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 4def2c663..60ae840b0 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -12,11 +12,12 @@ If owning a state, trying to attach a state will automatically close the previou Calling a Lua function is done internally by pushing the function using PushFunction(), then pushing the arguments and finally executing CallFunction(). cLuaState automatically keeps track of the number of arguments and the name of the function (for logging purposes). After the call the return values are read from -the stack using GetStackValue(). All of this is wrapped in a templated function overloads cLuaState::Call(), -which is generated automatically by gen_LuaState_Call.lua script file into the LuaState_Call.inc file. +the stack using GetStackValue(). All of this is wrapped in a templated function overloads cLuaState::Call(). Reference management is provided by the cLuaState::cRef class. This is used when you need to hold a reference to -any Lua object across several function calls. The class is RAII-like, with automatic resource management. +any Lua object across several function calls. The class is RAII-like, with automatic resource management. Note +that the cRef object is not inherently thread-safe and is not notified when its cLuaState is closed. For those +purposes, cTrackedRef can be used. Callbacks management is provided by the cLuaState::cCallback class. Use a GetStackValue() with cCallbackPtr parameter to store the callback, and then at any time you can use the cCallback's Call() templated function @@ -462,6 +463,14 @@ public: /** Returns true if a_FunctionName is a valid Lua function that can be called */ bool HasFunction(const char * a_FunctionName); + /** Pushes multiple arguments onto the Lua stack. */ + template + void Push(Arg1 && a_Arg1, Arg2 && a_Arg2, Args &&... a_Args) + { + Push(std::forward(a_Arg1)); + Push(std::forward(a_Arg2), std::forward(a_Args)...); + } + void PushNil(void); // Push a const value onto the stack (keep alpha-sorted): diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 20792393d..8927b0b19 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -2062,14 +2062,7 @@ static int tolua_cUrlParser_Parse(lua_State * a_LuaState) L.Push(res.second); return 2; } - L.Push(scheme); - L.Push(username); - L.Push(password); - L.Push(host); - L.Push(port); - L.Push(path); - L.Push(query); - L.Push(fragment); + L.Push(scheme, username, password, host, port, path, query, fragment); return 8; } @@ -2110,10 +2103,7 @@ static int tolua_cUrlParser_ParseAuthorityPart(lua_State * a_LuaState) L.Push(res.second); return 2; } - L.Push(username); - L.Push(password); - L.Push(host); - L.Push(port); + L.Push(username, password, host, port); return 4; } @@ -2976,9 +2966,7 @@ static int tolua_cRoot_GetFurnaceRecipe(lua_State * tolua_S) } // Push the output, number of ticks and input as the three return values: - L.Push(Recipe->Out); - L.Push(Recipe->CookTime); - L.Push(Recipe->In); + L.Push(Recipe->Out, Recipe->CookTime, Recipe->In); return 3; } @@ -3141,12 +3129,7 @@ static int tolua_cBlockArea_GetNonAirCropRelCoords(lua_State * tolua_S) self->GetNonAirCropRelCoords(MinRelX, MinRelY, MinRelZ, MaxRelX, MaxRelY, MaxRelZ, IgnoreBlockType); // Push the six crop coords: - L.Push(MinRelX); - L.Push(MinRelY); - L.Push(MinRelZ); - L.Push(MaxRelX); - L.Push(MaxRelY); - L.Push(MaxRelZ); + L.Push(MinRelX, MinRelY, MinRelZ, MaxRelX, MaxRelY, MaxRelZ); return 6; } @@ -3405,8 +3388,7 @@ static int tolua_cBoundingBox_CalcLineIntersection(lua_State * a_LuaState) L.Push(res); if (res) { - L.Push(lineCoeff); - L.Push(blockFace); + L.Push(lineCoeff, blockFace); return 3; } return 1; @@ -3465,8 +3447,7 @@ static int tolua_cChunkDesc_GetBlockTypeMeta(lua_State * a_LuaState) BLOCKTYPE blockType; NIBBLETYPE blockMeta; self->GetBlockTypeMeta(relX, relY, relZ, blockType, blockMeta); - L.Push(blockType); - L.Push(blockMeta); + L.Push(blockType, blockMeta); return 2; } diff --git a/src/Bindings/ManualBindings_RankManager.cpp b/src/Bindings/ManualBindings_RankManager.cpp index 99c0cc63f..7871c26ef 100644 --- a/src/Bindings/ManualBindings_RankManager.cpp +++ b/src/Bindings/ManualBindings_RankManager.cpp @@ -489,9 +489,7 @@ static int tolua_cRankManager_GetPlayerMsgVisuals(lua_State * L) } // Push the results: - S.Push(MsgPrefix); - S.Push(MsgSuffix); - S.Push(MsgNameColorCode); + S.Push(MsgPrefix, MsgSuffix, MsgNameColorCode); return 3; } @@ -752,9 +750,7 @@ static int tolua_cRankManager_GetRankVisuals(lua_State * L) } // Push the results: - S.Push(MsgPrefix); - S.Push(MsgSuffix); - S.Push(MsgNameColorCode); + S.Push(MsgPrefix, MsgSuffix, MsgNameColorCode); return 3; } diff --git a/src/Bindings/ManualBindings_World.cpp b/src/Bindings/ManualBindings_World.cpp index 9bb9778dd..f24e5ac34 100644 --- a/src/Bindings/ManualBindings_World.cpp +++ b/src/Bindings/ManualBindings_World.cpp @@ -296,10 +296,7 @@ static int tolua_cWorld_GetBlockInfo(lua_State * tolua_S) L.Push(res); if (res) { - L.Push(BlockType); - L.Push(BlockMeta); - L.Push(BlockSkyLight); - L.Push(BlockBlockLight); + L.Push(BlockType, BlockMeta, BlockSkyLight, BlockBlockLight); return 5; } return 1; @@ -345,8 +342,7 @@ static int tolua_cWorld_GetBlockTypeMeta(lua_State * tolua_S) L.Push(res); if (res) { - L.Push(BlockType); - L.Push(BlockMeta); + L.Push(BlockType, BlockMeta); return 3; } return 1; @@ -390,10 +386,7 @@ static int tolua_cWorld_GetSignLines(lua_State * tolua_S) L.Push(res); if (res) { - L.Push(Line1); - L.Push(Line2); - L.Push(Line3); - L.Push(Line4); + L.Push(Line1, Line2, Line3, Line4); return 5; } return 1; -- cgit v1.2.3