Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/shell_command.h @ 5150

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

orxonox/trunk: SubString is now Safe also for NULL and 1 parameters

File size: 5.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"
[5143]12#include "functor_list.h"
[5145]13#include "substring.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///////////////////////
28
[5142]29// COMMAND REGISTRATION
30// NO ARGUMENTS
31#define ShellCommandRegister0 \
[5145]32  static void registerCommand(const char* commandName, ClassID classID, void (T::*function)()) \
[5142]33  { \
34    if (isRegistered(commandName, classID, 0)== true) \
35      return; \
[5145]36    new ShellCommand<T>(commandName, classID, function); \
[5142]37  }
[5135]38
[5145]39#define ShellCommandRegister1(t1) \
40  static void registerCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \
[5135]41  { \
[5145]42    if (isRegistered(commandName, classID, 1, t1##_PARAM)== true) \
43      return; \
44    new ShellCommand<T>(commandName, classID, function, d1); \
[5142]45  }
46
[5148]47// CONSTRUCTORS
[5145]48#define ShellCommandConstructor0 \
49  void (T::*functionPointer_0)(); \
50  ShellCommand(const char* commandName, ClassID classID, void (T::*function)()) \
51  : ShellCommandBase(commandName, classID, 0) \
[5142]52  { \
[5145]53    this->functionPointer_0 = function; \
[5142]54  }
55
[5145]56#define ShellCommandConstructor1(t1) \
57  void (T::*functionPointer_1_##t1)(t1##_TYPE); \
58  ShellCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE), t1##_TYPE d1) \
59  : ShellCommandBase(commandName, classID, 1, t1##_PARAM, d1) \
[5135]60  { \
[5145]61    this->functionPointer_1_##t1 = function; \
[5135]62  }
63
64
[5141]65
[5145]66#define ShellCommandExecute0 \
67  if (this->paramCount == 0) \
68    (dynamic_cast<T*>(object)->*functionPointer_0)()
69
70#define ShellCommandExecute1(t1) \
[5146]71   else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \
[5148]72    (dynamic_cast<T*>(object)->*functionPointer_1_##t1)(t1##_FUNC(parameters, 0))
[5145]73
74
[5135]75////////////////
76// BASE CLASS //
77////////////////
78//! a baseClass for all possible ShellCommands
[5130]79class ShellCommandBase : public BaseObject
[5129]80{
[5068]81  public:
[5129]82    static bool execute (const char* executionString);
[2036]83
[5141]84    static const tList<ShellCommandBase>* getCommandList() { return ShellCommandBase::commandList; };
[5130]85
[5141]86
[5129]87  protected:
[5135]88    ShellCommandBase(const char* commandName, ClassID classID, unsigned int paramCount, ...);
[5130]89    ~ShellCommandBase();
[1853]90
[5135]91    static bool isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...);
[5148]92    static const char* paramToString(long parameter);
[1853]93
[5148]94    void debug();
[5141]95  private:
[5142]96    virtual void executeCommand (BaseObject* object, const char* parameters) = NULL;
[5141]97
[5130]98  protected:
99    void*                            functionPointer;     //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL )
100    unsigned int                     paramCount;          //!< the count of parameters
[5148]101    unsigned int*                    parameters;          //!< Parameters
[5140]102    bool                             isSingleton;         //!< if the Class is Singleton @todo autocheck
[5130]103
[5129]104  private:
[5141]105    long                             classID;             //!< The ID of the Class associated to this Command
106    const char*                      className;           //!< The Name of the Class associated to this Command
[3245]107
[5142]108    char*                            defaultStrings[FUNCTOR_MAX_ARGUMENTS];
109    int                              defaultInts[FUNCTOR_MAX_ARGUMENTS];
110    float                            defaultFloats[FUNCTOR_MAX_ARGUMENTS];
111    bool                             defaultBools[FUNCTOR_MAX_ARGUMENTS];
112
113
[5130]114    // STATIC MEMBERS
[5129]115    static tList<ShellCommandBase>*  commandList;         //!< A list of availiable commands.
116};
[5068]117
[5136]118template<class T> class ShellCommand;
[5142]119/*
[5136]120template<class T> class ShellCommandSingleton : public ShellCommandBase
121{
122  friend class ShellCommand<T>;
123  private:
124    ShellCommandSingleton(const char* commandName, ClassID classID, void (T::*functionPointer)())
125    : ShellCommandBase (commandName, classID, 0)
126    {
127      this->functionPointer_NULL = functionPointer;
128    }
129    void (T::*functionPointer_NULL)();
[5142]130
[5136]131    virtual void executeCommand (const char* parameters)
132    {
133      if (this->paramCount == 0)
134        (T::getInstance()->*functionPointer_NULL)();
135    }
[5142]136};*/
[5135]137
[5136]138
[5129]139//! keeps information about a ShellCommand
140template<class T> class ShellCommand : public ShellCommandBase
141{
142  public:
143    static void unregisterCommand(const char* commandNaame, ClassID classID);
[5068]144
[5142]145#define FUNCTOR_LIST(x) ShellCommandRegister ## x
146//#include "functor_list.h"
[5148]147  FUNCTOR_LIST(0);
148  FUNCTOR_LIST(1)(l_INT);
149  FUNCTOR_LIST(1)(l_STRING);
[5142]150#undef FUNCTOR_LIST
[5136]151
[5142]152
[5068]153  private:
[5142]154#define FUNCTOR_LIST(x) ShellCommandConstructor ## x
155//#include "functor_list.h"
156  FUNCTOR_LIST(0);
[5145]157  FUNCTOR_LIST(1)(l_INT);
158  FUNCTOR_LIST(1)(l_STRING);
[5142]159#undef FUNCTOR_LIST
160
161    virtual void executeCommand (BaseObject* object, const char* parameters)
[5135]162    {
[5149]163//      if (parameters != NULL)
[5150]164      SubString subLoads(parameters);
165      subLoads.debug();
[5145]166#define FUNCTOR_LIST(x) ShellCommandExecute ## x
[5135]167//#include "functor_list.h"
[5145]168  FUNCTOR_LIST(0);
169  FUNCTOR_LIST(1)(l_INT);
[5149]170  FUNCTOR_LIST(1)(l_STRING);
[5135]171#undef FUNCTOR_LIST
[5145]172    }
[5129]173};
[5113]174
[5129]175#endif /* _SHELL_COMMAND_H */
Note: See TracBrowser for help on using the repository browser.