From c1dc3e83d29cd22f5bc85b6fa94a8e6bf6d16939 Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Sun, 26 Oct 2014 21:44:04 +0000 Subject: [PATCH] Support lambdas in module commands --- include/znc/Modules.h | 13 +++++++++---- modules/notes.cpp | 7 +++---- src/Modules.cpp | 18 ++++++++++++------ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/include/znc/Modules.h b/include/znc/Modules.h index 8a59cdca..933ad341 100644 --- a/include/znc/Modules.h +++ b/include/znc/Modules.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -283,6 +284,7 @@ class CModCommand { public: /// Type for the callback function that handles the actual command. typedef void (CModule::*ModCmdFunc)(const CString& sLine); + typedef std::function CmdFunc; /// Default constructor, needed so that this can be saved in a std::map. CModCommand(); @@ -293,7 +295,8 @@ public: * @param sArgs Help text describing the arguments to this command. * @param sDesc Help text describing what this command does. */ - CModCommand(const CString& sCmd, ModCmdFunc func, const CString& sArgs, const CString& sDesc); + CModCommand(const CString& sCmd, CModule* pMod, ModCmdFunc func, const CString& sArgs, const CString& sDesc); + CModCommand(const CString& sCmd, CmdFunc func, const CString& sArgs, const CString& sDesc); /** Copy constructor, needed so that this can be saved in a std::map. * @param other Object to copy from. @@ -317,15 +320,15 @@ public: void AddHelp(CTable& Table) const; const CString& GetCommand() const { return m_sCmd; } - ModCmdFunc GetFunction() const { return m_pFunc; } + CmdFunc GetFunction() const { return m_pFunc; } const CString& GetArgs() const { return m_sArgs; } const CString& GetDescription() const { return m_sDesc; } - void Call(CModule *pMod, const CString& sLine) const { (pMod->*m_pFunc)(sLine); } + void Call(const CString& sLine) const { m_pFunc(sLine); } private: CString m_sCmd; - ModCmdFunc m_pFunc; + CmdFunc m_pFunc; CString m_sArgs; CString m_sDesc; }; @@ -926,6 +929,8 @@ public: bool AddCommand(const CModCommand& Command); /// @return True if the command was successfully added. bool AddCommand(const CString& sCmd, CModCommand::ModCmdFunc func, const CString& sArgs = "", const CString& sDesc = ""); + /// @return True if the command was successfully added. + bool AddCommand(const CString& sCmd, const CString& sArgs, const CString& sDesc, std::function func); /// @return True if the command was successfully removed. bool RemCommand(const CString& sCmd); /// @return The CModCommand instance or NULL if none was found. diff --git a/modules/notes.cpp b/modules/notes.cpp index 53221719..26ab79af 100644 --- a/modules/notes.cpp +++ b/modules/notes.cpp @@ -71,16 +71,15 @@ class CNotesMod : public CModule { public: MODCONSTRUCTOR(CNotesMod) { + using std::placeholders::_1; AddHelpCommand(); AddCommand("List", static_cast(&CNotesMod::ListCommand)); AddCommand("Add", static_cast(&CNotesMod::AddNoteCommand), " "); AddCommand("Del", static_cast(&CNotesMod::DelCommand), "", "Delete a note"); - AddCommand("Mod", static_cast(&CNotesMod::ModCommand), - " ", "Modify a note"); - AddCommand("Get", static_cast(&CNotesMod::GetCommand), - ""); + AddCommand("Mod", " ", "Modify a note", std::bind(&CNotesMod::ModCommand, this, _1)); + AddCommand("Get", "", "", [this](const CString& sLine){ GetCommand(sLine); }); } virtual ~CNotesMod() {} diff --git a/src/Modules.cpp b/src/Modules.cpp index 2a14a460..4f13ce35 100644 --- a/src/Modules.cpp +++ b/src/Modules.cpp @@ -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 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)