Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: default value can now be supplied as aditional arg of the SHELL_COMMAND-macro

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