mirror of
https://github.com/znc/znc.git
synced 2026-05-07 05:44:41 +02:00
Support lambdas in module commands
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include <znc/WebModules.h>
|
||||
#include <znc/Threads.h>
|
||||
#include <znc/main.h>
|
||||
#include <functional>
|
||||
#include <set>
|
||||
#include <queue>
|
||||
|
||||
@@ -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<void(const CString& sLine)> 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<void(const CString& sLine)> func);
|
||||
/// @return True if the command was successfully removed.
|
||||
bool RemCommand(const CString& sCmd);
|
||||
/// @return The CModCommand instance or NULL if none was found.
|
||||
|
||||
+3
-4
@@ -71,16 +71,15 @@ class CNotesMod : public CModule {
|
||||
|
||||
public:
|
||||
MODCONSTRUCTOR(CNotesMod) {
|
||||
using std::placeholders::_1;
|
||||
AddHelpCommand();
|
||||
AddCommand("List", static_cast<CModCommand::ModCmdFunc>(&CNotesMod::ListCommand));
|
||||
AddCommand("Add", static_cast<CModCommand::ModCmdFunc>(&CNotesMod::AddNoteCommand),
|
||||
"<key> <note>");
|
||||
AddCommand("Del", static_cast<CModCommand::ModCmdFunc>(&CNotesMod::DelCommand),
|
||||
"<key>", "Delete a note");
|
||||
AddCommand("Mod", static_cast<CModCommand::ModCmdFunc>(&CNotesMod::ModCommand),
|
||||
"<key> <note>", "Modify a note");
|
||||
AddCommand("Get", static_cast<CModCommand::ModCmdFunc>(&CNotesMod::GetCommand),
|
||||
"<key>");
|
||||
AddCommand("Mod", "<key> <note>", "Modify a note", std::bind(&CNotesMod::ModCommand, this, _1));
|
||||
AddCommand("Get", "<key>", "", [this](const CString& sLine){ GetCommand(sLine); });
|
||||
}
|
||||
|
||||
virtual ~CNotesMod() {}
|
||||
|
||||
+12
-6
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user