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 "substring.h" |
---|
13 | #include "functor_list.h" |
---|
14 | |
---|
15 | #include <stdarg.h> |
---|
16 | |
---|
17 | #define SHELL_COMMAND_MAX_SIZE //!< The maximum size of a Shell Command |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | // FORWARD DECLARATION |
---|
22 | template<class T> class tList; |
---|
23 | |
---|
24 | /** |
---|
25 | * an easy to use Macro to create a Command |
---|
26 | * @param command the name of the command (without "" around the string) |
---|
27 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
28 | * @param function the function to call |
---|
29 | */ |
---|
30 | #define SHELL_COMMAND(command, class, function) \ |
---|
31 | ShellCommandBase* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function) |
---|
32 | |
---|
33 | |
---|
34 | //////////////// |
---|
35 | // BASE CLASS // |
---|
36 | //////////////// |
---|
37 | //! a baseClass for all possible ShellCommands |
---|
38 | class ShellCommandBase : public BaseObject |
---|
39 | { |
---|
40 | public: |
---|
41 | static bool execute (const char* executionString); |
---|
42 | |
---|
43 | ShellCommandBase* describe(const char* description); |
---|
44 | |
---|
45 | /** @returns the CommandList of the Shell */ |
---|
46 | static const tList<ShellCommandBase>* getCommandList() { return ShellCommandBase::commandList; }; |
---|
47 | |
---|
48 | static void unregisterAllCommands(); |
---|
49 | static void unregisterCommand(const char* commandName, const char* className); |
---|
50 | |
---|
51 | static void debug(); |
---|
52 | |
---|
53 | protected: |
---|
54 | ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...); |
---|
55 | ~ShellCommandBase(); |
---|
56 | |
---|
57 | static bool isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...); |
---|
58 | static const char* paramToString(long parameter); |
---|
59 | |
---|
60 | void debugDyn(); |
---|
61 | |
---|
62 | private: |
---|
63 | /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */ |
---|
64 | virtual void executeCommand (BaseObject* object, const char* parameters) = NULL; |
---|
65 | |
---|
66 | protected: |
---|
67 | void* functionPointer; //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL ) |
---|
68 | unsigned int paramCount; //!< the count of parameters. |
---|
69 | unsigned int* parameters; //!< Parameters the function of this Command takes. |
---|
70 | char* defaultStrings[FUNCTOR_MAX_ARGUMENTS];//!< A list of default Strings stored. |
---|
71 | int defaultInts[FUNCTOR_MAX_ARGUMENTS]; //!< A list of default Ints stored. |
---|
72 | float defaultFloats[FUNCTOR_MAX_ARGUMENTS]; //!< A list of default Floats stored. |
---|
73 | bool defaultBools[FUNCTOR_MAX_ARGUMENTS]; //!< A list of default Bools stored. |
---|
74 | |
---|
75 | private: |
---|
76 | // long classID; //!< The ID of the Class associated to this Command |
---|
77 | const char* className; //!< The Name of the Class associated to this Command |
---|
78 | |
---|
79 | const char* description; //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla"); |
---|
80 | |
---|
81 | // STATIC MEMBERS |
---|
82 | static tList<ShellCommandBase>* commandList; //!< A list of availiable commands. |
---|
83 | }; |
---|
84 | |
---|
85 | /////////////////////////////////////////////////// |
---|
86 | /////////////////////////////////////////////////// |
---|
87 | |
---|
88 | /////////////////////// |
---|
89 | // MACRO DEFINITIONS // |
---|
90 | /////////////////////// |
---|
91 | //! where to chek for default BOOL values |
---|
92 | #define l_BOOL_DEFGRAB(i) this->defaultBools[i] |
---|
93 | //! where to chek for default INT values |
---|
94 | #define l_INT_DEFGRAB(i) this->defaultInts[i] |
---|
95 | //! where to chek for default UINT values |
---|
96 | #define l_UINT_DEFGRAB(i) (unsigned int)this->defaultInts[i] |
---|
97 | //! where to chek for default LONG values |
---|
98 | #define l_LONG_DEFGRAB(i) (long)this->defaultInts[i] |
---|
99 | //! where to chek for default FLOAT values |
---|
100 | #define l_FLOAT_DEFGRAB(i) this->defaultFloats[i] |
---|
101 | //! where to chek for default STRING values |
---|
102 | #define l_STRING_DEFGRAB(i) this->defaultStrings[i] |
---|
103 | |
---|
104 | ////////////////////////// |
---|
105 | // COMMAND REGISTRATION // |
---|
106 | ////////////////////////// |
---|
107 | //! registers a command without any parameters |
---|
108 | #define ShellCommandRegister0() \ |
---|
109 | static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)()) \ |
---|
110 | { \ |
---|
111 | if (isRegistered(commandName, className, 0)== true) \ |
---|
112 | return NULL; \ |
---|
113 | return new ShellCommand<T>(commandName, className, function); \ |
---|
114 | } |
---|
115 | |
---|
116 | //! registers a command with 1 parameter |
---|
117 | #define ShellCommandRegister1(t1) \ |
---|
118 | static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \ |
---|
119 | { \ |
---|
120 | if (isRegistered(commandName, className, 1, t1##_PARAM)== true) \ |
---|
121 | return NULL; \ |
---|
122 | return new ShellCommand<T>(commandName, className, function, d1); \ |
---|
123 | } |
---|
124 | |
---|
125 | //! registers a command with 2 parameters |
---|
126 | #define ShellCommandRegister2(t1,t2) \ |
---|
127 | static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT) \ |
---|
128 | { \ |
---|
129 | if (isRegistered(commandName, className, 2, t1##_PARAM, t2##_PARAM)== true) \ |
---|
130 | return NULL; \ |
---|
131 | return new ShellCommand<T>(commandName, className, function, d1, d2); \ |
---|
132 | } |
---|
133 | |
---|
134 | //! registers a command with 3 parameters |
---|
135 | #define ShellCommandRegister3(t1,t2,t3) \ |
---|
136 | static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT, t3##_TYPE d3 = t3##_DEFAULT) \ |
---|
137 | { \ |
---|
138 | if (isRegistered(commandName, className, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM)== true) \ |
---|
139 | return NULL; \ |
---|
140 | return new ShellCommand<T>(commandName, className, function, d1, d2, d3); \ |
---|
141 | } |
---|
142 | |
---|
143 | //! registers a command with 4 parameters |
---|
144 | #define ShellCommandRegister4(t1,t2,t3,t4) \ |
---|
145 | static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT, t3##_TYPE d3 = t3##_DEFAULT, t4##_TYPE d4 = t4##_DEFAULT) \ |
---|
146 | { \ |
---|
147 | if (isRegistered(commandName, className, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM)== true) \ |
---|
148 | return NULL; \ |
---|
149 | return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4); \ |
---|
150 | } |
---|
151 | |
---|
152 | //! registers a command with 5 parameters |
---|
153 | #define ShellCommandRegister5(t1,t2,t3,t4,t5) \ |
---|
154 | static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT, t3##_TYPE d3 = t3##_DEFAULT, t4##_TYPE d4 = t4##_DEFAULT, t5##_TYPE d5 = t5##_DEFAULT) \ |
---|
155 | { \ |
---|
156 | if (isRegistered(commandName, className, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM)== true) \ |
---|
157 | return NULL; \ |
---|
158 | return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4, d5); \ |
---|
159 | } |
---|
160 | |
---|
161 | ////////////////// |
---|
162 | // CONSTRUCTORS // |
---|
163 | ///////////////// |
---|
164 | //! creates a command that takes no parameters |
---|
165 | #define ShellCommandConstructor0() \ |
---|
166 | void (T::*functionPointer_0)(); \ |
---|
167 | ShellCommand(const char* commandName, const char* className, void (T::*function)()) \ |
---|
168 | : ShellCommandBase(commandName, className, 0) \ |
---|
169 | { \ |
---|
170 | this->functionPointer_0 = function; \ |
---|
171 | } |
---|
172 | |
---|
173 | //! creates a command that takes one parameter |
---|
174 | #define ShellCommandConstructor1(t1) \ |
---|
175 | void (T::*functionPointer_1_##t1)(t1##_TYPE); \ |
---|
176 | ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE), t1##_TYPE d1) \ |
---|
177 | : ShellCommandBase(commandName, className, 1, t1##_PARAM, d1) \ |
---|
178 | { \ |
---|
179 | this->functionPointer_1_##t1 = function; \ |
---|
180 | } |
---|
181 | |
---|
182 | //! creates a command that takes two parameters |
---|
183 | #define ShellCommandConstructor2(t1,t2) \ |
---|
184 | void (T::*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE); \ |
---|
185 | ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1, t2##_TYPE d2) \ |
---|
186 | : ShellCommandBase(commandName, className, 2, t1##_PARAM, d1, t2##_PARAM, d2) \ |
---|
187 | { \ |
---|
188 | this->functionPointer_2_##t1##_##t2 = function; \ |
---|
189 | } |
---|
190 | |
---|
191 | //! creates a command that takes three parameter |
---|
192 | #define ShellCommandConstructor3(t1,t2,t3) \ |
---|
193 | void (T::*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE); \ |
---|
194 | ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3) \ |
---|
195 | : ShellCommandBase(commandName, className, 3, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3) \ |
---|
196 | { \ |
---|
197 | this->functionPointer_3_##t1##_##t2##_##t3 = function; \ |
---|
198 | } |
---|
199 | |
---|
200 | //! creates a command that takes four parameter |
---|
201 | #define ShellCommandConstructor4(t1,t2,t3,t4) \ |
---|
202 | void (T::*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE); \ |
---|
203 | ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3, t4##_TYPE d4) \ |
---|
204 | : ShellCommandBase(commandName, className, 4, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4) \ |
---|
205 | { \ |
---|
206 | this->functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \ |
---|
207 | } |
---|
208 | |
---|
209 | //! creates a command that takes five parameter |
---|
210 | #define ShellCommandConstructor5(t1,t2,t3,t4,t5) \ |
---|
211 | void (T::*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \ |
---|
212 | ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3, t4##_TYPE d4, t5##_TYPE d5) \ |
---|
213 | : ShellCommandBase(commandName, className, 5, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4, t5##_PARAM, d5) \ |
---|
214 | { \ |
---|
215 | this->functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \ |
---|
216 | } |
---|
217 | |
---|
218 | /////////////// |
---|
219 | // EXECUTION // |
---|
220 | /////////////// |
---|
221 | //! execute-macro for functions with no parameters |
---|
222 | #define ShellCommandExecute0() \ |
---|
223 | if (this->paramCount == 0) \ |
---|
224 | (dynamic_cast<T*>(object)->*functionPointer_0)() |
---|
225 | |
---|
226 | //! execute-macro for functions with one parameter |
---|
227 | #define ShellCommandExecute1(t1) \ |
---|
228 | else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \ |
---|
229 | (dynamic_cast<T*>(object)->*functionPointer_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0))) |
---|
230 | |
---|
231 | //! execute-macro for functions with two parameters |
---|
232 | #define ShellCommandExecute2(t1,t2) \ |
---|
233 | else if (this->paramCount == 2 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM) \ |
---|
234 | (dynamic_cast<T*>(object)->*functionPointer_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1))) |
---|
235 | |
---|
236 | //! execute-macro for functions with three parameters |
---|
237 | #define ShellCommandExecute3(t1,t2,t3) \ |
---|
238 | else if (this->paramCount == 3 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM) \ |
---|
239 | (dynamic_cast<T*>(object)->*functionPointer_3_##t1##_##t2##_##t3)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)), t3##_FUNC(sub.getString(2), t3##_DEFGRAB(2))) |
---|
240 | |
---|
241 | //! execute-macro for functions with four parameters |
---|
242 | #define ShellCommandExecute4(t1,t2,t3,t4) \ |
---|
243 | else if (this->paramCount == 4 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM && this->parameters[3] == t4##_PARAM) \ |
---|
244 | (dynamic_cast<T*>(object)->*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)), t3##_FUNC(sub.getString(2), t3##_DEFGRAB(2)), t4##_FUNC(sub.getString(3), t4##_DEFGRAB(3))) |
---|
245 | |
---|
246 | //! execute-macro for functions with five parameters |
---|
247 | #define ShellCommandExecute5(t1,t2,t3,t4,t5) \ |
---|
248 | else if (this->paramCount == 5 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM && this->parameters[3] == t4##_PARAM && this->parameters[4] == t5##_PARAM) \ |
---|
249 | (dynamic_cast<T*>(object)->*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)), t3##_FUNC(sub.getString(2), t3##_DEFGRAB(2)), t4##_FUNC(sub.getString(3), t4##_DEFGRAB(3)), t5##_FUNC(sub.getString(4), t5##_DEFGRAB(4))) |
---|
250 | |
---|
251 | |
---|
252 | //! keeps information about a ShellCommand |
---|
253 | template<class T> class ShellCommand : public ShellCommandBase |
---|
254 | { |
---|
255 | public: |
---|
256 | |
---|
257 | #ifdef FUNCTOR_LIST |
---|
258 | #undef FUNCTOR_LIST |
---|
259 | #endif |
---|
260 | |
---|
261 | //! FUNCTOR_LIST is the List of command-registerers |
---|
262 | #define FUNCTOR_LIST(x) ShellCommandRegister ## x |
---|
263 | #include "functor_list.h" |
---|
264 | #undef FUNCTOR_LIST |
---|
265 | |
---|
266 | |
---|
267 | private: |
---|
268 | //! FUNCTOR_LIST is the List of CommandConstructors |
---|
269 | #define FUNCTOR_LIST(x) ShellCommandConstructor ## x |
---|
270 | #include "functor_list.h" |
---|
271 | #undef FUNCTOR_LIST |
---|
272 | |
---|
273 | virtual void executeCommand (BaseObject* object, const char* parameters) |
---|
274 | { |
---|
275 | // if (parameters != NULL) |
---|
276 | SubString sub(parameters); |
---|
277 | //! FUNCTOR_LIST is the List of Executive Functions |
---|
278 | #define FUNCTOR_LIST(x) ShellCommandExecute ## x |
---|
279 | #include "functor_list.h" |
---|
280 | #undef FUNCTOR_LIST |
---|
281 | } |
---|
282 | }; |
---|
283 | |
---|
284 | #endif /* _SHELL_COMMAND_H */ |
---|