From 6b3dff404248bcf03e0bf0463a8c53d8918f7eec Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Wed, 31 May 2023 20:55:49 +1200 Subject: Added support for commands without a module Also made it easier to add commands by making the ~addCommand~ function accept a vector for ~argTypes~ and then convert it. --- commands.h | 73 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 29 deletions(-) (limited to 'commands.h') diff --git a/commands.h b/commands.h index 2e364ac..92801bd 100644 --- a/commands.h +++ b/commands.h @@ -8,20 +8,20 @@ #include enum MoveDir -{ - UP, - RIGHT, - DOWN, - LEFT -}; + { + UP, + RIGHT, + DOWN, + LEFT + }; enum CommandArgType -{ - STR, - NUM, - MOVDIR, - STR_REST, - NUM_ARR_REST -}; + { + STR, + NUM, + MOVDIR, + STR_REST, + NUM_ARR_REST + }; struct NumArr { @@ -40,27 +40,32 @@ struct Command { const std::string name; const std::function func; + const std::function staticFunc; const int argc; CommandArgType* argTypes; std::any* module; }; class CommandsModule { - private: - std::vector commandList; - std::vector splitCommand(std::string command); - CommandArg* getCommandArgs(std::vector& args, const CommandArgType* argTypes, const int argc); - const void printHello(const CommandArg* argv); - const void echo(const CommandArg* argv); - public: - CommandsModule(); - ~CommandsModule(); - template - void addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, CommandArgType* argTypes, T* module); - void addCommand(Command c); - Command* lookupCommand(std::string name); - void runCommand(std::string command); - Err checkCommand(std::string command); +private: + std::vector commandList; + std::vector splitCommand(std::string command); + CommandArg* getCommandArgs(std::vector& args, const CommandArgType* argTypes, const int argc); + const void printHello(const CommandArg* argv); + const void echo(const CommandArg* argv); +public: + CommandsModule(); + ~CommandsModule(); + template + 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 + void addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, std::vector argTypes, T* module); + void addCommand(std::string name, const void(*func)(const CommandArg*), const int argc, std::vector 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 @@ -68,6 +73,16 @@ class CommandsModule template 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, argc, argTypes, (std::any*)module}; + Command c = {name, (const void*(std::any::*)(const CommandArg* argv)) func, nullptr, argc, argTypes, (std::any*)module}; addCommand(c); } +template +void CommandsModule::addCommand(std::string name, const void(T::*func)(const CommandArg*), const int argc, std::vector 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); +} -- cgit v1.2.3