Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: doxygen-tags

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