Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better method to register Shell-Commands (now via the Factory-algorithm proposed)

BUT!! NOW ONLY SINGLETON IS SUPPORTED → fixing!!

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