1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Add function to retrieve command listeners associated with a certain manager as array or table and also to iterate them with a callback. Should close #19

This commit is contained in:
Sandu Liviu Catalin 2016-07-14 19:20:49 +03:00
parent 1d5b12f11f
commit 463dc75d91
2 changed files with 86 additions and 0 deletions

View File

@ -1082,6 +1082,9 @@ void Register(HSQUIRRELVM vm)
.Func(_SC("FindByName"), &Manager::FindByName)
.Func(_SC("BindOnFail"), &Manager::SetOnFail)
.Func(_SC("BindOnAuth"), &Manager::SetOnAuth)
.Func(_SC("GetArray"), &Manager::GetCommandsArray)
.Func(_SC("GetTable"), &Manager::GetCommandsTable)
.Func(_SC("Foreach"), &Manager::ForeachCommand)
// Member Overloads
.Overload< Object (Manager::*)(CSStr) >(_SC("Create"), &Manager::Create)
.Overload< Object (Manager::*)(CSStr, CSStr) >(_SC("Create"), &Manager::Create)

View File

@ -703,6 +703,57 @@ public:
// Return the requested information
return m_Context->mArgument;
}
/* --------------------------------------------------------------------------------------------
* Retrieve all command listeners in an array.
*/
Array GetCommandsArray() const
{
// Allocate an array with an adequate size
Array arr(DefaultVM::Get(), m_Commands.size());
// Index of the currently processed command listener
SQInteger index = 0;
// Populate the array with the command listeners
for (const auto & cmd : m_Commands)
{
arr.SetValue(index++, cmd.mObj);
}
// Return the resulted array
return arr;
}
/* --------------------------------------------------------------------------------------------
* Retrieve all command listeners in a table.
*/
Table GetCommandsTable() const
{
// Allocate an empty table
Table tbl(DefaultVM::Get());
// Populate the table with the command listeners
for (const auto & cmd : m_Commands)
{
tbl.SetValue(cmd.mName.c_str(), cmd.mObj);
}
// Return the resulted table
return tbl;
}
/* --------------------------------------------------------------------------------------------
* Process all command listeners with a function.
*/
void ForeachCommand(Function & func) const
{
// Make sure that the specified function works
if (func.IsNull())
{
return;
}
// Process all the managed command listeners
for (const auto & cmd : m_Commands)
{
func.Execute(cmd.mObj);
}
}
};
/* ------------------------------------------------------------------------------------------------
@ -928,6 +979,38 @@ public:
return GetValid()->GetArgument();
}
/* --------------------------------------------------------------------------------------------
* Retrieve all command listeners in an array.
*/
Array GetCommandsArray() const
{
return GetValid()->GetCommandsArray();
}
/* --------------------------------------------------------------------------------------------
* Retrieve all command listeners in a table.
*/
Table GetCommandsTable() const
{
return GetValid()->GetCommandsTable();
}
/* --------------------------------------------------------------------------------------------
* Process all command listeners with a function.
*/
void ForeachCommand(Object & env, Function & func) const
{
if (env.IsNull())
{
GetValid()->ForeachCommand(func);
}
else
{
Function fn(env.GetVM(), env, func.GetFunc());
GetValid()->ForeachCommand(fn);
}
}
/* --------------------------------------------------------------------------------------------
* Create command instances and obtain the associated object.
*/