Support lambdas in module commands

This commit is contained in:
Alexey Sokolov
2014-10-26 21:44:04 +00:00
parent 6509e26b89
commit c1dc3e83d2
3 changed files with 24 additions and 14 deletions

View File

@@ -530,10 +530,15 @@ bool CModule::AddCommand(const CModCommand& Command)
bool CModule::AddCommand(const CString& sCmd, CModCommand::ModCmdFunc func, const CString& sArgs, const CString& sDesc)
{
CModCommand cmd(sCmd, func, sArgs, sDesc);
CModCommand cmd(sCmd, this, func, sArgs, sDesc);
return AddCommand(cmd);
}
bool CModule::AddCommand(const CString& sCmd, const CString& sArgs, const CString& sDesc, std::function<void(const CString& sLine)> func) {
CModCommand cmd(sCmd, std::move(func), sArgs, sDesc);
return AddCommand(std::move(cmd));
}
void CModule::AddHelpCommand()
{
AddCommand("Help", &CModule::HandleHelpCommand, "search", "Generate this output");
@@ -560,7 +565,7 @@ bool CModule::HandleCommand(const CString& sLine) {
const CModCommand* pCmd = FindCommand(sCmd);
if (pCmd) {
pCmd->Call(this, sLine);
pCmd->Call(sLine);
return true;
}
@@ -1343,10 +1348,11 @@ CModCommand::CModCommand()
{
}
CModCommand::CModCommand(const CString& sCmd, ModCmdFunc func, const CString& sArgs, const CString& sDesc)
: m_sCmd(sCmd), m_pFunc(func), m_sArgs(sArgs), m_sDesc(sDesc)
{
}
CModCommand::CModCommand(const CString& sCmd, CModule* pMod, ModCmdFunc func, const CString& sArgs, const CString& sDesc)
: m_sCmd(sCmd), m_pFunc([pMod, func](const CString& sLine) { (pMod->*func)(sLine); }), m_sArgs(sArgs), m_sDesc(sDesc) {}
CModCommand::CModCommand(const CString& sCmd, CmdFunc func, const CString& sArgs, const CString& sDesc)
: m_sCmd(sCmd), m_pFunc(std::move(func)), m_sArgs(sArgs), m_sDesc(sDesc) {}
CModCommand::CModCommand(const CModCommand& other)
: m_sCmd(other.m_sCmd), m_pFunc(other.m_pFunc), m_sArgs(other.m_sArgs), m_sDesc(other.m_sDesc)