Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellInput is now almost perfectly extern.
ShellCompletion taken out.
Working again :)

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