Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellInput is now almost perfectly extern.
ShellCompletion taken out.
Working again :)

File size: 14.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      //!< The maximum size of a Shell Command
18
19
20
21// FORWARD DECLARATION
22template<class T> class tList;
23
24/**
25 * an easy to use Macro to create a Command
26 * @param command the name of the command (without "" around the string)
27 * @param class the name of the class to apply this command to (without the "" around the string)
28 * @param function the function to call
29 *
30 * MEANING:
31 *  ShellCommandBase* someUniqueVarName =
32 *       ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall);
33 *
34 * In the Shell you would call this Command using:
35 * $ ClassName [ObjectName] commandNameInShell [parameters]
36 */
37#define SHELL_COMMAND(command, class, function) \
38        ShellCommandBase* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function)
39
40
41////////////////
42// BASE CLASS //
43////////////////
44class ShellCommandBase;
45
46//! A class to hold all Classes that have (once) registered Commands.
47class ShellCommandClass : public BaseObject
48{
49  friend class ShellCommandBase;
50
51  public:
52    static const tList<ShellCommandClass>* getCommandClassList() { return ShellCommandClass::commandClassList; };
53    static ShellCommandClass* getCommandClass(const char* className);
54    static void unregisterAllCommands();
55
56  private:
57    ShellCommandClass(const char* className);
58    ~ShellCommandClass();
59
60    static const ShellCommandClass* isRegistered(const char* className);
61    static void initCommandClassList();
62
63  private:
64    const char*                      className;                 //!< The Name of the Class. This should match the ClassName of the Commands Class.
65    long                             classID;                   //!< The classID of this Class
66    tList<ShellCommandBase>*         commandList;               //!< A list of Commands from this Class
67    static tList<ShellCommandClass>* commandClassList;          //!< A list of Classes
68};
69
70
71//! a baseClass for all possible ShellCommands
72class ShellCommandBase : public BaseObject
73{
74  friend class ShellCommandClass;
75  public:
76    static bool execute (const char* executionString);
77
78    ShellCommandBase* describe(const char* description);
79
80    /** @returns the CommandList of the Shell */
81
82    static void unregisterCommand(const char* commandName, const char* className);
83
84    static void debug();
85
86  protected:
87    ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...);
88    ~ShellCommandBase();
89
90    static bool isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...);
91    static const char* paramToString(long parameter);
92
93    void debugDyn();
94
95  private:
96    /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */
97    virtual void executeCommand (BaseObject* object, const char* parameters) = NULL;
98
99  protected:
100    void*                            functionPointer;                      //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL )
101    unsigned int                     paramCount;                           //!< the count of parameters.
102    unsigned int*                    parameters;                           //!< Parameters the function of this Command takes.
103    char*                            defaultStrings[FUNCTOR_MAX_ARGUMENTS];//!< A list of default Strings stored.
104    int                              defaultInts[FUNCTOR_MAX_ARGUMENTS];   //!< A list of default Ints stored.
105    float                            defaultFloats[FUNCTOR_MAX_ARGUMENTS]; //!< A list of default Floats stored.
106    bool                             defaultBools[FUNCTOR_MAX_ARGUMENTS];  //!< A list of default Bools stored.
107
108  private:
109    ShellCommandClass*               shellClass;                           //!< A Pointer to the Shell-Class this Command belongs to.
110
111    const char*                      description;                          //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla");
112};
113
114///////////////////////////////////////////////////
115///////////////////////////////////////////////////
116
117///////////////////////
118// MACRO DEFINITIONS //
119///////////////////////
120//! where to chek for default BOOL values
121#define   l_BOOL_DEFGRAB(i)         this->defaultBools[i]
122//! where to chek for default INT values
123#define   l_INT_DEFGRAB(i)          this->defaultInts[i]
124//! where to chek for default UINT values
125#define   l_UINT_DEFGRAB(i)         (unsigned int)this->defaultInts[i]
126//! where to chek for default LONG values
127#define   l_LONG_DEFGRAB(i)         (long)this->defaultInts[i]
128//! where to chek for default FLOAT values
129#define   l_FLOAT_DEFGRAB(i)        this->defaultFloats[i]
130//! where to chek for default STRING values
131#define   l_STRING_DEFGRAB(i)       this->defaultStrings[i]
132
133//////////////////////////
134// COMMAND REGISTRATION //
135//////////////////////////
136//! registers a command without any parameters
137#define ShellCommandRegister0() \
138  static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)()) \
139  { \
140    if (isRegistered(commandName, className, 0)== true) \
141      return NULL; \
142    return new ShellCommand<T>(commandName, className, function); \
143  }
144
145//! registers a command with 1 parameter
146#define ShellCommandRegister1(t1) \
147  static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \
148  { \
149    if (isRegistered(commandName, className, 1, t1##_PARAM)== true) \
150      return NULL; \
151    return new ShellCommand<T>(commandName, className, function, d1); \
152  }
153
154//! registers a command with 2 parameters
155#define ShellCommandRegister2(t1,t2) \
156  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) \
157  { \
158    if (isRegistered(commandName, className, 2, t1##_PARAM, t2##_PARAM)== true) \
159      return NULL; \
160    return new ShellCommand<T>(commandName, className, function, d1, d2); \
161  }
162
163//! registers a command with 3 parameters
164#define ShellCommandRegister3(t1,t2,t3) \
165  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) \
166  { \
167    if (isRegistered(commandName, className, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM)== true) \
168      return NULL; \
169    return new ShellCommand<T>(commandName, className, function, d1, d2, d3); \
170  }
171
172//! registers a command with 4 parameters
173#define ShellCommandRegister4(t1,t2,t3,t4) \
174  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) \
175  { \
176    if (isRegistered(commandName, className, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM)== true) \
177      return NULL; \
178    return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4); \
179  }
180
181//! registers a command with 5 parameters
182#define ShellCommandRegister5(t1,t2,t3,t4,t5) \
183  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) \
184  { \
185    if (isRegistered(commandName, className, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM)== true) \
186      return NULL; \
187    return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4, d5); \
188  }
189
190//////////////////
191// CONSTRUCTORS //
192/////////////////
193//! creates a command that takes no parameters
194#define ShellCommandConstructor0() \
195  void (T::*functionPointer_0)(); \
196  ShellCommand(const char* commandName, const char* className, void (T::*function)()) \
197  : ShellCommandBase(commandName, className, 0) \
198  { \
199    this->functionPointer_0 = function; \
200  }
201
202//! creates a command that takes one parameter
203#define ShellCommandConstructor1(t1) \
204  void (T::*functionPointer_1_##t1)(t1##_TYPE); \
205  ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE), t1##_TYPE d1) \
206  : ShellCommandBase(commandName, className, 1, t1##_PARAM, d1) \
207  { \
208    this->functionPointer_1_##t1 = function; \
209  }
210
211//! creates a command that takes two parameters
212#define ShellCommandConstructor2(t1,t2) \
213  void (T::*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE); \
214  ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1, t2##_TYPE d2) \
215  : ShellCommandBase(commandName, className, 2, t1##_PARAM, d1, t2##_PARAM, d2) \
216  { \
217    this->functionPointer_2_##t1##_##t2 = function; \
218  }
219
220//! creates a command that takes three parameter
221#define ShellCommandConstructor3(t1,t2,t3) \
222  void (T::*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE); \
223  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) \
224  : ShellCommandBase(commandName, className, 3, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3) \
225  { \
226    this->functionPointer_3_##t1##_##t2##_##t3 = function; \
227  }
228
229//! creates a command that takes four parameter
230#define ShellCommandConstructor4(t1,t2,t3,t4) \
231  void (T::*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE); \
232  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) \
233  : ShellCommandBase(commandName, className, 4, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4) \
234  { \
235    this->functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
236  }
237
238//! creates a command that takes five parameter
239#define ShellCommandConstructor5(t1,t2,t3,t4,t5) \
240  void (T::*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
241  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) \
242  : ShellCommandBase(commandName, className, 5, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4, t5##_PARAM, d5) \
243  { \
244    this->functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
245  }
246
247///////////////
248// EXECUTION //
249///////////////
250//! execute-macro for functions with no parameters
251#define ShellCommandExecute0() \
252  if (this->paramCount == 0) \
253    (dynamic_cast<T*>(object)->*functionPointer_0)()
254
255//! execute-macro for functions with one parameter
256#define ShellCommandExecute1(t1) \
257   else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \
258    (dynamic_cast<T*>(object)->*functionPointer_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0)))
259
260//! execute-macro for functions with two parameters
261#define ShellCommandExecute2(t1,t2) \
262   else if (this->paramCount == 2 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM) \
263    (dynamic_cast<T*>(object)->*functionPointer_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))
264
265//! execute-macro for functions with three parameters
266#define ShellCommandExecute3(t1,t2,t3) \
267   else if (this->paramCount == 3 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM) \
268    (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)))
269
270//! execute-macro for functions with four parameters
271#define ShellCommandExecute4(t1,t2,t3,t4) \
272   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) \
273    (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)))
274
275//! execute-macro for functions with five parameters
276#define ShellCommandExecute5(t1,t2,t3,t4,t5) \
277   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) \
278    (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)))
279
280
281//! keeps information about a ShellCommand
282template<class T> class ShellCommand : public ShellCommandBase
283{
284  public:
285
286#ifdef FUNCTOR_LIST
287#undef FUNCTOR_LIST
288#endif
289
290//! FUNCTOR_LIST is the List of command-registerers
291#define FUNCTOR_LIST(x) ShellCommandRegister ## x
292#include "functor_list.h"
293#undef FUNCTOR_LIST
294
295
296  private:
297//! FUNCTOR_LIST is the List of CommandConstructors
298#define FUNCTOR_LIST(x) ShellCommandConstructor ## x
299#include "functor_list.h"
300#undef FUNCTOR_LIST
301
302    virtual void executeCommand (BaseObject* object, const char* parameters)
303    {
304//      if (parameters != NULL)
305      SubString sub(parameters);
306//! FUNCTOR_LIST is the List of Executive Functions
307#define FUNCTOR_LIST(x) ShellCommandExecute ## x
308#include "functor_list.h"
309#undef FUNCTOR_LIST
310    }
311};
312
313#endif /* _SHELL_COMMAND_H */
Note: See TracBrowser for help on using the repository browser.