Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now it is possible to get old commands from the List

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