renderbug-cpp/lib/Figments/Command.h

46 lines
1018 B
C++

#pragma once
#include <Arduino.h>
class Args {
private:
String *str;
public:
Args(String *str) : str(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)) {}
};