Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: shell is now a lib for its own

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