Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: match on Singleton dynamically

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