summaryrefslogtreecommitdiff
path: root/commands.h
blob: 2e364ace969c8d366320ac9843e30422da54db4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#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 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 printHello(const CommandArg* argv);
		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(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, argc, argTypes, (std::any*)module};
	addCommand(c);
}