figments: command: rewrite command api to use Task instances instead of static functions

This commit is contained in:
Torrie Fischer
2023-12-20 09:13:23 +01:00
parent 214825c1d3
commit 6e138175be
14 changed files with 76 additions and 81 deletions

View File

@ -1,7 +1,2 @@
#include "./Command.h"
void
doNothing(Args& args, Print& printer)
{}
Command::Command() : func(doNothing) {}

View File

@ -21,13 +21,25 @@ class Args {
}
};
struct CommandList;
struct Task;
struct Command {
using Executor = std::function<void(Args&, Print& output)>;
Executor func;
const char* name = NULL;
using Executor = void(Task::*)(Args&, Print&);
template<typename T>
using MemberExecutor = void(T::*)(Args&, Print&);
const char* name = NULL;
Executor func = NULL;
void invoke(Task* task, Args& args, Print& printer) const {
if (func) {
(*task.*func)(args, printer);
}
}
Command();
Command(const char* name, Executor func) : name(name), func(func) {}
template<class T>
Command(const char* name, MemberExecutor<T> func) : name(name), func(static_cast<Executor>(func)) {}
};