Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/2d-recalc/src/lib/shell/shell_command.h @ 5381

Last change on this file since 5381 was 5329, checked in by bensch, 19 years ago

orxonox/trunk: executing static commands work

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