Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: doxygen-tags

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