Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: alias-completion works too now :)

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