Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: construct for ShellCommandAlias… have to work some more on this :/

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