renderbug/lib/Figments/Command.h

49 lines
1.1 KiB
C
Raw Permalink Normal View History

#pragma once
#include <Arduino.h>
class Args {
private:
String *str;
public:
Args(String *str) : str(str) {}
2023-12-26 10:29:49 +00:00
operator const char*() const {
return str->c_str();
}
String operator[](int pos) {
char buf[64];
strncpy(buf, str->c_str(), sizeof(buf));
char *args = strtok(buf, " ");
while (pos > 0 && args != NULL) {
args = strtok(NULL, " ");
pos--;
}
if (args == NULL) {
return String();
}
return String(args);
}
};
struct Task;
struct Command {
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(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)) {}
};