Skip to content

Commit

Permalink
Wider use of lambdas
Browse files Browse the repository at this point in the history
  * Replace cItemCallback with cFunctionRef for lambda compatibility
  * Replace some use of "ad hoc functor classes" with lambdas.
  • Loading branch information
peterbell10 committed Aug 24, 2017
1 parent b55e5f5 commit a0880ed
Show file tree
Hide file tree
Showing 68 changed files with 897 additions and 1,781 deletions.
4 changes: 2 additions & 2 deletions src/Bindings/LuaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,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 @@ -403,7 +403,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
35 changes: 10 additions & 25 deletions src/Bindings/ManualBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1482,44 +1482,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)
{
}
protected:
const AString & m_FunctionName;
cLuaState & m_SrcLuaState;

virtual bool Item(cPlugin * a_Plugin) override
int NumReturns = 0;
auto PluginCallback = [&](cPlugin & a_Plugin)
{
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
Loading

0 comments on commit a0880ed

Please sign in to comment.