Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: match on Singleton dynamically

File size: 11.0 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
[5145]17#define SHELL_COMMAND_MAX_SIZE
[5130]18
[5127]19
[5130]20
[4838]21// FORWARD DECLARATION
[5068]22template<class T> class tList;
[3543]23
[5162]24#define SHELL_COMMAND(command, class, function) \
25        ShellCommand<class>* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function)
[5135]26
[5162]27
[5161]28////////////////
29// BASE CLASS //
30////////////////
31//! a baseClass for all possible ShellCommands
32class ShellCommandBase : public BaseObject
33{
34  public:
35    static bool execute (const char* executionString);
36
37    static const tList<ShellCommandBase>* getCommandList() { return ShellCommandBase::commandList; };
38
39//    static void unregisterCommand(const char* commandNaame, const char* className);
40
41    static void debug();
42
43  protected:
44    ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...);
45    ~ShellCommandBase();
46
47    static bool isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...);
48    static const char* paramToString(long parameter);
49
50    void debugDyn();
51
52  private:
53    virtual void executeCommand (BaseObject* object, const char* parameters) = NULL;
54
55  protected:
[5163]56    void*                            functionPointer;                      //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL )
57    unsigned int                     paramCount;                           //!< the count of parameters
58    unsigned int*                    parameters;                           //!< Parameters
[5161]59    char*                            defaultStrings[FUNCTOR_MAX_ARGUMENTS];
60    int                              defaultInts[FUNCTOR_MAX_ARGUMENTS];
61    float                            defaultFloats[FUNCTOR_MAX_ARGUMENTS];
62    bool                             defaultBools[FUNCTOR_MAX_ARGUMENTS];
63
64  private:
65//    long                             classID;             //!< The ID of the Class associated to this Command
[5163]66    const char*                      className;                            //!< The Name of the Class associated to this Command
[5161]67
68    // STATIC MEMBERS
[5163]69    static tList<ShellCommandBase>*  commandList;                          //!< A list of availiable commands.
[5161]70};
71
72///////////////////////////////////////////////////
73///////////////////////////////////////////////////
74
[5135]75///////////////////////
76// MACRO DEFINITIONS //
77///////////////////////
[5151]78#define   l_BOOL_DEFGRAB(i)         this->defaultBools[i]
79#define   l_INT_DEFGRAB(i)          this->defaultInts[i]
80#define   l_UINT_DEFGRAB(i)         (unsigned int)this->defaultInts[i]
81#define   l_LONG_DEFGRAB(i)         (long)this->defaultInts[i]
82#define   l_FLOAT_DEFGRAB(i)        this->defaultFloats[i]
83#define   l_STRING_DEFGRAB(i)       this->defaultStrings[i]
[5135]84
[5153]85//////////////////////////
86// COMMAND REGISTRATION //
87//////////////////////////
[5142]88// NO ARGUMENTS
[5153]89#define ShellCommandRegister0() \
[5161]90  static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)()) \
[5142]91  { \
[5161]92    if (isRegistered(commandName, className, 0)== true) \
93      return NULL; \
94    return new ShellCommand<T>(commandName, className, function); \
[5142]95  }
[5135]96
[5145]97#define ShellCommandRegister1(t1) \
[5161]98  static ShellCommand<T>* registerCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \
[5135]99  { \
[5161]100    if (isRegistered(commandName, className, 1, t1##_PARAM)== true) \
101      return NULL; \
102    return new ShellCommand<T>(commandName, className, function, d1); \
[5142]103  }
104
[5153]105#define ShellCommandRegister2(t1,t2) \
[5161]106  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]107  { \
[5161]108    if (isRegistered(commandName, className, 2, t1##_PARAM, t2##_PARAM)== true) \
109      return NULL; \
110    return new ShellCommand<T>(commandName, className, function, d1, d2); \
[5153]111  }
112
113#define ShellCommandRegister3(t1,t2,t3) \
[5161]114  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]115  { \
[5161]116    if (isRegistered(commandName, className, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM)== true) \
117      return NULL; \
118    return new ShellCommand<T>(commandName, className, function, d1, d2, d3); \
[5153]119  }
120
121#define ShellCommandRegister4(t1,t2,t3,t4) \
[5161]122  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]123  { \
[5161]124    if (isRegistered(commandName, className, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM)== true) \
125      return NULL; \
126    return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4); \
[5153]127  }
[5161]128
[5153]129#define ShellCommandRegister5(t1,t2,t3,t4,t5) \
[5161]130  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]131  { \
[5161]132    if (isRegistered(commandName, className, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM)== true) \
133      return NULL; \
134    return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4, d5); \
[5153]135  }
136
137//////////////////
138// CONSTRUCTORS //
139/////////////////
140#define ShellCommandConstructor0() \
[5145]141  void (T::*functionPointer_0)(); \
[5161]142  ShellCommand(const char* commandName, const char* className, void (T::*function)()) \
143  : ShellCommandBase(commandName, className, 0) \
[5142]144  { \
[5145]145    this->functionPointer_0 = function; \
[5142]146  }
147
[5145]148#define ShellCommandConstructor1(t1) \
149  void (T::*functionPointer_1_##t1)(t1##_TYPE); \
[5161]150  ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE), t1##_TYPE d1) \
151  : ShellCommandBase(commandName, className, 1, t1##_PARAM, d1) \
[5135]152  { \
[5145]153    this->functionPointer_1_##t1 = function; \
[5135]154  }
155
[5153]156#define ShellCommandConstructor2(t1,t2) \
157  void (T::*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE); \
[5161]158  ShellCommand(const char* commandName, const char* className, void (T::*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1, t2##_TYPE d2) \
159  : ShellCommandBase(commandName, className, 2, t1##_PARAM, d1, t2##_PARAM, d2) \
[5153]160  { \
161    this->functionPointer_2_##t1##_##t2 = function; \
162  }
[5135]163
[5153]164#define ShellCommandConstructor3(t1,t2,t3) \
165  void (T::*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE); \
[5161]166  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) \
167  : ShellCommandBase(commandName, className, 3, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3) \
[5153]168  { \
169    this->functionPointer_3_##t1##_##t2##_##t3 = function; \
170  }
[5141]171
[5153]172#define ShellCommandConstructor4(t1,t2,t3,t4) \
173  void (T::*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE); \
[5161]174  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) \
175  : ShellCommandBase(commandName, className, 4, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4) \
[5153]176  { \
177    this->functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
178  }
179
180#define ShellCommandConstructor5(t1,t2,t3,t4,t5) \
181  void (T::*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
[5161]182  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) \
183  : ShellCommandBase(commandName, className, 5, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4, t5##_PARAM, d5) \
[5153]184  { \
185    this->functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
186  }
187
188///////////////
189// EXECUTION //
190///////////////
191#define ShellCommandExecute0() \
[5145]192  if (this->paramCount == 0) \
193    (dynamic_cast<T*>(object)->*functionPointer_0)()
194
195#define ShellCommandExecute1(t1) \
[5146]196   else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \
[5151]197    (dynamic_cast<T*>(object)->*functionPointer_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0)))
[5145]198
[5153]199#define ShellCommandExecute2(t1,t2) \
200   else if (this->paramCount == 2 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM) \
201    (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]202
[5153]203#define ShellCommandExecute3(t1,t2,t3) \
204   else if (this->paramCount == 3 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM) \
205    (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)))
206
207#define ShellCommandExecute4(t1,t2,t3,t4) \
208   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) \
209    (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)))
210
211#define ShellCommandExecute5(t1,t2,t3,t4,t5) \
212   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) \
213    (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)))
214
215
[5129]216//! keeps information about a ShellCommand
217template<class T> class ShellCommand : public ShellCommandBase
218{
219  public:
[5068]220
[5153]221#ifdef FUNCTOR_LIST
222#undef FUNCTOR_LIST
223#endif
224
[5142]225#define FUNCTOR_LIST(x) ShellCommandRegister ## x
[5153]226#include "functor_list.h"
[5142]227#undef FUNCTOR_LIST
[5136]228
[5142]229
[5068]230  private:
[5142]231#define FUNCTOR_LIST(x) ShellCommandConstructor ## x
[5153]232#include "functor_list.h"
[5142]233#undef FUNCTOR_LIST
234
235    virtual void executeCommand (BaseObject* object, const char* parameters)
[5135]236    {
[5149]237//      if (parameters != NULL)
[5153]238      SubString sub(parameters);
[5145]239#define FUNCTOR_LIST(x) ShellCommandExecute ## x
[5153]240#include "functor_list.h"
[5135]241#undef FUNCTOR_LIST
[5145]242    }
[5129]243};
[5113]244
[5129]245#endif /* _SHELL_COMMAND_H */
Note: See TracBrowser for help on using the repository browser.