summaryrefslogtreecommitdiff
path: root/commands.h
diff options
context:
space:
mode:
authorBossCode45 <human.cyborg42@gmail.com>2023-06-19 12:49:19 +1200
committerBossCode45 <human.cyborg42@gmail.com>2023-06-19 12:49:19 +1200
commit8b4b487e685c72a478baf74aea2bf756988fe550 (patch)
tree9a5ac8ffbd65942f4ce0117a5316f7db7f4eb918 /commands.h
parent6655d5dfb24ca3fd36b02550c526bca1f5d924e9 (diff)
parent185015b39b8db953b8034f29724ef03de09e7ea1 (diff)
downloadYATwm-8b4b487e685c72a478baf74aea2bf756988fe550.tar.gz
YATwm-8b4b487e685c72a478baf74aea2bf756988fe550.zip
Merge branch 'config-refactor'
Merging in the new config system with commands
Diffstat (limited to 'commands.h')
-rw-r--r--commands.h86
1 files changed, 86 insertions, 0 deletions
diff --git a/commands.h b/commands.h
new file mode 100644
index 0000000..d7f2351
--- /dev/null
+++ b/commands.h
@@ -0,0 +1,86 @@
+#pragma once
+
+#include "error.h"
+
+#include <vector>
+#include <string>
+#include <any>
+#include <functional>
+
+enum MoveDir
+ {
+ UP,
+ RIGHT,
+ DOWN,
+ LEFT
+ };
+enum CommandArgType
+ {
+ STR,
+ NUM,
+ MOVDIR,
+ STR_REST,
+ NUM_ARR_REST
+ };
+
+struct NumArr
+{
+ int* arr;
+ int size;
+};
+typedef union
+{
+ char* str;
+ int num;
+ NumArr numArr;
+ MoveDir dir;
+} CommandArg;
+
+struct Command
+{
+ const std::string name;
+ const std::function<void(std::any&, const CommandArg* argv)> func;
+ const std::function<void(const CommandArg* argv)> staticFunc;
+ const int argc;
+ CommandArgType* argTypes;
+ std::any* module;
+};
+class CommandsModule
+{
+private:
+ std::vector<Command> commandList;
+ std::vector<std::string> splitCommand(std::string command);
+ CommandArg* getCommandArgs(std::vector<std::string>& args, const CommandArgType* argTypes, const int argc);
+public:
+ CommandsModule();
+ ~CommandsModule();
+ template <class T>
+ void addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, CommandArgType* argTypes, T* module);
+ void addCommand(std::string name, const void(*func)(const CommandArg*), const int argc, CommandArgType* argTypes);
+ template <class T>
+ void addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, std::vector<CommandArgType> argTypes, T* module);
+ void addCommand(std::string name, const void(*func)(const CommandArg*), const int argc, std::vector<CommandArgType> argTypes);
+ void addCommand(Command c);
+ Command* lookupCommand(std::string name);
+ void runCommand(std::string command);
+ Err checkCommand(std::string command);
+};
+
+// YES I KNOW THIS IS BAD
+// but it needs to be done this way
+template <class T>
+void CommandsModule::addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, CommandArgType* argTypes, T* module)
+{
+ Command c = {name, (const void*(std::any::*)(const CommandArg* argv)) func, nullptr, argc, argTypes, (std::any*)module};
+ addCommand(c);
+}
+template <class T>
+void CommandsModule::addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, std::vector<CommandArgType> argTypes, T* module)
+{
+ CommandArgType* argTypesArr = new CommandArgType[argc];
+ for(int i = 0; i < argc; i++)
+ {
+ argTypesArr[i] = argTypes[i];
+ }
+ addCommand(name, func, argc, argTypesArr, module);
+}