Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minor function, to retrieve a List of Commands from a ClassList

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