Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: flush

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