Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: commands now get unregistered when leaving orxonox

File size: 11.1 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
18
19
20
21// FORWARD DECLARATION
22template<class T> class tList;
23
24#define SHELL_COMMAND(command, class, function) \
25        ShellCommandBase* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function)
26
27
28////////////////
29// BASE CLASS //
30////////////////
31//! a baseClass for all possible ShellCommands
32class ShellCommandBase : public BaseObject
33{
34  public:
35    static bool execute (const char* executionString);
36
37    ShellCommandBase* describe(const char* description);
38
39    static const tList<ShellCommandBase>* getCommandList() { return ShellCommandBase::commandList; };
40
41    static void unregisterAllCommands();
42    static void unregisterCommand(const char* commandNaame, const char* className);
43
44    static void debug();
45
46  protected:
47    ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...);
48    ~ShellCommandBase();
49
50    static bool isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...);
51    static const char* paramToString(long parameter);
52
53    void debugDyn();
54
55  private:
56    virtual void executeCommand (BaseObject* object, const char* parameters) = NULL;
57
58  protected:
59    void*                            functionPointer;                      //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL )
60    unsigned int                     paramCount;                           //!< the count of parameters
61    unsigned int*                    parameters;                           //!< Parameters
62    char*                            defaultStrings[FUNCTOR_MAX_ARGUMENTS];
63    int                              defaultInts[FUNCTOR_MAX_ARGUMENTS];
64    float                            defaultFloats[FUNCTOR_MAX_ARGUMENTS];
65    bool                             defaultBools[FUNCTOR_MAX_ARGUMENTS];
66
67  private:
68//    long                             classID;             //!< The ID of the Class associated to this Command
69    const char*                      className;                            //!< The Name of the Class associated to this Command
70
71    const char*                      description;
72
73    // STATIC MEMBERS
74    static tList<ShellCommandBase>*  commandList;                          //!< A list of availiable commands.
75};
76
77///////////////////////////////////////////////////
78///////////////////////////////////////////////////
79
80///////////////////////
81// MACRO DEFINITIONS //
82///////////////////////
83#define   l_BOOL_DEFGRAB(i)         this->defaultBools[i]
84#define   l_INT_DEFGRAB(i)          this->defaultInts[i]
85#define   l_UINT_DEFGRAB(i)         (unsigned int)this->defaultInts[i]
86#define   l_LONG_DEFGRAB(i)         (long)this->defaultInts[i]
87#define   l_FLOAT_DEFGRAB(i)        this->defaultFloats[i]
88#define   l_STRING_DEFGRAB(i)       this->defaultStrings[i]
89
90//////////////////////////
91// COMMAND REGISTRATION //
92//////////////////////////
93// NO ARGUMENTS
94#define ShellCommandRegister0() \
95  static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)()) \
96  { \
97    if (isRegistered(commandName, className, 0)== true) \
98      return NULL; \
99    return new ShellCommand<T>(commandName, className, function); \
100  }
101
102#define ShellCommandRegister1(t1) \
103  static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \
104  { \
105    if (isRegistered(commandName, className, 1, t1##_PARAM)== true) \
106      return NULL; \
107    return new ShellCommand<T>(commandName, className, function, d1); \
108  }
109
110#define ShellCommandRegister2(t1,t2) \
111  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) \
112  { \
113    if (isRegistered(commandName, className, 2, t1##_PARAM, t2##_PARAM)== true) \
114      return NULL; \
115    return new ShellCommand<T>(commandName, className, function, d1, d2); \
116  }
117
118#define ShellCommandRegister3(t1,t2,t3) \
119  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) \
120  { \
121    if (isRegistered(commandName, className, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM)== true) \
122      return NULL; \
123    return new ShellCommand<T>(commandName, className, function, d1, d2, d3); \
124  }
125
126#define ShellCommandRegister4(t1,t2,t3,t4) \
127  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) \
128  { \
129    if (isRegistered(commandName, className, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM)== true) \
130      return NULL; \
131    return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4); \
132  }
133
134#define ShellCommandRegister5(t1,t2,t3,t4,t5) \
135  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) \
136  { \
137    if (isRegistered(commandName, className, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM)== true) \
138      return NULL; \
139    return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4, d5); \
140  }
141
142//////////////////
143// CONSTRUCTORS //
144/////////////////
145#define ShellCommandConstructor0() \
146  void (T::*functionPointer_0)(); \
147  ShellCommand(const char* commandName, const char* className, void (T::*function)()) \
148  : ShellCommandBase(commandName, className, 0) \
149  { \
150    this->functionPointer_0 = function; \
151  }
152
153#define ShellCommandConstructor1(t1) \
154  void (T::*functionPointer_1_##t1)(t1##_TYPE); \
155  ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE), t1##_TYPE d1) \
156  : ShellCommandBase(commandName, className, 1, t1##_PARAM, d1) \
157  { \
158    this->functionPointer_1_##t1 = function; \
159  }
160
161#define ShellCommandConstructor2(t1,t2) \
162  void (T::*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE); \
163  ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1, t2##_TYPE d2) \
164  : ShellCommandBase(commandName, className, 2, t1##_PARAM, d1, t2##_PARAM, d2) \
165  { \
166    this->functionPointer_2_##t1##_##t2 = function; \
167  }
168
169#define ShellCommandConstructor3(t1,t2,t3) \
170  void (T::*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE); \
171  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) \
172  : ShellCommandBase(commandName, className, 3, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3) \
173  { \
174    this->functionPointer_3_##t1##_##t2##_##t3 = function; \
175  }
176
177#define ShellCommandConstructor4(t1,t2,t3,t4) \
178  void (T::*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE); \
179  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) \
180  : ShellCommandBase(commandName, className, 4, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4) \
181  { \
182    this->functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
183  }
184
185#define ShellCommandConstructor5(t1,t2,t3,t4,t5) \
186  void (T::*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
187  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) \
188  : ShellCommandBase(commandName, className, 5, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4, t5##_PARAM, d5) \
189  { \
190    this->functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
191  }
192
193///////////////
194// EXECUTION //
195///////////////
196#define ShellCommandExecute0() \
197  if (this->paramCount == 0) \
198    (dynamic_cast<T*>(object)->*functionPointer_0)()
199
200#define ShellCommandExecute1(t1) \
201   else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \
202    (dynamic_cast<T*>(object)->*functionPointer_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0)))
203
204#define ShellCommandExecute2(t1,t2) \
205   else if (this->paramCount == 2 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM) \
206    (dynamic_cast<T*>(object)->*functionPointer_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))
207
208#define ShellCommandExecute3(t1,t2,t3) \
209   else if (this->paramCount == 3 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM) \
210    (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)))
211
212#define ShellCommandExecute4(t1,t2,t3,t4) \
213   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) \
214    (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)))
215
216#define ShellCommandExecute5(t1,t2,t3,t4,t5) \
217   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) \
218    (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)))
219
220
221//! keeps information about a ShellCommand
222template<class T> class ShellCommand : public ShellCommandBase
223{
224  public:
225
226#ifdef FUNCTOR_LIST
227#undef FUNCTOR_LIST
228#endif
229
230#define FUNCTOR_LIST(x) ShellCommandRegister ## x
231#include "functor_list.h"
232#undef FUNCTOR_LIST
233
234
235  private:
236#define FUNCTOR_LIST(x) ShellCommandConstructor ## x
237#include "functor_list.h"
238#undef FUNCTOR_LIST
239
240    virtual void executeCommand (BaseObject* object, const char* parameters)
241    {
242//      if (parameters != NULL)
243      SubString sub(parameters);
244#define FUNCTOR_LIST(x) ShellCommandExecute ## x
245#include "functor_list.h"
246#undef FUNCTOR_LIST
247    }
248};
249
250#endif /* _SHELL_COMMAND_H */
Note: See TracBrowser for help on using the repository browser.