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