Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minor function, to retrieve a List of Commands from a ClassList

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