Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: default value can now be supplied as aditional arg of the SHELL_COMMAND-macro

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