Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_command.h @ 5551

Last change on this file since 5551 was 5551, checked in by bensch, 18 years ago

orxonox/trunk: ShellCommands use less memory due to the use of unions.

File size: 19.6 KB
Line 
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
22template<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 * MEANING:
31 *  ShellCommandBase* someUniqueVarName =
32 *       ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall);
33 *
34 * In the Shell you would call this Command using:
35 * $ ClassName [ObjectName] commandNameInShell [parameters]
36 */
37#define SHELL_COMMAND(command, class, function) \
38        ShellCommandBase* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function)
39/**
40 * an easy to use Macro to create a Command
41 * @param command the name of the command (without "" around the string)
42 * @param class the name of the class to apply this command to (without the "" around the string)
43 * @param function the function to call
44 *
45 * MEANING:
46 *  ShellCommandBase* someUniqueVarName =
47 *       ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall);
48 *
49 * In the Shell you would call this Command using:
50 * $ ClassName [ObjectName] commandNameInShell [parameters]
51 */
52#define SHELL_COMMAND_STATIC(command, class, function) \
53                         ShellCommandBase* shell_command_##class##_##command = ShellCommandStatic<class>::registerCommand(#command, #class, function)
54
55
56//! an enumerator for the definition of the Type.
57typedef enum {
58  ShellCommand_Objective = 1,
59  ShellCommand_Static    = 2,
60} ShellCommand_Type;
61
62////////////////
63// BASE CLASS //
64////////////////
65class ShellCommandBase;
66class ShellCommandAlias;
67
68//! A class to hold all Classes that have (once) registered Commands.
69class ShellCommandClass : public BaseObject
70{
71  friend class ShellCommandBase;
72
73  public:
74    /** @returns the CommandClassList */
75    static const tList<ShellCommandClass>* getCommandClassList() { return ShellCommandClass::commandClassList; };
76    static bool getCommandListOfClass(const char* className, tList<const char>* stringList);
77    static bool getCommandListOfAlias(tList<const char>* aliasList);
78
79    static ShellCommandClass* getCommandClass(const char* className);
80    static void unregisterAllCommands();
81
82    static void help (const char* className);
83
84  private:
85    ShellCommandClass(const char* className);
86    ~ShellCommandClass();
87
88    static const ShellCommandClass* isRegistered(const char* className);
89    static void initCommandClassList();
90
91  private:
92    const char*                      className;                 //!< The Name of the Class. This should match the ClassName of the Commands Class.
93    long                             classID;                   //!< The classID of this Class
94    tList<ShellCommandBase>*         commandList;               //!< A list of Commands from this Class
95    static tList<ShellCommandClass>* commandClassList;          //!< A list of Classes
96    static tList<ShellCommandAlias>* aliasList;                 //!< An Alias to A Command. (only for classes with one Instance)
97};
98
99
100//! a baseClass for all possible ShellCommands
101class ShellCommandBase : public BaseObject
102{
103  friend class ShellCommandClass;
104  public:
105    static bool execute (const char* executionString);
106
107    ShellCommandBase* describe(const char* description);
108    ShellCommandBase* setAlias(const char* alias);
109    ShellCommandBase* defaultValues(unsigned int count, ...);
110
111    /** @returns the CommandList of the Shell */
112    static void unregisterCommand(const char* commandName, const char* className);
113
114    static void debug();
115
116  protected:
117    ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...);
118    ~ShellCommandBase();
119
120    /** @returns the Type of this Function (either static or objective) */
121    inline ShellCommand_Type getType() { return this->functorType; };
122
123    static bool isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...);
124    static const char* paramToString(long parameter);
125
126  private:
127    /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */
128    virtual void executeCommand (BaseObject* object, const char* parameters) = 0;
129
130  protected:
131    ShellCommand_Type                functorType;                          //!< The type of Function we've got (either static or objective).
132    void*                            functionPointer;                      //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL )
133    unsigned int                     paramCount;                           //!< the count of parameters.
134    unsigned int*                    parameters;                           //!< Parameters the function of this Command takes.
135    char*                            defaultStrings[FUNCTOR_MAX_ARGUMENTS];//!< A list of default Strings stored.
136    int                              defaultInts[FUNCTOR_MAX_ARGUMENTS];   //!< A list of default Ints stored.
137    float                            defaultFloats[FUNCTOR_MAX_ARGUMENTS]; //!< A list of default Floats stored.
138    bool                             defaultBools[FUNCTOR_MAX_ARGUMENTS];  //!< A list of default Bools stored.
139
140
141  private:
142    ShellCommandClass*               shellClass;                           //!< A Pointer to the Shell-Class this Command belongs to.
143    ShellCommandAlias*               alias;                                //!< An Alias for the Class.
144
145    const char*                      description;                          //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla");
146};
147
148///////////////////////////////////////////////////
149///////////////////////////////////////////////////
150
151/////////////////////////////////
152// MACRO DEFINITION EXTENSIONS //
153/////////////////////////////////
154//! where to chek for default BOOL values
155#define   l_BOOL_DEFGRAB(i)         this->defaultBools[i]
156//! where to chek for default INT values
157#define   l_INT_DEFGRAB(i)          this->defaultInts[i]
158//! where to chek for default UINT values
159#define   l_UINT_DEFGRAB(i)         (unsigned int)this->defaultInts[i]
160//! where to chek for default LONG values
161#define   l_LONG_DEFGRAB(i)         (long)this->defaultInts[i]
162//! where to chek for default FLOAT values
163#define   l_FLOAT_DEFGRAB(i)        this->defaultFloats[i]
164//! where to chek for default STRING values
165#define   l_STRING_DEFGRAB(i)       this->defaultStrings[i]
166
167//////////////////////////
168// COMMAND REGISTRATION //
169//////////////////////////
170// SHELLCOMMAND can be redefined as ShellCommand or ShellCommandStatic
171// SHELLCOMMANDEXECUTER can be redefined too.
172// SHELLCOMMANDINCLASS
173// SHELLCOMMANDTYPE
174//! registers a command without any parameters
175#define ShellCommandRegister0() \
176  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)()) \
177  { \
178    if (isRegistered(commandName, className, 0)== true) \
179      return NULL; \
180    return new SHELLCOMMAND<T>(commandName, className, function); \
181  }
182
183//! registers a command with 1 parameter
184#define ShellCommandRegister1(t1) \
185  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \
186  { \
187    if (isRegistered(commandName, className, 1, t1##_PARAM)== true) \
188      return NULL; \
189    return new SHELLCOMMAND<T>(commandName, className, function, d1); \
190  }
191
192//! registers a command with 2 parameters
193#define ShellCommandRegister2(t1,t2) \
194  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT) \
195  { \
196    if (isRegistered(commandName, className, 2, t1##_PARAM, t2##_PARAM)== true) \
197      return NULL; \
198    return new SHELLCOMMAND<T>(commandName, className, function, d1, d2); \
199  }
200
201//! registers a command with 3 parameters
202#define ShellCommandRegister3(t1,t2,t3) \
203  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT, t3##_TYPE d3 = t3##_DEFAULT) \
204  { \
205    if (isRegistered(commandName, className, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM)== true) \
206      return NULL; \
207    return new SHELLCOMMAND<T>(commandName, className, function, d1, d2, d3); \
208  }
209
210//! registers a command with 4 parameters
211#define ShellCommandRegister4(t1,t2,t3,t4) \
212  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*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) \
213  { \
214    if (isRegistered(commandName, className, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM)== true) \
215      return NULL; \
216    return new SHELLCOMMAND<T>(commandName, className, function, d1, d2, d3, d4); \
217  }
218
219//! registers a command with 5 parameters
220#define ShellCommandRegister5(t1,t2,t3,t4,t5) \
221  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*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) \
222  { \
223    if (isRegistered(commandName, className, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM)== true) \
224      return NULL; \
225    return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4, d5); \
226  }
227
228///////////////////////
229// FUNCTION POINTERS //
230///////////////////////
231#define ShellCommandFunctionPoiter0() \
232  void SHELLCOMMANDINCLASS(*functionPointer_0)();
233
234#define ShellCommandFunctionPoiter1(t1) \
235  void SHELLCOMMANDINCLASS(*functionPointer_1_##t1)(t1##_TYPE);
236
237#define ShellCommandFunctionPoiter2(t1, t2) \
238  void SHELLCOMMANDINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE);
239
240
241#define ShellCommandFunctionPoiter3(t1, t2, t3) \
242  void SHELLCOMMANDINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE);
243
244#define ShellCommandFunctionPoiter4(t1, t2, t3, t4) \
245  void SHELLCOMMANDINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE);
246
247
248#define ShellCommandFunctionPoiter5(t1, t2, t3, t4, t5) \
249  void SHELLCOMMANDINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
250
251
252//////////////////
253// CONSTRUCTORS //
254/////////////////
255//! creates a command that takes no parameters
256#define ShellCommandConstructor0() \
257  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)()) \
258  : ShellCommandBase(commandName, className, 0) \
259  { \
260    this->functorType = SHELLCOMMANDTYPE; \
261    this->fp.functionPointer_0 = function; \
262  }
263
264//! creates a command that takes one parameter
265#define ShellCommandConstructor1(t1) \
266  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE), t1##_TYPE d1) \
267  : ShellCommandBase(commandName, className, 1, t1##_PARAM, d1) \
268  { \
269    this->functorType = SHELLCOMMANDTYPE; \
270    this->fp.functionPointer_1_##t1 = function; \
271  }
272
273//! creates a command that takes two parameters
274#define ShellCommandConstructor2(t1,t2) \
275  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1, t2##_TYPE d2) \
276  : ShellCommandBase(commandName, className, 2, t1##_PARAM, d1, t2##_PARAM, d2) \
277  { \
278    this->functorType = SHELLCOMMANDTYPE; \
279    this->fp.functionPointer_2_##t1##_##t2 = function; \
280  }
281
282//! creates a command that takes three parameter
283#define ShellCommandConstructor3(t1,t2,t3) \
284  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3) \
285  : ShellCommandBase(commandName, className, 3, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3) \
286  { \
287    this->functorType = SHELLCOMMANDTYPE; \
288    this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \
289  }
290
291//! creates a command that takes four parameter
292#define ShellCommandConstructor4(t1,t2,t3,t4) \
293  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3, t4##_TYPE d4) \
294  : ShellCommandBase(commandName, className, 4, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4) \
295  { \
296    this->functorType = SHELLCOMMANDTYPE; \
297    this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
298  }
299
300//! creates a command that takes five parameter
301#define ShellCommandConstructor5(t1,t2,t3,t4,t5) \
302  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*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) \
303  : ShellCommandBase(commandName, className, 5, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4, t5##_PARAM, d5) \
304  { \
305    this->functorType = SHELLCOMMANDTYPE; \
306    this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
307  }
308
309///////////////
310// EXECUTION //
311///////////////
312//! execute-macro for functions with no parameters
313#define ShellCommandExecute0() \
314  if (this->paramCount == 0) \
315    SHELLCOMMANDEXECUTER(_0)()
316
317//! execute-macro for functions with one parameter
318#define ShellCommandExecute1(t1) \
319   else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \
320    SHELLCOMMANDEXECUTER(_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0)))
321
322//! execute-macro for functions with two parameters
323#define ShellCommandExecute2(t1,t2) \
324   else if (this->paramCount == 2 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM) \
325    SHELLCOMMANDEXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))
326
327//! execute-macro for functions with three parameters
328#define ShellCommandExecute3(t1,t2,t3) \
329   else if (this->paramCount == 3 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM) \
330    SHELLCOMMANDEXECUTER(_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)))
331
332//! execute-macro for functions with four parameters
333#define ShellCommandExecute4(t1,t2,t3,t4) \
334   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) \
335    SHELLCOMMANDEXECUTER(_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)))
336
337//! execute-macro for functions with five parameters
338#define ShellCommandExecute5(t1,t2,t3,t4,t5) \
339   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) \
340    SHELLCOMMANDEXECUTER(_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)))
341
342
343//! keeps information about a ShellCommand
344template<class T> class ShellCommand : public ShellCommandBase
345{
346  public:
347#ifdef FUNCTOR_LIST
348#undef FUNCTOR_LIST
349#endif
350#ifdef SHELLCOMMAND
351#undef SHELLCOMMAND
352#endif
353#define SHELLCOMMAND                       ShellCommand
354#ifdef SHELLCOMMANDEXECUTER
355#undef SHELLCOMMANDEXECUTER
356#endif
357#define SHELLCOMMANDEXECUTER(nameExt)      (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))
358#ifdef SHELLCOMMANDINCLASS
359#undef SHELLCOMMANDINCLASS
360#endif
361#define SHELLCOMMANDINCLASS(FUNCTION)      (T::FUNCTION)
362#ifdef SHELLCOMMANDTYPE
363#undef SHELLCOMMANDTYPE
364#endif
365#define SHELLCOMMANDTYPE                   ShellCommand_Objective
366//! FUNCTOR_LIST is the List of command-registerers
367#define FUNCTOR_LIST(x) ShellCommandRegister ## x
368#include "functor_list.h"
369#undef FUNCTOR_LIST
370
371
372  private:
373//! FUNCTOR_LIST is the List of FunctionPointers
374    union FunctionPointers {
375#define FUNCTOR_LIST(x) ShellCommandFunctionPoiter ## x
376#include "functor_list.h"
377#undef FUNCTOR_LIST
378    } fp;
379
380//! FUNCTOR_LIST is the List of CommandConstructors
381#define FUNCTOR_LIST(x) ShellCommandConstructor ## x
382#include "functor_list.h"
383#undef FUNCTOR_LIST
384
385    virtual void executeCommand (BaseObject* object, const char* parameters)
386    {
387      SubString sub(parameters, true);
388//! FUNCTOR_LIST is the List of Executive Functions
389#define FUNCTOR_LIST(x) ShellCommandExecute ## x
390#include "functor_list.h"
391#undef FUNCTOR_LIST
392    }
393};
394
395//! keeps information about a ShellCommand, that points to a Static Function
396template<class T> class ShellCommandStatic : public ShellCommandBase
397{
398  public:
399#ifdef FUNCTOR_LIST
400#undef FUNCTOR_LIST
401#endif
402#ifdef SHELLCOMMAND
403#undef SHELLCOMMAND
404#endif
405#define SHELLCOMMAND                      ShellCommandStatic
406#ifdef SHELLCOMMANDEXECUTER
407#undef SHELLCOMMANDEXECUTER
408#endif
409#define SHELLCOMMANDEXECUTER(nameExt)     fp.functionPointer##nameExt
410#ifdef SHELLCOMMANDINCLASS
411#undef SHELLCOMMANDINCLASS
412#endif
413#define SHELLCOMMANDINCLASS(FUNCTION)     (FUNCTION)
414#ifdef SHELLCOMMANDTYPE
415#undef SHELLCOMMANDTYPE
416#endif
417#define SHELLCOMMANDTYPE                   ShellCommand_Static
418
419//! FUNCTOR_LIST is the List of command-registerers
420#define FUNCTOR_LIST(x) ShellCommandRegister ## x
421#include "functor_list.h"
422#undef FUNCTOR_LIST
423
424  private:
425//! FUNCTOR_LIST is the List of FunctionPointers
426    union FunctionPointers {
427#define FUNCTOR_LIST(x) ShellCommandFunctionPoiter ## x
428#include "functor_list.h"
429#undef FUNCTOR_LIST
430    } fp;
431
432//! FUNCTOR_LIST is the List of CommandConstructors
433#define FUNCTOR_LIST(x) ShellCommandConstructor ## x
434#include "functor_list.h"
435#undef FUNCTOR_LIST
436
437    virtual void executeCommand (BaseObject* object, const char* parameters)
438    {
439  SubString sub(parameters, true);
440//! FUNCTOR_LIST is the List of Executive Functions
441#define FUNCTOR_LIST(x) ShellCommandExecute ## x
442#include "functor_list.h"
443#undef FUNCTOR_LIST
444    }
445};
446
447//! A Class, that handles aliases.
448class ShellCommandAlias
449{
450  friend class ShellCommandBase;
451  public:
452    /** @returns the Name of the Alias. */
453    const char* getName() const { return this->aliasName; };
454    /** @returns the Command, this Alias is asociated with */
455    ShellCommandBase* getCommand() const { return this->command; };
456
457  private:
458    /** @param aliasName the name of the Alias @param command the Command, to associate this alias with */
459    ShellCommandAlias(const char* aliasName, ShellCommandBase* command) { this->aliasName = aliasName; this->command = command; };
460
461  private:
462    const char*         aliasName;       //!< the name of the Alias
463    ShellCommandBase*   command;         //!< a pointer to the command, this alias executes.
464};
465
466#endif /* _SHELL_COMMAND_H */
Note: See TracBrowser for help on using the repository browser.