From b1ec20982b5c0195edbc0c5e9553b203fbc1a159 Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Mon, 11 Dec 2023 07:52:44 +0100 Subject: [PATCH] figments: start building generic command-execution framework --- lib/Figments/Command.cpp | 7 +++++++ lib/Figments/Command.h | 33 +++++++++++++++++++++++++++++++++ lib/Figments/Figment.cpp | 9 +++++++++ lib/Figments/Figment.h | 3 +++ 4 files changed, 52 insertions(+) create mode 100644 lib/Figments/Command.cpp create mode 100644 lib/Figments/Command.h create mode 100644 lib/Figments/Figment.cpp diff --git a/lib/Figments/Command.cpp b/lib/Figments/Command.cpp new file mode 100644 index 0000000..33df959 --- /dev/null +++ b/lib/Figments/Command.cpp @@ -0,0 +1,7 @@ +#include "./Command.h" + +void +doNothing(Args& args, Print& printer) +{} + +Command::Command() : func(doNothing) {} diff --git a/lib/Figments/Command.h b/lib/Figments/Command.h new file mode 100644 index 0000000..87cf490 --- /dev/null +++ b/lib/Figments/Command.h @@ -0,0 +1,33 @@ +#pragma once +#include + +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 CommandList; + +struct Command { + using Executor = std::function; + Executor func; + const char* name = NULL; + + Command(); + Command(const char* name, Executor func) : name(name), func(func) {} +}; diff --git a/lib/Figments/Figment.cpp b/lib/Figments/Figment.cpp new file mode 100644 index 0000000..289d1c0 --- /dev/null +++ b/lib/Figments/Figment.cpp @@ -0,0 +1,9 @@ +#include "./Figment.h" + +const std::vector emptyCommands; + +const std::vector& +Task::commands() const +{ + return emptyCommands; +} diff --git a/lib/Figments/Figment.h b/lib/Figments/Figment.h index 87d7af7..5937fcf 100644 --- a/lib/Figments/Figment.h +++ b/lib/Figments/Figment.h @@ -2,6 +2,7 @@ #include #include #include +#include "./Command.h" #define F_LIKELY(x) __builtin_expect(!!(x), true) #define F_UNLIKELY(x) __builtin_expect(!!(x), false) @@ -68,6 +69,8 @@ struct Task : public virtual Loopable { const char* name = ""; State state = Stopped; + + virtual const std::vector &commands() const; }; /**