Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5326 was 5326, checked in by bensch, 20 years ago

orxonox/trunk: ShellCommandStatic defined… now going to test it, (or revert if not working)

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