Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more overwhelmingly complicated code, that slowly gets out of my grasp

File size: 17.3 KB
RevLine 
[4838]1/*!
[5129]2 * @file shell_command.h
[5068]3 * Definition of a on-screen-shell
[5243]4 *
5 * @todo also take Static functions
[3245]6*/
[1853]7
[5129]8#ifndef _SHELL_COMMAND_H
9#define _SHELL_COMMAND_H
[1853]10
[5129]11#include "base_object.h"
[1853]12
[5141]13#include "helper_functions.h"
[5155]14#include "substring.h"
[5143]15#include "functor_list.h"
[5141]16
[5068]17#include <stdarg.h>
18
[5166]19#define     SHELL_COMMAND_MAX_SIZE      //!< The maximum size of a Shell Command
[5130]20
[5127]21
[5130]22
[4838]23// FORWARD DECLARATION
[5068]24template<class T> class tList;
[3543]25
[5166]26/**
27 * an easy to use Macro to create a Command
28 * @param command the name of the command (without "" around the string)
29 * @param class the name of the class to apply this command to (without the "" around the string)
30 * @param function the function to call
[5179]31 *
32 * MEANING:
33 *  ShellCommandBase* someUniqueVarName =
34 *       ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall);
35 *
36 * In the Shell you would call this Command using:
37 * $ ClassName [ObjectName] commandNameInShell [parameters]
[5166]38 */
[5162]39#define SHELL_COMMAND(command, class, function) \
[5164]40        ShellCommandBase* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function)
[5135]41
[5162]42
[5161]43////////////////
44// BASE CLASS //
45////////////////
[5170]46class ShellCommandBase;
[5190]47class ShellCommandAlias;
[5170]48
49//! A class to hold all Classes that have (once) registered Commands.
50class ShellCommandClass : public BaseObject
51{
52  friend class ShellCommandBase;
53
54  public:
[5197]55    /** @returns the CommandClassList */
[5170]56    static const tList<ShellCommandClass>* getCommandClassList() { return ShellCommandClass::commandClassList; };
[5195]57    static bool getCommandListOfClass(const char* className, tList<const char>* stringList);
58    static bool getCommandListOfAlias(tList<const char>* aliasList);
[5190]59
[5170]60    static ShellCommandClass* getCommandClass(const char* className);
[5171]61    static void unregisterAllCommands();
[5170]62
[5204]63    static void help (const char* className);
64
[5170]65  private:
66    ShellCommandClass(const char* className);
67    ~ShellCommandClass();
68
69    static const ShellCommandClass* isRegistered(const char* className);
70    static void initCommandClassList();
71
72  private:
[5171]73    const char*                      className;                 //!< The Name of the Class. This should match the ClassName of the Commands Class.
74    long                             classID;                   //!< The classID of this Class
75    tList<ShellCommandBase>*         commandList;               //!< A list of Commands from this Class
76    static tList<ShellCommandClass>* commandClassList;          //!< A list of Classes
[5195]77    static tList<ShellCommandAlias>* aliasList;                 //!< An Alias to A Command. (only for classes with one Instance)
[5170]78};
79
80
[5161]81//! a baseClass for all possible ShellCommands
82class ShellCommandBase : public BaseObject
83{
[5170]84  friend class ShellCommandClass;
[5161]85  public:
86    static bool execute (const char* executionString);
87
[5164]88    ShellCommandBase* describe(const char* description);
[5190]89    ShellCommandBase* setAlias(const char* alias);
[5207]90    ShellCommandBase* defaultValues(unsigned int count, ...);
[5164]91
[5166]92    /** @returns the CommandList of the Shell */
93    static void unregisterCommand(const char* commandName, const char* className);
[5161]94
95    static void debug();
96
97  protected:
98    ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...);
99    ~ShellCommandBase();
100
101    static bool isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...);
102    static const char* paramToString(long parameter);
103
104    void debugDyn();
105
106  private:
[5166]107    /** executes a Command @param object the object to apply this to @param parameters the parameters the command takes */
[5279]108    virtual void executeCommand (BaseObject* object, const char* parameters) = 0;
[5161]109
110  protected:
[5163]111    void*                            functionPointer;                      //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL )
[5166]112    unsigned int                     paramCount;                           //!< the count of parameters.
113    unsigned int*                    parameters;                           //!< Parameters the function of this Command takes.
114    char*                            defaultStrings[FUNCTOR_MAX_ARGUMENTS];//!< A list of default Strings stored.
115    int                              defaultInts[FUNCTOR_MAX_ARGUMENTS];   //!< A list of default Ints stored.
116    float                            defaultFloats[FUNCTOR_MAX_ARGUMENTS]; //!< A list of default Floats stored.
117    bool                             defaultBools[FUNCTOR_MAX_ARGUMENTS];  //!< A list of default Bools stored.
[5161]118
119  private:
[5170]120    ShellCommandClass*               shellClass;                           //!< A Pointer to the Shell-Class this Command belongs to.
[5196]121    ShellCommandAlias*               alias;                                //!< An Alias for the Class.
[5161]122
[5166]123    const char*                      description;                          //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla");
[5161]124};
125
126///////////////////////////////////////////////////
127///////////////////////////////////////////////////
128
[5326]129/////////////////////////////////
130// MACRO DEFINITION EXTENSIONS //
131/////////////////////////////////
[5166]132//! where to chek for default BOOL values
[5151]133#define   l_BOOL_DEFGRAB(i)         this->defaultBools[i]
[5166]134//! where to chek for default INT values
[5151]135#define   l_INT_DEFGRAB(i)          this->defaultInts[i]
[5166]136//! where to chek for default UINT values
[5151]137#define   l_UINT_DEFGRAB(i)         (unsigned int)this->defaultInts[i]
[5166]138//! where to chek for default LONG values
[5151]139#define   l_LONG_DEFGRAB(i)         (long)this->defaultInts[i]
[5166]140//! where to chek for default FLOAT values
[5151]141#define   l_FLOAT_DEFGRAB(i)        this->defaultFloats[i]
[5166]142//! where to chek for default STRING values
[5151]143#define   l_STRING_DEFGRAB(i)       this->defaultStrings[i]
[5135]144
[5153]145//////////////////////////
146// COMMAND REGISTRATION //
147//////////////////////////
[5326]148// SHELLCOMMAND can be redefined as ShellCommand or ShellCommandStatic
149// SHELLCOMMANDEXECUTER can be redefined too.
[5327]150// SHELLCOMMANDINCLASS
[5166]151//! registers a command without any parameters
[5153]152#define ShellCommandRegister0() \
[5327]153  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)()) \
[5142]154  { \
[5161]155    if (isRegistered(commandName, className, 0)== true) \
156      return NULL; \
[5327]157    return new SHELLCOMMAND<T>(commandName, className, function); \
[5142]158  }
[5135]159
[5166]160//! registers a command with 1 parameter
[5145]161#define ShellCommandRegister1(t1) \
[5327]162  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \
[5135]163  { \
[5161]164    if (isRegistered(commandName, className, 1, t1##_PARAM)== true) \
165      return NULL; \
[5327]166    return new SHELLCOMMAND<T>(commandName, className, function, d1); \
[5142]167  }
168
[5166]169//! registers a command with 2 parameters
[5153]170#define ShellCommandRegister2(t1,t2) \
[5327]171  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT) \
[5153]172  { \
[5161]173    if (isRegistered(commandName, className, 2, t1##_PARAM, t2##_PARAM)== true) \
174      return NULL; \
[5327]175    return new SHELLCOMMAND<T>(commandName, className, function, d1, d2); \
[5153]176  }
177
[5166]178//! registers a command with 3 parameters
[5153]179#define ShellCommandRegister3(t1,t2,t3) \
[5327]180  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT, t3##_TYPE d3 = t3##_DEFAULT) \
[5153]181  { \
[5161]182    if (isRegistered(commandName, className, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM)== true) \
183      return NULL; \
[5327]184    return new SHELLCOMMAND<T>(commandName, className, function, d1, d2, d3); \
[5153]185  }
186
[5166]187//! registers a command with 4 parameters
[5153]188#define ShellCommandRegister4(t1,t2,t3,t4) \
[5327]189  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*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]190  { \
[5161]191    if (isRegistered(commandName, className, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM)== true) \
192      return NULL; \
[5327]193    return new SHELLCOMMAND<T>(commandName, className, function, d1, d2, d3, d4); \
[5153]194  }
[5161]195
[5166]196//! registers a command with 5 parameters
[5153]197#define ShellCommandRegister5(t1,t2,t3,t4,t5) \
[5327]198  static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*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]199  { \
[5161]200    if (isRegistered(commandName, className, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM)== true) \
201      return NULL; \
202    return new ShellCommand<T>(commandName, className, function, d1, d2, d3, d4, d5); \
[5153]203  }
204
205//////////////////
206// CONSTRUCTORS //
207/////////////////
[5166]208//! creates a command that takes no parameters
[5153]209#define ShellCommandConstructor0() \
[5327]210  void SHELLCOMMANDINCLASS(*functionPointer_0)(); \
211  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)()) \
[5161]212  : ShellCommandBase(commandName, className, 0) \
[5142]213  { \
[5145]214    this->functionPointer_0 = function; \
[5142]215  }
216
[5166]217//! creates a command that takes one parameter
[5145]218#define ShellCommandConstructor1(t1) \
[5327]219  void SHELLCOMMANDINCLASS(*functionPointer_1_##t1)(t1##_TYPE); \
220  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE), t1##_TYPE d1) \
[5161]221  : ShellCommandBase(commandName, className, 1, t1##_PARAM, d1) \
[5135]222  { \
[5145]223    this->functionPointer_1_##t1 = function; \
[5135]224  }
225
[5166]226//! creates a command that takes two parameters
[5153]227#define ShellCommandConstructor2(t1,t2) \
[5327]228  void SHELLCOMMANDINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE); \
229  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1, t2##_TYPE d2) \
[5161]230  : ShellCommandBase(commandName, className, 2, t1##_PARAM, d1, t2##_PARAM, d2) \
[5153]231  { \
232    this->functionPointer_2_##t1##_##t2 = function; \
233  }
[5135]234
[5166]235//! creates a command that takes three parameter
[5153]236#define ShellCommandConstructor3(t1,t2,t3) \
[5327]237  void SHELLCOMMANDINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE); \
238  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3) \
[5161]239  : ShellCommandBase(commandName, className, 3, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3) \
[5153]240  { \
241    this->functionPointer_3_##t1##_##t2##_##t3 = function; \
242  }
[5141]243
[5166]244//! creates a command that takes four parameter
[5153]245#define ShellCommandConstructor4(t1,t2,t3,t4) \
[5327]246  void SHELLCOMMANDINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE); \
247  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3, t4##_TYPE d4) \
[5161]248  : ShellCommandBase(commandName, className, 4, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4) \
[5153]249  { \
250    this->functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \
251  }
252
[5166]253//! creates a command that takes five parameter
[5153]254#define ShellCommandConstructor5(t1,t2,t3,t4,t5) \
[5327]255  void SHELLCOMMANDINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \
256  SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*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) \
[5161]257  : ShellCommandBase(commandName, className, 5, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4, t5##_PARAM, d5) \
[5153]258  { \
259    this->functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \
260  }
261
262///////////////
263// EXECUTION //
264///////////////
[5166]265//! execute-macro for functions with no parameters
[5153]266#define ShellCommandExecute0() \
[5145]267  if (this->paramCount == 0) \
[5326]268    SHELLCOMMANDEXECUTER(_0)()
[5145]269
[5166]270//! execute-macro for functions with one parameter
[5145]271#define ShellCommandExecute1(t1) \
[5146]272   else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \
[5326]273    SHELLCOMMANDEXECUTER(_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0)))
[5145]274
[5166]275//! execute-macro for functions with two parameters
[5153]276#define ShellCommandExecute2(t1,t2) \
277   else if (this->paramCount == 2 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM) \
[5326]278    SHELLCOMMANDEXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))
[5145]279
[5166]280//! execute-macro for functions with three parameters
[5153]281#define ShellCommandExecute3(t1,t2,t3) \
282   else if (this->paramCount == 3 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM) \
[5326]283    SHELLCOMMANDEXECUTER(_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)))
[5153]284
[5166]285//! execute-macro for functions with four parameters
[5153]286#define ShellCommandExecute4(t1,t2,t3,t4) \
287   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) \
[5326]288    SHELLCOMMANDEXECUTER(_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)))
[5153]289
[5166]290//! execute-macro for functions with five parameters
[5153]291#define ShellCommandExecute5(t1,t2,t3,t4,t5) \
292   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) \
[5326]293    SHELLCOMMANDEXECUTER(_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)))
[5153]294
295
[5129]296//! keeps information about a ShellCommand
297template<class T> class ShellCommand : public ShellCommandBase
298{
299  public:
[5153]300#ifdef FUNCTOR_LIST
301#undef FUNCTOR_LIST
302#endif
[5326]303#ifdef SHELLCOMMAND
304#undef SHELLCOMMAND
305#endif
[5327]306#define SHELLCOMMAND                       ShellCommand
[5326]307#ifdef SHELLCOMMANDEXECUTER
308#undef SHELLCOMMANDEXECUTER
309#endif
[5327]310#define SHELLCOMMANDEXECUTER(nameExt)      (dynamic_cast<T*>(object)->*functionPointer##nameExt)
311#ifdef SHELLCOMMANDINCLASS
312#undef SHELLCOMMANDINCLASS
313#endif
314#define SHELLCOMMANDINCLASS(FUNCTION)      (T::FUNCTION)
[5153]315
[5166]316//! FUNCTOR_LIST is the List of command-registerers
[5142]317#define FUNCTOR_LIST(x) ShellCommandRegister ## x
[5153]318#include "functor_list.h"
[5142]319#undef FUNCTOR_LIST
[5136]320
[5142]321
[5068]322  private:
[5166]323//! FUNCTOR_LIST is the List of CommandConstructors
[5142]324#define FUNCTOR_LIST(x) ShellCommandConstructor ## x
[5153]325#include "functor_list.h"
[5142]326#undef FUNCTOR_LIST
327
328    virtual void executeCommand (BaseObject* object, const char* parameters)
[5135]329    {
[5204]330      SubString sub(parameters, true);
[5166]331//! FUNCTOR_LIST is the List of Executive Functions
[5145]332#define FUNCTOR_LIST(x) ShellCommandExecute ## x
[5153]333#include "functor_list.h"
[5135]334#undef FUNCTOR_LIST
[5145]335    }
[5129]336};
[5113]337
[5327]338//! keeps information about a ShellCommand, that points to a Static Function
[5326]339template<class T> class ShellCommandStatic : public ShellCommandBase
340{
341  public:
342#ifdef FUNCTOR_LIST
343#undef FUNCTOR_LIST
344#endif
345#ifdef SHELLCOMMAND
346#undef SHELLCOMMAND
347#endif
348#define SHELLCOMMAND                      ShellCommandStatic
349#ifdef SHELLCOMMANDEXECUTER
350#undef SHELLCOMMANDEXECUTER
351#endif
352#define SHELLCOMMANDEXECUTER(nameExt)     T::functionPointer##nameExt
[5327]353#ifdef SHELLCOMMANDINCLASS
354#undef SHELLCOMMANDINCLASS
355#endif
356#define SHELLCOMMANDINCLASS(FUNCTION)     (FUNCTION)
[5326]357
358//! FUNCTOR_LIST is the List of command-registerers
359#define FUNCTOR_LIST(x) ShellCommandRegister ## x
360#include "functor_list.h"
361#undef FUNCTOR_LIST
362
363  private:
364//! FUNCTOR_LIST is the List of CommandConstructors
365#define FUNCTOR_LIST(x) ShellCommandConstructor ## x
366#include "functor_list.h"
367#undef FUNCTOR_LIST
368
369    virtual void executeCommand (BaseObject* object, const char* parameters)
370    {
371  SubString sub(parameters, true);
372//! FUNCTOR_LIST is the List of Executive Functions
373#define FUNCTOR_LIST(x) ShellCommandExecute ## x
374#include "functor_list.h"
375#undef FUNCTOR_LIST
376    }
377};
378
[5197]379//! A Class, that handles aliases.
[5190]380class ShellCommandAlias
381{
[5196]382  friend class ShellCommandBase;
[5190]383  public:
[5197]384    /** @returns the Name of the Alias. */
[5195]385    const char* getName() const { return this->aliasName; };
[5197]386    /** @returns the Command, this Alias is asociated with */
[5195]387    ShellCommandBase* getCommand() const { return this->command; };
[5196]388
[5190]389  private:
[5197]390    /** @param aliasName the name of the Alias @param command the Command, to associate this alias with */
[5196]391    ShellCommandAlias(const char* aliasName, ShellCommandBase* command) { this->aliasName = aliasName; this->command = command; };
392
393  private:
[5197]394    const char*         aliasName;       //!< the name of the Alias
395    ShellCommandBase*   command;         //!< a pointer to the command, this alias executes.
[5190]396};
397
[5129]398#endif /* _SHELL_COMMAND_H */
Note: See TracBrowser for help on using the repository browser.