Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ability to describe the presented options :)

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