Skip to content

Commit

Permalink
Replace ItemCallbacks with lambdas (cuberite#3993)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbell10 authored and madmaxoft committed Sep 11, 2017
1 parent 115bc56 commit e225b7f
Show file tree
Hide file tree
Showing 69 changed files with 915 additions and 1,813 deletions.
4 changes: 2 additions & 2 deletions src/Bindings/LuaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ cLuaState::cStackTable::cStackTable(cLuaState & a_LuaState, int a_StackPos):



void cLuaState::cStackTable::ForEachArrayElement(std::function<bool(cLuaState & a_LuaState, int a_Index)> a_ElementCallback) const
void cLuaState::cStackTable::ForEachArrayElement(cFunctionRef<bool(cLuaState & a_LuaState, int a_Index)> a_ElementCallback) const
{
auto numElements = luaL_getn(m_LuaState, m_StackPos);
#ifdef _DEBUG
Expand All @@ -404,7 +404,7 @@ void cLuaState::cStackTable::ForEachArrayElement(std::function<bool(cLuaState &



void cLuaState::cStackTable::ForEachElement(std::function<bool(cLuaState & a_LuaState)> a_ElementCallback) const
void cLuaState::cStackTable::ForEachElement(cFunctionRef<bool(cLuaState & a_LuaState)> a_ElementCallback) const
{
#ifdef _DEBUG
auto stackTop = lua_gettop(m_LuaState);
Expand Down
5 changes: 3 additions & 2 deletions src/Bindings/LuaState.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern "C"
#include <functional>

#include "../Defines.h"
#include "../FunctionRef.h"
#include "PluginManager.h"
#include "LuaState_Typedefs.inc"

Expand Down Expand Up @@ -521,14 +522,14 @@ class cLuaState
The callback receives the LuaState in which the table resides, and the element's index. The LuaState has
the element on top of its stack. If the callback returns true, the iteration is aborted, if it returns
false, the iteration continues with the next element. */
void ForEachArrayElement(std::function<bool(cLuaState & a_LuaState, int a_Index)> a_ElementCallback) const;
void ForEachArrayElement(cFunctionRef<bool(cLuaState & a_LuaState, int a_Index)> a_ElementCallback) const;

/** Iterates over all dictionary elements in the table in random order, and calls the a_ElementCallback for
each of them.
The callback receives the LuaState in which the table reside. The LuaState has the element on top of its
stack, and the element's key just below it. If the callback returns true, the iteration is aborted, if it
returns false, the iteration continues with the next element. */
void ForEachElement(std::function<bool(cLuaState & a_LuaState)> a_ElementCallback) const;
void ForEachElement(cFunctionRef<bool(cLuaState & a_LuaState)> a_ElementCallback) const;

cLuaState & GetLuaState(void) const { return m_LuaState; }

Expand Down
15 changes: 4 additions & 11 deletions src/Bindings/LuaWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,15 @@ cLuaWindow::~cLuaWindow()
m_Contents.RemoveListener(*this);

// Close open lua window from players, to avoid dangling pointers
class cPlayerCallback : public cPlayerListCallback
{
virtual bool Item(cPlayer * a_Player)
cRoot::Get()->ForEachPlayer([this](cPlayer & a_Player)
{
if (a_Player->GetWindow() == m_LuaWindow)
if (a_Player.GetWindow() == this)
{
a_Player->CloseWindow(false);
a_Player.CloseWindow(false);
}
return false;
}
cLuaWindow * m_LuaWindow;
public:
cPlayerCallback(cLuaWindow & a_LuaWindow) { m_LuaWindow = &a_LuaWindow; }
} PlayerCallback(*this);

cRoot::Get()->ForEachPlayer(PlayerCallback);
);

// Must delete slot areas now, because they are referencing this->m_Contents and would try to access it in cWindow's
// destructor, when the member is already gone.
Expand Down
2 changes: 1 addition & 1 deletion src/Bindings/LuaWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


class cPlayer;
typedef cItemCallback<cPlayer> cPlayerListCallback;
typedef cFunctionRef<bool(cPlayer &)> cPlayerListCallback;


/** A window that has been created by a Lua plugin and is handled entirely by that plugin
Expand Down
68 changes: 20 additions & 48 deletions src/Bindings/ManualBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1483,44 +1483,29 @@ static int tolua_cPluginManager_CallPlugin(lua_State * tolua_S)
}

// Call the destination plugin using a plugin callback:
class cCallback :
public cPluginManager::cPluginCallback
{
public:
int m_NumReturns;

cCallback(const AString & a_FunctionName, cLuaState & a_SrcLuaState) :
m_NumReturns(0),
m_FunctionName(a_FunctionName),
m_SrcLuaState(a_SrcLuaState)
int NumReturns = 0;
auto PluginCallback = [&](cPlugin & a_Plugin)
{
}
protected:
const AString & m_FunctionName;
cLuaState & m_SrcLuaState;

virtual bool Item(cPlugin * a_Plugin) override
{
if (!a_Plugin->IsLoaded())
if (!a_Plugin.IsLoaded())
{
return false;
}
m_NumReturns = static_cast<cPluginLua *>(a_Plugin)->CallFunctionFromForeignState(
m_FunctionName, m_SrcLuaState, 4, lua_gettop(m_SrcLuaState)
NumReturns = static_cast<cPluginLua &>(a_Plugin).CallFunctionFromForeignState(
FunctionName, L, 4, lua_gettop(L)
);
return true;
}
} Callback(FunctionName, L);
if (!cPluginManager::Get()->DoWithPlugin(PluginName, Callback))
};

if (!cPluginManager::Get()->DoWithPlugin(PluginName, PluginCallback))
{
return 0;
}
if (Callback.m_NumReturns < 0)
if (NumReturns < 0)
{
// The call has failed, there are zero return values. Do NOT return negative number (Lua considers that a "yield")
return 0;
}
return Callback.m_NumReturns;
return NumReturns;
}


Expand Down Expand Up @@ -3243,42 +3228,29 @@ static int tolua_cRoot_DoWithPlayerByUUID(lua_State * tolua_S)
return 0;
}

class cCallback :
public cPlayerListCallback
{
public:
cCallback(cLuaState & a_LuaState) :
m_LuaState(a_LuaState)
{
}

virtual bool Item(cPlayer * a_Player) override
{
bool ret = false;
m_LuaState.Call(m_FnRef, a_Player, cLuaState::Return, ret);
return ret;
}

cLuaState & m_LuaState;
cLuaState::cRef m_FnRef;
} Callback(L);

// Get parameters:
cRoot * Self;
cUUID PlayerUUID;
L.GetStackValues(1, Self, PlayerUUID, Callback.m_FnRef);
cLuaState::cRef FnRef;
L.GetStackValues(1, Self, PlayerUUID, FnRef);

if (PlayerUUID.IsNil())
{
return L.ApiParamError("Expected a non-nil UUID for parameter #1");
}
if (!Callback.m_FnRef.IsValid())
if (!FnRef.IsValid())
{
return L.ApiParamError("Expected a valid callback function for parameter #2");
}

// Call the function:
bool res = Self->DoWithPlayerByUUID(PlayerUUID, Callback);
bool res = Self->DoWithPlayerByUUID(PlayerUUID, [&](cPlayer & a_Player)
{
bool ret = false;
L.Call(FnRef, &a_Player, cLuaState::Return, ret);
return ret;
}
);

// Push the result as the return value:
L.Push(res);
Expand Down
Loading

0 comments on commit e225b7f

Please sign in to comment.