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