Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/shell/shell_command.h @ 5729

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

orxonox/trunk: ShellCommand availiable again, in orxonox-mode, but unavailiable in subproject Network

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