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