diff options
| author | BossCode45 <human.cyborg42@gmail.com> | 2024-12-01 19:24:09 +1300 |
|---|---|---|
| committer | BossCode45 <human.cyborg42@gmail.com> | 2024-12-01 19:24:09 +1300 |
| commit | 8dca89a1be23f0de2dd1676b95feb6b46cbdd5f2 (patch) | |
| tree | 7ebc1a5161bbe70d6ab6a0e5353a1a622240c915 /src/commands.h | |
| parent | e162dff48c251e262f475de9261f0ecfa0f39dc4 (diff) | |
| parent | 434ec6542d0d79190c6aa7003aac91b03cad4398 (diff) | |
| download | YATwm-8dca89a1be23f0de2dd1676b95feb6b46cbdd5f2.tar.gz YATwm-8dca89a1be23f0de2dd1676b95feb6b46cbdd5f2.zip | |
Merge branch 'IPC'
Diffstat (limited to 'src/commands.h')
| -rw-r--r-- | src/commands.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/commands.h b/src/commands.h new file mode 100644 index 0000000..af4a4a0 --- /dev/null +++ b/src/commands.h @@ -0,0 +1,89 @@ +#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); + const void echo(const CommandArg* argv); +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); + void runCommand(std::vector<std::string> split); + std::vector<Err> checkCommand(std::string command); + Err checkCommand(std::vector<std::string> split); +}; + +// 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); +} |
