1 | /*! |
---|
2 | * @file shell_command.h |
---|
3 | * Definition of a on-screen-shell |
---|
4 | */ |
---|
5 | |
---|
6 | #ifndef _SHELL_COMMAND_H |
---|
7 | #define _SHELL_COMMAND_H |
---|
8 | |
---|
9 | #include "base_object.h" |
---|
10 | |
---|
11 | #include "helper_functions.h" |
---|
12 | #include "multi_type.h" |
---|
13 | #include "substring.h" |
---|
14 | #include "functor_list.h" |
---|
15 | #include "executor/executor.h" |
---|
16 | #include <stdarg.h> |
---|
17 | |
---|
18 | #define SHELL_COMMAND_MAX_SIZE //!< The maximum size of a Shell Command |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | // FORWARD DECLARATION |
---|
23 | template<class T> class tList; |
---|
24 | |
---|
25 | /** |
---|
26 | * an easy to use Macro to create a Command |
---|
27 | * @param command the name of the command (without "" around the string) |
---|
28 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
29 | * @param function the function to call |
---|
30 | * |
---|
31 | * MEANING: |
---|
32 | * ShellCommand* someUniqueVarName = |
---|
33 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
34 | * |
---|
35 | * In the Shell you would call this Command using: |
---|
36 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
37 | */ |
---|
38 | //#define SHELL_COMMAND(command, class, function) \ |
---|
39 | // ShellCommand* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function) |
---|
40 | #define SHELL_COMMAND(command, class, function) \ |
---|
41 | ShellCommand* shell_command_##class##_##command = ShellCommand::registerCommand(#command, #class, new ExecutorObjective<class>(&class::function)) |
---|
42 | |
---|
43 | /** |
---|
44 | * an easy to use Macro to create a Command |
---|
45 | * @param command the name of the command (without "" around the string) |
---|
46 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
47 | * @param function the function to call |
---|
48 | * |
---|
49 | * MEANING: |
---|
50 | * ShellCommand* someUniqueVarName = |
---|
51 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
52 | * |
---|
53 | * In the Shell you would call this Command using: |
---|
54 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
55 | */ |
---|
56 | #define SHELL_COMMAND_STATIC(command, class, function) \ |
---|
57 | ShellCommand* shell_command_##class##_##command = ShellCommand::registerCommand(#command, #class, new ExecutorStatic<class>(function)) |
---|
58 | |
---|
59 | |
---|
60 | //! an enumerator for the definition of the Type. |
---|
61 | typedef enum { |
---|
62 | ShellCommand_Objective = 1, |
---|
63 | ShellCommand_Static = 2, |
---|
64 | } ShellCommand_Type; |
---|
65 | |
---|
66 | //////////////// |
---|
67 | // BASE CLASS // |
---|
68 | //////////////// |
---|
69 | class ShellCommand; |
---|
70 | class ShellCommandAlias; |
---|
71 | |
---|
72 | //! A class to hold all Classes that have (once) registered Commands. |
---|
73 | class ShellCommandClass : public BaseObject |
---|
74 | { |
---|
75 | friend class ShellCommand; |
---|
76 | |
---|
77 | public: |
---|
78 | /** @returns the CommandClassList */ |
---|
79 | static const tList<ShellCommandClass>* getCommandClassList() { return ShellCommandClass::commandClassList; }; |
---|
80 | static bool getCommandListOfClass(const char* className, tList<const char>* stringList); |
---|
81 | static bool getCommandListOfAlias(tList<const char>* aliasList); |
---|
82 | |
---|
83 | static ShellCommandClass* getCommandClass(const char* className); |
---|
84 | static void unregisterAllCommands(); |
---|
85 | |
---|
86 | static void help (const char* className); |
---|
87 | |
---|
88 | private: |
---|
89 | ShellCommandClass(const char* className); |
---|
90 | ~ShellCommandClass(); |
---|
91 | |
---|
92 | static const ShellCommandClass* isRegistered(const char* className); |
---|
93 | static void initCommandClassList(); |
---|
94 | |
---|
95 | private: |
---|
96 | const char* className; //!< The Name of the Class. This should match the ClassName of the Commands Class. |
---|
97 | long classID; //!< The classID of this Class |
---|
98 | tList<ShellCommand>* commandList; //!< A list of Commands from this Class |
---|
99 | static tList<ShellCommandClass>* commandClassList; //!< A list of Classes |
---|
100 | static tList<ShellCommandAlias>* aliasList; //!< An Alias to A Command. (only for classes with one Instance) |
---|
101 | }; |
---|
102 | |
---|
103 | |
---|
104 | //! a baseClass for all possible ShellCommands |
---|
105 | class ShellCommand : public BaseObject |
---|
106 | { |
---|
107 | friend class ShellCommandClass; |
---|
108 | public: |
---|
109 | static bool execute (const char* executionString); |
---|
110 | |
---|
111 | ShellCommand* describe(const char* description); |
---|
112 | ShellCommand* setAlias(const char* alias); |
---|
113 | ShellCommand* defaultValues(unsigned int count, ...); |
---|
114 | |
---|
115 | static ShellCommand* registerCommand(const char* commandName, const char* className, Executor* executor); |
---|
116 | |
---|
117 | static void unregisterCommand(const char* commandName, const char* className); |
---|
118 | |
---|
119 | static void debug(); |
---|
120 | |
---|
121 | protected: |
---|
122 | ShellCommand(const char* commandName, const char* className, unsigned int paramCount, ...); |
---|
123 | ~ShellCommand(); |
---|
124 | |
---|
125 | /** @returns the Type of this Function (either static or objective) */ |
---|
126 | inline ShellCommand_Type getType() { return this->functorType; }; |
---|
127 | |
---|
128 | static bool isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...); |
---|
129 | static const char* paramToString(long parameter); |
---|
130 | |
---|
131 | private: |
---|
132 | /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */ |
---|
133 | virtual void executeCommand (BaseObject* object, const char* parameters) = 0; |
---|
134 | |
---|
135 | protected: |
---|
136 | ShellCommand_Type functorType; //!< The type of Function we've got (either static or objective). |
---|
137 | void* functionPointer; //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL ) |
---|
138 | unsigned int paramCount; //!< the count of parameters. |
---|
139 | unsigned int* parameters; //!< Parameters the function of this Command takes. |
---|
140 | MultiType* defaultValue; //!< Default Values. |
---|
141 | |
---|
142 | private: |
---|
143 | ShellCommandClass* shellClass; //!< A Pointer to the Shell-Class this Command belongs to. |
---|
144 | ShellCommandAlias* alias; //!< An Alias for the Class. |
---|
145 | |
---|
146 | const char* description; //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla"); |
---|
147 | Executor* executor; |
---|
148 | |
---|
149 | }; |
---|
150 | |
---|
151 | //! A Class, that handles aliases. |
---|
152 | class ShellCommandAlias |
---|
153 | { |
---|
154 | friend class ShellCommand; |
---|
155 | public: |
---|
156 | /** @returns the Name of the Alias. */ |
---|
157 | const char* getName() const { return this->aliasName; }; |
---|
158 | /** @returns the Command, this Alias is asociated with */ |
---|
159 | ShellCommand* getCommand() const { return this->command; }; |
---|
160 | |
---|
161 | private: |
---|
162 | /** @param aliasName the name of the Alias @param command the Command, to associate this alias with */ |
---|
163 | ShellCommandAlias(const char* aliasName, ShellCommand* command) { this->aliasName = aliasName; this->command = command; }; |
---|
164 | |
---|
165 | private: |
---|
166 | const char* aliasName; //!< the name of the Alias |
---|
167 | ShellCommand* command; //!< a pointer to the command, this alias executes. |
---|
168 | }; |
---|
169 | |
---|
170 | #endif /* _SHELL_COMMAND_H */ |
---|