Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: almost working

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