Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some more implementation

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