Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/event/command_chain.h @ 5515

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

orxonox/trunk: command-chain (copy and renaming of shell_command) added to project … think some more

File size: 18.9 KB
Line 
1/*!
2 * @file command_chain.h
3 * Definition of a on-screen-shell
4 */
5
6#ifndef _COMMAND_CHAIN_H
7#define _COMMAND_CHAIN_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     COMMAND_CHAIN_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 *  CommandChainBase* someUniqueVarName =
32 *       CommandChain<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 COMMAND_CHAIN(command, class, function) \
38        CommandChainBase* command_chain_##class##_##command = CommandChain<class>::registerCommand(#command, #class, &class::function)
39/**
40 * an easy to use Macro to create a Command
41 * @param command the name of the command (without "" around the string)
42 * @param class the name of the class to apply this command to (without the "" around the string)
43 * @param function the function to call
44 *
45 * MEANING:
46 *  CommandChainBase* someUniqueVarName =
47 *       CommandChain<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall);
48 *
49 * In the Shell you would call this Command using:
50 * $ ClassName [ObjectName] commandNameInShell [parameters]
51 */
52#define COMMAND_CHAIN_STATIC(command, class, function) \
53                         CommandChainBase* command_chain_##class##_##command = CommandChainStatic<class>::registerCommand(#command, #class, function)
54
55
56//! an enumerator for the definition of the Type.
57typedef enum {
58  CommandChain_Objective = 1,
59  CommandChain_Static    = 2,
60} CommandChain_Type;
61
62////////////////
63// BASE CLASS //
64////////////////
65class CommandChainBase;
66class CommandChainAlias;
67
68//! A class to hold all Classes that have (once) registered Commands.
69class CommandChainClass : public BaseObject
70{
71  friend class CommandChainBase;
72
73  public:
74    /** @returns the CommandClassList */
75    static const tList<CommandChainClass>* getCommandClassList() { return CommandChainClass::commandClassList; };
76    static bool getCommandListOfClass(const char* className, tList<const char>* stringList);
77    static bool getCommandListOfAlias(tList<const char>* aliasList);
78
79    static CommandChainClass* getCommandClass(const char* className);
80    static void unregisterAllCommands();
81
82    static void help (const char* className);
83
84  private:
85    CommandChainClass(const char* className);
86    ~CommandChainClass();
87
88    static const CommandChainClass* isRegistered(const char* className);
89    static void initCommandClassList();
90
91  private:
92    const char*                      className;                 //!< The Name of the Class. This should match the ClassName of the Commands Class.
93    long                             classID;                   //!< The classID of this Class
94    tList<CommandChainBase>*         commandList;               //!< A list of Commands from this Class
95    static tList<CommandChainClass>* commandClassList;          //!< A list of Classes
96    static tList<CommandChainAlias>* aliasList;                 //!< An Alias to A Command. (only for classes with one Instance)
97};
98
99
100//! a baseClass for all possible CommandChains
101class CommandChainBase : public BaseObject
102{
103  friend class CommandChainClass;
104  public:
105    static bool execute (const char* executionString);
106
107    CommandChainBase* describe(const char* description);
108    CommandChainBase* setAlias(const char* alias);
109    CommandChainBase* defaultValues(unsigned int count, ...);
110
111    /** @returns the CommandList of the Shell */
112    static void unregisterCommand(const char* commandName, const char* className);
113
114    static void debug();
115
116  protected:
117    CommandChainBase(const char* commandName, const char* className, unsigned int paramCount, ...);
118    ~CommandChainBase();
119
120    /** @returns the Type of this Function (either static or objective) */
121    inline CommandChain_Type getType() { return this->functorType; };
122
123    static bool isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...);
124    static const char* paramToString(long parameter);
125
126  private:
127    /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */
128    virtual void executeCommand (BaseObject* object, const char* parameters) = 0;
129
130  protected:
131    CommandChain_Type                functorType;                          //!< The type of Function we've got (either static or objective).
132    void*                            functionPointer;                      //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL )
133    unsigned int                     paramCount;                           //!< the count of parameters.
134    unsigned int*                    parameters;                           //!< Parameters the function of this Command takes.
135    char*                            defaultStrings[FUNCTOR_MAX_ARGUMENTS];//!< A list of default Strings stored.
136    int                              defaultInts[FUNCTOR_MAX_ARGUMENTS];   //!< A list of default Ints stored.
137    float                            defaultFloats[FUNCTOR_MAX_ARGUMENTS]; //!< A list of default Floats stored.
138    bool                             defaultBools[FUNCTOR_MAX_ARGUMENTS];  //!< A list of default Bools stored.
139
140
141  private:
142    CommandChainClass*               shellClass;                           //!< A Pointer to the Shell-Class this Command belongs to.
143    CommandChainAlias*               alias;                                //!< An Alias for the Class.
144
145    const char*                      description;                          //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla");
146};
147
148///////////////////////////////////////////////////
149///////////////////////////////////////////////////
150
151/////////////////////////////////
152// MACRO DEFINITION EXTENSIONS //
153/////////////////////////////////
154//! where to chek for default BOOL values
155#define   l_BOOL_DEFGRAB(i)         this->defaultBools[i]
156//! where to chek for default INT values
157#define   l_INT_DEFGRAB(i)          this->defaultInts[i]
158//! where to chek for default UINT values
159#define   l_UINT_DEFGRAB(i)         (unsigned int)this->defaultInts[i]
160//! where to chek for default LONG values
161#define   l_LONG_DEFGRAB(i)         (long)this->defaultInts[i]
162//! where to chek for default FLOAT values
163#define   l_FLOAT_DEFGRAB(i)        this->defaultFloats[i]
164//! where to chek for default STRING values
165#define   l_STRING_DEFGRAB(i)       this->defaultStrings[i]
166
167//////////////////////////
168// COMMAND REGISTRATION //
169//////////////////////////
170// SHELLCOMMAND can be redefined as CommandChain or CommandChainStatic
171// SHELLCOMMANDEXECUTER can be redefined too.
172// SHELLCOMMANDINCLASS
173// SHELLCOMMANDTYPE
174//! registers a command without any parameters
175#define CommandChainRegister0() \
176  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)()) \
177  { \
178    if (isRegistered(commandName, className, 0)== true) \
179      return NULL; \
180    return new SHELLCOMMAND<T>(commandName, className, function); \
181  }
182
183//! registers a command with 1 parameter
184#define CommandChainRegister1(t1) \
185  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \
186  { \
187    if (isRegistered(commandName, className, 1, t1##_PARAM)== true) \
188      return NULL; \
189    return new SHELLCOMMAND<T>(commandName, className, function, d1); \
190  }
191
192//! registers a command with 2 parameters
193#define CommandChainRegister2(t1,t2) \
194  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT) \
195  { \
196    if (isRegistered(commandName, className, 2, t1##_PARAM, t2##_PARAM)== true) \
197      return NULL; \
198    return new SHELLCOMMAND<T>(commandName, className, function, d1, d2); \
199  }
200
201//! registers a command with 3 parameters
202#define CommandChainRegister3(t1,t2,t3) \
203  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT, t3##_TYPE d3 = t3##_DEFAULT) \
204  { \
205    if (isRegistered(commandName, className, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM)== true) \
206      return NULL; \
207    return new SHELLCOMMAND<T>(commandName, className, function, d1, d2, d3); \
208  }
209
210//! registers a command with 4 parameters
211#define CommandChainRegister4(t1,t2,t3,t4) \
212  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*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) \
213  { \
214    if (isRegistered(commandName, className, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM)== true) \
215      return NULL; \
216    return new SHELLCOMMAND<T>(commandName, className, function, d1, d2, d3, d4); \
217  }
218
219//! registers a command with 5 parameters
220#define CommandChainRegister5(t1,t2,t3,t4,t5) \
221  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*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) \
222  { \
223    if (isRegistered(commandName, className, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM)== true) \
224      return NULL; \
225    return new CommandChain<T>(commandName, className, function, d1, d2, d3, d4, d5); \
226  }
227
228//////////////////
229// CONSTRUCTORS //
230/////////////////
231//! creates a command that takes no parameters
232#define CommandChainConstructor0() \
233  void SHELLCOMMANDINCLASS(*functionPointer_0)(); \
234  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)()) \
235  : CommandChainBase(commandName, className, 0) \
236  { \
237    this->functorType = SHELLCOMMANDTYPE; \
238    this->functionPointer_0 = function; \
239  }
240
241//! creates a command that takes one parameter
242#define CommandChainConstructor1(t1) \
243  void SHELLCOMMANDINCLASS(*functionPointer_1_##t1)(t1##_TYPE); \
244  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE), t1##_TYPE d1) \
245  : CommandChainBase(commandName, className, 1, t1##_PARAM, d1) \
246  { \
247    this->functorType = SHELLCOMMANDTYPE; \
248    this->functionPointer_1_##t1 = function; \
249  }
250
251//! creates a command that takes two parameters
252#define CommandChainConstructor2(t1,t2) \
253  void SHELLCOMMANDINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE); \
254  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1, t2##_TYPE d2) \
255  : CommandChainBase(commandName, className, 2, t1##_PARAM, d1, t2##_PARAM, d2) \
256  { \
257    this->functorType = SHELLCOMMANDTYPE; \
258    this->functionPointer_2_##t1##_##t2 = function; \
259  }
260
261//! creates a command that takes three parameter
262#define CommandChainConstructor3(t1,t2,t3) \
263  void SHELLCOMMANDINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE); \
264  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3) \
265  : CommandChainBase(commandName, className, 3, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3) \
266  { \
267    this->functorType = SHELLCOMMANDTYPE; \
268    this->functionPointer_3_##t1##_##t2##_##t3 = function; \
269  }
270
271//! creates a command that takes four parameter
272#define CommandChainConstructor4(t1,t2,t3,t4) \
273  void SHELLCOMMANDINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE); \
274  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3, t4##_TYPE d4) \
275  : CommandChainBase(commandName, className, 4, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4) \
276  { \
277    this->functorType = SHELLCOMMANDTYPE; \
278    this->functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
279  }
280
281//! creates a command that takes five parameter
282#define CommandChainConstructor5(t1,t2,t3,t4,t5) \
283  void SHELLCOMMANDINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
284  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*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) \
285  : CommandChainBase(commandName, className, 5, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4, t5##_PARAM, d5) \
286  { \
287    this->functorType = SHELLCOMMANDTYPE; \
288    this->functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
289  }
290
291///////////////
292// EXECUTION //
293///////////////
294//! execute-macro for functions with no parameters
295#define CommandChainExecute0() \
296  if (this->paramCount == 0) \
297    SHELLCOMMANDEXECUTER(_0)()
298
299//! execute-macro for functions with one parameter
300#define CommandChainExecute1(t1) \
301   else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \
302    SHELLCOMMANDEXECUTER(_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0)))
303
304//! execute-macro for functions with two parameters
305#define CommandChainExecute2(t1,t2) \
306   else if (this->paramCount == 2 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM) \
307    SHELLCOMMANDEXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))
308
309//! execute-macro for functions with three parameters
310#define CommandChainExecute3(t1,t2,t3) \
311   else if (this->paramCount == 3 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM) \
312    SHELLCOMMANDEXECUTER(_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)))
313
314//! execute-macro for functions with four parameters
315#define CommandChainExecute4(t1,t2,t3,t4) \
316   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) \
317    SHELLCOMMANDEXECUTER(_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)))
318
319//! execute-macro for functions with five parameters
320#define CommandChainExecute5(t1,t2,t3,t4,t5) \
321   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) \
322    SHELLCOMMANDEXECUTER(_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)))
323
324
325//! keeps information about a CommandChain
326template<class T> class CommandChain : public CommandChainBase
327{
328  public:
329#ifdef FUNCTOR_LIST
330#undef FUNCTOR_LIST
331#endif
332#ifdef SHELLCOMMAND
333#undef SHELLCOMMAND
334#endif
335#define SHELLCOMMAND                       CommandChain
336#ifdef SHELLCOMMANDEXECUTER
337#undef SHELLCOMMANDEXECUTER
338#endif
339#define SHELLCOMMANDEXECUTER(nameExt)      (dynamic_cast<T*>(object)->*functionPointer##nameExt)
340#ifdef SHELLCOMMANDINCLASS
341#undef SHELLCOMMANDINCLASS
342#endif
343#define SHELLCOMMANDINCLASS(FUNCTION)      (T::FUNCTION)
344#ifdef SHELLCOMMANDTYPE
345#undef SHELLCOMMANDTYPE
346#endif
347#define SHELLCOMMANDTYPE                   CommandChain_Objective
348//! FUNCTOR_LIST is the List of command-registerers
349#define FUNCTOR_LIST(x) CommandChainRegister ## x
350#include "functor_list.h"
351#undef FUNCTOR_LIST
352
353
354  private:
355//! FUNCTOR_LIST is the List of CommandConstructors
356#define FUNCTOR_LIST(x) CommandChainConstructor ## x
357#include "functor_list.h"
358#undef FUNCTOR_LIST
359
360    virtual void executeCommand (BaseObject* object, const char* parameters)
361    {
362      SubString sub(parameters, true);
363//! FUNCTOR_LIST is the List of Executive Functions
364#define FUNCTOR_LIST(x) CommandChainExecute ## x
365#include "functor_list.h"
366#undef FUNCTOR_LIST
367    }
368};
369
370//! keeps information about a CommandChain, that points to a Static Function
371template<class T> class CommandChainStatic : public CommandChainBase
372{
373  public:
374#ifdef FUNCTOR_LIST
375#undef FUNCTOR_LIST
376#endif
377#ifdef SHELLCOMMAND
378#undef SHELLCOMMAND
379#endif
380#define SHELLCOMMAND                      CommandChainStatic
381#ifdef SHELLCOMMANDEXECUTER
382#undef SHELLCOMMANDEXECUTER
383#endif
384#define SHELLCOMMANDEXECUTER(nameExt)     functionPointer##nameExt
385#ifdef SHELLCOMMANDINCLASS
386#undef SHELLCOMMANDINCLASS
387#endif
388#define SHELLCOMMANDINCLASS(FUNCTION)     (FUNCTION)
389#ifdef SHELLCOMMANDTYPE
390#undef SHELLCOMMANDTYPE
391#endif
392#define SHELLCOMMANDTYPE                   CommandChain_Static
393
394//! FUNCTOR_LIST is the List of command-registerers
395#define FUNCTOR_LIST(x) CommandChainRegister ## x
396#include "functor_list.h"
397#undef FUNCTOR_LIST
398
399  private:
400//! FUNCTOR_LIST is the List of CommandConstructors
401#define FUNCTOR_LIST(x) CommandChainConstructor ## x
402#include "functor_list.h"
403#undef FUNCTOR_LIST
404
405    virtual void executeCommand (BaseObject* object, const char* parameters)
406    {
407  SubString sub(parameters, true);
408//! FUNCTOR_LIST is the List of Executive Functions
409#define FUNCTOR_LIST(x) CommandChainExecute ## x
410#include "functor_list.h"
411#undef FUNCTOR_LIST
412    }
413};
414
415//! A Class, that handles aliases.
416class CommandChainAlias
417{
418  friend class CommandChainBase;
419  public:
420    /** @returns the Name of the Alias. */
421    const char* getName() const { return this->aliasName; };
422    /** @returns the Command, this Alias is asociated with */
423    CommandChainBase* getCommand() const { return this->command; };
424
425  private:
426    /** @param aliasName the name of the Alias @param command the Command, to associate this alias with */
427    CommandChainAlias(const char* aliasName, CommandChainBase* command) { this->aliasName = aliasName; this->command = command; };
428
429  private:
430    const char*         aliasName;       //!< the name of the Alias
431    CommandChainBase*   command;         //!< a pointer to the command, this alias executes.
432};
433
434#endif /* _COMMAND_CHAIN_H */
Note: See TracBrowser for help on using the repository browser.