Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5162 was 5162, checked in by bensch, 20 years ago

orxonox/trunk: more elaborate and MUCH easier through Macro-definition

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