Converted cLuaState::cCallbackPtr into a UniquePtr.
This commit is contained in:
parent
7a6670d1d1
commit
bf88312a16
@ -115,8 +115,8 @@ void cLuaChunkStay::AddChunkCoord(cLuaState & L, int a_Index)
|
|||||||
|
|
||||||
void cLuaChunkStay::Enable(cChunkMap & a_ChunkMap, cLuaState::cCallbackPtr a_OnChunkAvailable, cLuaState::cCallbackPtr a_OnAllChunksAvailable)
|
void cLuaChunkStay::Enable(cChunkMap & a_ChunkMap, cLuaState::cCallbackPtr a_OnChunkAvailable, cLuaState::cCallbackPtr a_OnAllChunksAvailable)
|
||||||
{
|
{
|
||||||
m_OnChunkAvailable = a_OnChunkAvailable;
|
m_OnChunkAvailable = std::move(a_OnChunkAvailable);
|
||||||
m_OnAllChunksAvailable = a_OnAllChunksAvailable;
|
m_OnAllChunksAvailable = std::move(a_OnAllChunksAvailable);
|
||||||
|
|
||||||
// Enable the ChunkStay:
|
// Enable the ChunkStay:
|
||||||
super::Enable(a_ChunkMap);
|
super::Enable(a_ChunkMap);
|
||||||
|
@ -1020,6 +1020,19 @@ bool cLuaState::GetStackValue(int a_StackPos, cCallback & a_Callback)
|
|||||||
|
|
||||||
|
|
||||||
bool cLuaState::GetStackValue(int a_StackPos, cCallbackPtr & a_Callback)
|
bool cLuaState::GetStackValue(int a_StackPos, cCallbackPtr & a_Callback)
|
||||||
|
{
|
||||||
|
if (a_Callback == nullptr)
|
||||||
|
{
|
||||||
|
a_Callback = cpp14::make_unique<cCallback>();
|
||||||
|
}
|
||||||
|
return a_Callback->RefStack(*this, a_StackPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool cLuaState::GetStackValue(int a_StackPos, cCallbackSharedPtr & a_Callback)
|
||||||
{
|
{
|
||||||
if (a_Callback == nullptr)
|
if (a_Callback == nullptr)
|
||||||
{
|
{
|
||||||
|
@ -216,7 +216,8 @@ public:
|
|||||||
Use cCallbackPtr for a copyable object. */
|
Use cCallbackPtr for a copyable object. */
|
||||||
cCallback(cCallback &&) = delete;
|
cCallback(cCallback &&) = delete;
|
||||||
};
|
};
|
||||||
typedef SharedPtr<cCallback> cCallbackPtr;
|
typedef UniquePtr<cCallback> cCallbackPtr;
|
||||||
|
typedef SharedPtr<cCallback> cCallbackSharedPtr;
|
||||||
|
|
||||||
|
|
||||||
/** A dummy class that's used only to delimit function args from return values for cLuaState::Call() */
|
/** A dummy class that's used only to delimit function args from return values for cLuaState::Call() */
|
||||||
@ -380,6 +381,7 @@ public:
|
|||||||
bool GetStackValue(int a_StackPos, bool & a_Value);
|
bool GetStackValue(int a_StackPos, bool & a_Value);
|
||||||
bool GetStackValue(int a_StackPos, cCallback & a_Callback);
|
bool GetStackValue(int a_StackPos, cCallback & a_Callback);
|
||||||
bool GetStackValue(int a_StackPos, cCallbackPtr & a_Callback);
|
bool GetStackValue(int a_StackPos, cCallbackPtr & a_Callback);
|
||||||
|
bool GetStackValue(int a_StackPos, cCallbackSharedPtr & a_Callback);
|
||||||
bool GetStackValue(int a_StackPos, cPluginManager::CommandResult & a_Result);
|
bool GetStackValue(int a_StackPos, cPluginManager::CommandResult & a_Result);
|
||||||
bool GetStackValue(int a_StackPos, cRef & a_Ref);
|
bool GetStackValue(int a_StackPos, cRef & a_Ref);
|
||||||
bool GetStackValue(int a_StackPos, double & a_Value);
|
bool GetStackValue(int a_StackPos, double & a_Value);
|
||||||
|
@ -66,26 +66,26 @@ cLuaWindow::~cLuaWindow()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cLuaWindow::SetOnClosing(cLuaState::cCallbackPtr a_OnClosing)
|
void cLuaWindow::SetOnClosing(cLuaState::cCallbackPtr && a_OnClosing)
|
||||||
{
|
{
|
||||||
// Only one Lua state can be a cLuaWindow object callback:
|
// Only one Lua state can be a cLuaWindow object callback:
|
||||||
ASSERT(a_OnClosing->IsSameLuaState(*m_LuaState));
|
ASSERT(a_OnClosing->IsSameLuaState(*m_LuaState));
|
||||||
|
|
||||||
// Store the new reference, releasing the old one if appropriate:
|
// Store the new reference, releasing the old one if appropriate:
|
||||||
m_OnClosing = a_OnClosing;
|
m_OnClosing = std::move(a_OnClosing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cLuaWindow::SetOnSlotChanged(cLuaState::cCallbackPtr a_OnSlotChanged)
|
void cLuaWindow::SetOnSlotChanged(cLuaState::cCallbackPtr && a_OnSlotChanged)
|
||||||
{
|
{
|
||||||
// Only one Lua state can be a cLuaWindow object callback:
|
// Only one Lua state can be a cLuaWindow object callback:
|
||||||
ASSERT(a_OnSlotChanged->IsSameLuaState(*m_LuaState));
|
ASSERT(a_OnSlotChanged->IsSameLuaState(*m_LuaState));
|
||||||
|
|
||||||
// Store the new reference, releasing the old one if appropriate:
|
// Store the new reference, releasing the old one if appropriate:
|
||||||
m_OnSlotChanged = a_OnSlotChanged;
|
m_OnSlotChanged = std::move(a_OnSlotChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,10 +50,10 @@ public:
|
|||||||
// tolua_end
|
// tolua_end
|
||||||
|
|
||||||
/** Sets the Lua callback function to call when the window is about to close */
|
/** Sets the Lua callback function to call when the window is about to close */
|
||||||
void SetOnClosing(cLuaState::cCallbackPtr a_OnClosing);
|
void SetOnClosing(cLuaState::cCallbackPtr && a_OnClosing);
|
||||||
|
|
||||||
/** Sets the Lua callback function to call when a slot is changed */
|
/** Sets the Lua callback function to call when a slot is changed */
|
||||||
void SetOnSlotChanged(cLuaState::cCallbackPtr a_OnSlotChanged);
|
void SetOnSlotChanged(cLuaState::cCallbackPtr && a_OnSlotChanged);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
@ -51,8 +51,8 @@ class LuaCommandHandler:
|
|||||||
public cPluginManager::cCommandHandler
|
public cPluginManager::cCommandHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LuaCommandHandler(cLuaState::cCallbackPtr a_Callback):
|
LuaCommandHandler(cLuaState::cCallbackPtr && a_Callback):
|
||||||
m_Callback(a_Callback)
|
m_Callback(std::move(a_Callback))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1052,14 +1052,14 @@ static int tolua_cPluginManager_AddHook_FnRef(cPluginManager * a_PluginManager,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add the hook to the plugin
|
// Add the hook to the plugin
|
||||||
auto callback = std::make_shared<cLuaState::cCallback>();
|
cLuaState::cCallbackPtr callback;
|
||||||
if (!S.GetStackValue(a_ParamIdx + 1, callback))
|
if (!S.GetStackValue(a_ParamIdx + 1, callback))
|
||||||
{
|
{
|
||||||
LOGWARNING("cPluginManager.AddHook(): Cannot read the callback parameter");
|
LOGWARNING("cPluginManager.AddHook(): Cannot read the callback parameter");
|
||||||
S.LogStackTrace();
|
S.LogStackTrace();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (!Plugin->AddHookCallback(HookType, callback))
|
if (!Plugin->AddHookCallback(HookType, std::move(callback)))
|
||||||
{
|
{
|
||||||
LOGWARNING("cPluginManager.AddHook(): Cannot add hook %d, unknown error.", HookType);
|
LOGWARNING("cPluginManager.AddHook(): Cannot add hook %d, unknown error.", HookType);
|
||||||
S.LogStackTrace();
|
S.LogStackTrace();
|
||||||
@ -1116,7 +1116,7 @@ static int tolua_cPluginManager_AddHook_DefFn(cPluginManager * a_PluginManager,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the function to call and add it to the plugin:
|
// Retrieve the function to call and add it to the plugin:
|
||||||
auto callback = std::make_shared<cLuaState::cCallback>();
|
cLuaState::cCallbackPtr callback;
|
||||||
lua_getglobal(S, FnName);
|
lua_getglobal(S, FnName);
|
||||||
bool res = S.GetStackValue(-1, callback);
|
bool res = S.GetStackValue(-1, callback);
|
||||||
lua_pop(S, 1);
|
lua_pop(S, 1);
|
||||||
@ -1360,7 +1360,7 @@ static int tolua_cPluginManager_BindCommand(lua_State * a_LuaState)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto CommandHandler = std::make_shared<LuaCommandHandler>(Handler);
|
auto CommandHandler = std::make_shared<LuaCommandHandler>(std::move(Handler));
|
||||||
if (!self->BindCommand(Command, Plugin, CommandHandler, Permission, HelpString))
|
if (!self->BindCommand(Command, Plugin, CommandHandler, Permission, HelpString))
|
||||||
{
|
{
|
||||||
// Refused. Possibly already bound. Error message has been given, display the callstack:
|
// Refused. Possibly already bound. Error message has been given, display the callstack:
|
||||||
@ -1425,7 +1425,7 @@ static int tolua_cPluginManager_BindConsoleCommand(lua_State * a_LuaState)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto CommandHandler = std::make_shared<LuaCommandHandler>(Handler);
|
auto CommandHandler = std::make_shared<LuaCommandHandler>(std::move(Handler));
|
||||||
if (!self->BindConsoleCommand(Command, Plugin, CommandHandler, HelpString))
|
if (!self->BindConsoleCommand(Command, Plugin, CommandHandler, HelpString))
|
||||||
{
|
{
|
||||||
// Refused. Possibly already bound. Error message has been given, display the callstack:
|
// Refused. Possibly already bound. Error message has been given, display the callstack:
|
||||||
@ -1666,7 +1666,7 @@ static int tolua_cPlayer_PermissionMatches(lua_State * tolua_S)
|
|||||||
|
|
||||||
template <
|
template <
|
||||||
class OBJTYPE,
|
class OBJTYPE,
|
||||||
void (OBJTYPE::*SetCallback)(cLuaState::cCallbackPtr a_CallbackFn)
|
void (OBJTYPE::*SetCallback)(cLuaState::cCallbackPtr && a_CallbackFn)
|
||||||
>
|
>
|
||||||
static int tolua_SetObjectCallback(lua_State * tolua_S)
|
static int tolua_SetObjectCallback(lua_State * tolua_S)
|
||||||
{
|
{
|
||||||
@ -1684,7 +1684,7 @@ static int tolua_SetObjectCallback(lua_State * tolua_S)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set the callback
|
// Set the callback
|
||||||
(self->*SetCallback)(callback);
|
(self->*SetCallback)(std::move(callback));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +105,7 @@ static int tolua_cWorld_ChunkStay(lua_State * tolua_S)
|
|||||||
|
|
||||||
cLuaState::cCallbackPtr onChunkAvailable, onAllChunksAvailable;
|
cLuaState::cCallbackPtr onChunkAvailable, onAllChunksAvailable;
|
||||||
L.GetStackValues(3, onChunkAvailable, onAllChunksAvailable); // Callbacks may be unassigned at all - as a request to load / generate chunks
|
L.GetStackValues(3, onChunkAvailable, onAllChunksAvailable); // Callbacks may be unassigned at all - as a request to load / generate chunks
|
||||||
ChunkStay->Enable(*World->GetChunkMap(), onChunkAvailable, onAllChunksAvailable);
|
ChunkStay->Enable(*World->GetChunkMap(), std::move(onChunkAvailable), std::move(onAllChunksAvailable));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -484,7 +484,7 @@ static int tolua_cWorld_QueueTask(lua_State * tolua_S)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
cWorld * World;
|
cWorld * World;
|
||||||
auto Task = std::make_shared<cLuaState::cCallback>();
|
cLuaState::cCallbackSharedPtr Task;
|
||||||
if (!L.GetStackValues(1, World, Task))
|
if (!L.GetStackValues(1, World, Task))
|
||||||
{
|
{
|
||||||
return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Cannot read parameters");
|
return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Cannot read parameters");
|
||||||
|
@ -260,7 +260,7 @@ bool cPluginLua::OnChat(cPlayer & a_Player, AString & a_Message)
|
|||||||
}
|
}
|
||||||
bool res = false;
|
bool res = false;
|
||||||
auto & hooks = m_HookMap[cPluginManager::HOOK_CHAT];
|
auto & hooks = m_HookMap[cPluginManager::HOOK_CHAT];
|
||||||
for (auto hook: hooks)
|
for (auto & hook: hooks)
|
||||||
{
|
{
|
||||||
hook->Call(&a_Player, a_Message, cLuaState::Return, res, a_Message);
|
hook->Call(&a_Player, a_Message, cLuaState::Return, res, a_Message);
|
||||||
if (res)
|
if (res)
|
||||||
@ -1096,9 +1096,9 @@ const char * cPluginLua::GetHookFnName(int a_HookType)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool cPluginLua::AddHookCallback(int a_HookType, cLuaState::cCallbackPtr a_Callback)
|
bool cPluginLua::AddHookCallback(int a_HookType, cLuaState::cCallbackPtr && a_Callback)
|
||||||
{
|
{
|
||||||
m_HookMap[a_HookType].push_back(a_Callback);
|
m_HookMap[a_HookType].push_back(std::move(a_Callback));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@ public:
|
|||||||
|
|
||||||
/** Adds a Lua callback to be called for the specified hook.
|
/** Adds a Lua callback to be called for the specified hook.
|
||||||
Returns true if the hook was added successfully. */
|
Returns true if the hook was added successfully. */
|
||||||
bool AddHookCallback(int a_HookType, cLuaState::cCallbackPtr a_Callback);
|
bool AddHookCallback(int a_HookType, cLuaState::cCallbackPtr && a_Callback);
|
||||||
|
|
||||||
/** Calls a function in this plugin's LuaState with parameters copied over from a_ForeignState.
|
/** Calls a function in this plugin's LuaState with parameters copied over from a_ForeignState.
|
||||||
The values that the function returns are placed onto a_ForeignState.
|
The values that the function returns are placed onto a_ForeignState.
|
||||||
|
Loading…
Reference in New Issue
Block a user