| 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 "substring.h" | 
|---|
| 13 | #include "functor_list.h" | 
|---|
| 14 |  | 
|---|
| 15 | #include <stdarg.h> | 
|---|
| 16 |  | 
|---|
| 17 | #define SHELL_COMMAND_MAX_SIZE | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | // FORWARD DECLARATION | 
|---|
| 22 | template<class T> class tList; | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | /////////////////////// | 
|---|
| 26 | // MACRO DEFINITIONS // | 
|---|
| 27 | /////////////////////// | 
|---|
| 28 | #define   l_BOOL_DEFGRAB(i)         this->defaultBools[i] | 
|---|
| 29 | #define   l_INT_DEFGRAB(i)          this->defaultInts[i] | 
|---|
| 30 | #define   l_UINT_DEFGRAB(i)         (unsigned int)this->defaultInts[i] | 
|---|
| 31 | #define   l_LONG_DEFGRAB(i)         (long)this->defaultInts[i] | 
|---|
| 32 | #define   l_FLOAT_DEFGRAB(i)        this->defaultFloats[i] | 
|---|
| 33 | #define   l_STRING_DEFGRAB(i)       this->defaultStrings[i] | 
|---|
| 34 |  | 
|---|
| 35 | ////////////////////////// | 
|---|
| 36 | // COMMAND REGISTRATION // | 
|---|
| 37 | ////////////////////////// | 
|---|
| 38 | // NO ARGUMENTS | 
|---|
| 39 | #define ShellCommandRegister0() \ | 
|---|
| 40 |   static void registerCommand(const char* commandName, ClassID classID, void (T::*function)()) \ | 
|---|
| 41 |   { \ | 
|---|
| 42 |     if (isRegistered(commandName, classID, 0)== true) \ | 
|---|
| 43 |       return; \ | 
|---|
| 44 |     new ShellCommand<T>(commandName, classID, function); \ | 
|---|
| 45 |   } | 
|---|
| 46 |  | 
|---|
| 47 | #define ShellCommandRegister1(t1) \ | 
|---|
| 48 |   static void registerCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \ | 
|---|
| 49 |   { \ | 
|---|
| 50 |     if (isRegistered(commandName, classID, 1, t1##_PARAM)== true) \ | 
|---|
| 51 |       return; \ | 
|---|
| 52 |     new ShellCommand<T>(commandName, classID, function, d1); \ | 
|---|
| 53 |   } | 
|---|
| 54 |  | 
|---|
| 55 | #define ShellCommandRegister2(t1,t2) \ | 
|---|
| 56 |   static void registerCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT) \ | 
|---|
| 57 |   { \ | 
|---|
| 58 |     if (isRegistered(commandName, classID, 2, t1##_PARAM, t2##_PARAM)== true) \ | 
|---|
| 59 |       return; \ | 
|---|
| 60 |     new ShellCommand<T>(commandName, classID, function, d1, d2); \ | 
|---|
| 61 |   } | 
|---|
| 62 |  | 
|---|
| 63 | #define ShellCommandRegister3(t1,t2,t3) \ | 
|---|
| 64 |   static void registerCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1 = t1##_DEFAULT, t2##_TYPE d2 = t2##_DEFAULT, t3##_TYPE d3 = t3##_DEFAULT) \ | 
|---|
| 65 |   { \ | 
|---|
| 66 |     if (isRegistered(commandName, classID, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM)== true) \ | 
|---|
| 67 |       return; \ | 
|---|
| 68 |     new ShellCommand<T>(commandName, classID, function, d1, d2, d3); \ | 
|---|
| 69 |   } | 
|---|
| 70 |  | 
|---|
| 71 | #define ShellCommandRegister4(t1,t2,t3,t4) \ | 
|---|
| 72 |   static void registerCommand(const char* commandName, ClassID classID, 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) \ | 
|---|
| 73 |   { \ | 
|---|
| 74 |     if (isRegistered(commandName, classID, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM)== true) \ | 
|---|
| 75 |       return; \ | 
|---|
| 76 |     new ShellCommand<T>(commandName, classID, function, d1, d2, d3, d4); \ | 
|---|
| 77 |   } | 
|---|
| 78 | #define ShellCommandRegister5(t1,t2,t3,t4,t5) \ | 
|---|
| 79 |   static void registerCommand(const char* commandName, ClassID classID, 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) \ | 
|---|
| 80 |   { \ | 
|---|
| 81 |     if (isRegistered(commandName, classID, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM)== true) \ | 
|---|
| 82 |       return; \ | 
|---|
| 83 |     new ShellCommand<T>(commandName, classID, function, d1, d2, d3, d4, d5); \ | 
|---|
| 84 |   } | 
|---|
| 85 |  | 
|---|
| 86 | ////////////////// | 
|---|
| 87 | // CONSTRUCTORS // | 
|---|
| 88 | ///////////////// | 
|---|
| 89 | #define ShellCommandConstructor0() \ | 
|---|
| 90 |   void (T::*functionPointer_0)(); \ | 
|---|
| 91 |   ShellCommand(const char* commandName, ClassID classID, void (T::*function)()) \ | 
|---|
| 92 |   : ShellCommandBase(commandName, classID, 0) \ | 
|---|
| 93 |   { \ | 
|---|
| 94 |     this->functionPointer_0 = function; \ | 
|---|
| 95 |   } | 
|---|
| 96 |  | 
|---|
| 97 | #define ShellCommandConstructor1(t1) \ | 
|---|
| 98 |   void (T::*functionPointer_1_##t1)(t1##_TYPE); \ | 
|---|
| 99 |   ShellCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE), t1##_TYPE d1) \ | 
|---|
| 100 |   : ShellCommandBase(commandName, classID, 1, t1##_PARAM, d1) \ | 
|---|
| 101 |   { \ | 
|---|
| 102 |     this->functionPointer_1_##t1 = function; \ | 
|---|
| 103 |   } | 
|---|
| 104 |  | 
|---|
| 105 | #define ShellCommandConstructor2(t1,t2) \ | 
|---|
| 106 |   void (T::*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE); \ | 
|---|
| 107 |   ShellCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE, t2##_TYPE), t1##_TYPE d1, t2##_TYPE d2) \ | 
|---|
| 108 |   : ShellCommandBase(commandName, classID, 2, t1##_PARAM, d1, t2##_PARAM, d2) \ | 
|---|
| 109 |   { \ | 
|---|
| 110 |     this->functionPointer_2_##t1##_##t2 = function; \ | 
|---|
| 111 |   } | 
|---|
| 112 |  | 
|---|
| 113 | #define ShellCommandConstructor3(t1,t2,t3) \ | 
|---|
| 114 |   void (T::*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE); \ | 
|---|
| 115 |   ShellCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3) \ | 
|---|
| 116 |   : ShellCommandBase(commandName, classID, 3, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3) \ | 
|---|
| 117 |   { \ | 
|---|
| 118 |     this->functionPointer_3_##t1##_##t2##_##t3 = function; \ | 
|---|
| 119 |   } | 
|---|
| 120 |  | 
|---|
| 121 | #define ShellCommandConstructor4(t1,t2,t3,t4) \ | 
|---|
| 122 |   void (T::*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE); \ | 
|---|
| 123 |   ShellCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE), t1##_TYPE d1, t2##_TYPE d2, t3##_TYPE d3, t4##_TYPE d4) \ | 
|---|
| 124 |   : ShellCommandBase(commandName, classID, 4, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4) \ | 
|---|
| 125 |   { \ | 
|---|
| 126 |     this->functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \ | 
|---|
| 127 |   } | 
|---|
| 128 |  | 
|---|
| 129 | #define ShellCommandConstructor5(t1,t2,t3,t4,t5) \ | 
|---|
| 130 |   void (T::*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \ | 
|---|
| 131 |   ShellCommand(const char* commandName, ClassID classID, 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) \ | 
|---|
| 132 |   : ShellCommandBase(commandName, classID, 5, t1##_PARAM, d1, t2##_PARAM, d2, t3##_PARAM, d3, t4##_PARAM, d4, t5##_PARAM, d5) \ | 
|---|
| 133 |   { \ | 
|---|
| 134 |     this->functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \ | 
|---|
| 135 |   } | 
|---|
| 136 |  | 
|---|
| 137 | /////////////// | 
|---|
| 138 | // EXECUTION // | 
|---|
| 139 | /////////////// | 
|---|
| 140 | #define ShellCommandExecute0() \ | 
|---|
| 141 |   if (this->paramCount == 0) \ | 
|---|
| 142 |     (dynamic_cast<T*>(object)->*functionPointer_0)() | 
|---|
| 143 |  | 
|---|
| 144 | #define ShellCommandExecute1(t1) \ | 
|---|
| 145 |    else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \ | 
|---|
| 146 |     (dynamic_cast<T*>(object)->*functionPointer_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0))) | 
|---|
| 147 |  | 
|---|
| 148 | #define ShellCommandExecute2(t1,t2) \ | 
|---|
| 149 |    else if (this->paramCount == 2 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM) \ | 
|---|
| 150 |     (dynamic_cast<T*>(object)->*functionPointer_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1))) | 
|---|
| 151 |  | 
|---|
| 152 | #define ShellCommandExecute3(t1,t2,t3) \ | 
|---|
| 153 |    else if (this->paramCount == 3 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM) \ | 
|---|
| 154 |     (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))) | 
|---|
| 155 |  | 
|---|
| 156 | #define ShellCommandExecute4(t1,t2,t3,t4) \ | 
|---|
| 157 |    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) \ | 
|---|
| 158 |     (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))) | 
|---|
| 159 |  | 
|---|
| 160 | #define ShellCommandExecute5(t1,t2,t3,t4,t5) \ | 
|---|
| 161 |    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) \ | 
|---|
| 162 |     (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))) | 
|---|
| 163 |  | 
|---|
| 164 |  | 
|---|
| 165 |  | 
|---|
| 166 | //////////////// | 
|---|
| 167 | // BASE CLASS // | 
|---|
| 168 | //////////////// | 
|---|
| 169 | //! a baseClass for all possible ShellCommands | 
|---|
| 170 | class ShellCommandBase : public BaseObject | 
|---|
| 171 | { | 
|---|
| 172 |   public: | 
|---|
| 173 |     static bool execute (const char* executionString); | 
|---|
| 174 |  | 
|---|
| 175 |     static const tList<ShellCommandBase>* getCommandList() { return ShellCommandBase::commandList; }; | 
|---|
| 176 |  | 
|---|
| 177 |  | 
|---|
| 178 |   protected: | 
|---|
| 179 |     ShellCommandBase(const char* commandName, ClassID classID, unsigned int paramCount, ...); | 
|---|
| 180 |     ~ShellCommandBase(); | 
|---|
| 181 |  | 
|---|
| 182 |     static bool isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...); | 
|---|
| 183 |     static const char* paramToString(long parameter); | 
|---|
| 184 |  | 
|---|
| 185 |     void debug(); | 
|---|
| 186 |   private: | 
|---|
| 187 |     virtual void executeCommand (BaseObject* object, const char* parameters) = NULL; | 
|---|
| 188 |  | 
|---|
| 189 |   protected: | 
|---|
| 190 |     void*                            functionPointer;     //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL ) | 
|---|
| 191 |     unsigned int                     paramCount;          //!< the count of parameters | 
|---|
| 192 |     unsigned int*                    parameters;          //!< Parameters | 
|---|
| 193 |     bool                             isSingleton;         //!< if the Class is Singleton @todo autocheck | 
|---|
| 194 |     char*                            defaultStrings[FUNCTOR_MAX_ARGUMENTS]; | 
|---|
| 195 |     int                              defaultInts[FUNCTOR_MAX_ARGUMENTS]; | 
|---|
| 196 |     float                            defaultFloats[FUNCTOR_MAX_ARGUMENTS]; | 
|---|
| 197 |     bool                             defaultBools[FUNCTOR_MAX_ARGUMENTS]; | 
|---|
| 198 |  | 
|---|
| 199 |   private: | 
|---|
| 200 |     long                             classID;             //!< The ID of the Class associated to this Command | 
|---|
| 201 |     const char*                      className;           //!< The Name of the Class associated to this Command | 
|---|
| 202 |  | 
|---|
| 203 |     // STATIC MEMBERS | 
|---|
| 204 |     static tList<ShellCommandBase>*  commandList;         //!< A list of availiable commands. | 
|---|
| 205 | }; | 
|---|
| 206 |  | 
|---|
| 207 | //! keeps information about a ShellCommand | 
|---|
| 208 | template<class T> class ShellCommand : public ShellCommandBase | 
|---|
| 209 | { | 
|---|
| 210 |   public: | 
|---|
| 211 |     static void unregisterCommand(const char* commandNaame, ClassID classID); | 
|---|
| 212 |  | 
|---|
| 213 | #ifdef FUNCTOR_LIST | 
|---|
| 214 | #undef FUNCTOR_LIST | 
|---|
| 215 | #endif | 
|---|
| 216 |  | 
|---|
| 217 | #define FUNCTOR_LIST(x) ShellCommandRegister ## x | 
|---|
| 218 | #include "functor_list.h" | 
|---|
| 219 | #undef FUNCTOR_LIST | 
|---|
| 220 |  | 
|---|
| 221 |  | 
|---|
| 222 |   private: | 
|---|
| 223 | #define FUNCTOR_LIST(x) ShellCommandConstructor ## x | 
|---|
| 224 | #include "functor_list.h" | 
|---|
| 225 | #undef FUNCTOR_LIST | 
|---|
| 226 |  | 
|---|
| 227 |     virtual void executeCommand (BaseObject* object, const char* parameters) | 
|---|
| 228 |     { | 
|---|
| 229 | //      if (parameters != NULL) | 
|---|
| 230 |       SubString sub(parameters); | 
|---|
| 231 | #define FUNCTOR_LIST(x) ShellCommandExecute ## x | 
|---|
| 232 | #include "functor_list.h" | 
|---|
| 233 | #undef FUNCTOR_LIST | 
|---|
| 234 |     } | 
|---|
| 235 | }; | 
|---|
| 236 |  | 
|---|
| 237 | #endif /* _SHELL_COMMAND_H */ | 
|---|