Changeset 5636 in orxonox.OLD for trunk/src/lib/shell/shell_command.h
- Timestamp:
- Nov 18, 2005, 6:55:18 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/shell/shell_command.h
r5552 r5636 13 13 #include "substring.h" 14 14 #include "functor_list.h" 15 15 #include "executor/executor.h" 16 16 #include <stdarg.h> 17 17 … … 30 30 * 31 31 * MEANING: 32 * ShellCommand Base* someUniqueVarName =32 * ShellCommand* someUniqueVarName = 33 33 * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); 34 34 * … … 36 36 * $ ClassName [ObjectName] commandNameInShell [parameters] 37 37 */ 38 //#define SHELL_COMMAND(command, class, function) \ 39 // ShellCommand* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function) 38 40 #define SHELL_COMMAND(command, class, function) \ 39 ShellCommandBase* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function) 41 ShellCommand* shell_command_##class##_##command = ShellCommand::registerCommand(#command, #class, new ExecutorObjective<class>(&class::function)) 42 40 43 /** 41 44 * an easy to use Macro to create a Command … … 45 48 * 46 49 * MEANING: 47 * ShellCommand Base* someUniqueVarName =50 * ShellCommand* someUniqueVarName = 48 51 * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); 49 52 * … … 52 55 */ 53 56 #define SHELL_COMMAND_STATIC(command, class, function) \ 54 ShellCommandBase* shell_command_##class##_##command = ShellCommandStatic<class>::registerCommand(#command, #class, function)57 ShellCommand* shell_command_##class##_##command = ShellCommand::registerCommand(#command, #class, new ExecutorStatic<class>(function)) 55 58 56 59 … … 64 67 // BASE CLASS // 65 68 //////////////// 66 class ShellCommand Base;69 class ShellCommand; 67 70 class ShellCommandAlias; 68 71 … … 70 73 class ShellCommandClass : public BaseObject 71 74 { 72 friend class ShellCommand Base;75 friend class ShellCommand; 73 76 74 77 public: … … 93 96 const char* className; //!< The Name of the Class. This should match the ClassName of the Commands Class. 94 97 long classID; //!< The classID of this Class 95 tList<ShellCommand Base>* commandList; //!< A list of Commands from this Class98 tList<ShellCommand>* commandList; //!< A list of Commands from this Class 96 99 static tList<ShellCommandClass>* commandClassList; //!< A list of Classes 97 100 static tList<ShellCommandAlias>* aliasList; //!< An Alias to A Command. (only for classes with one Instance) … … 100 103 101 104 //! a baseClass for all possible ShellCommands 102 class ShellCommand Base: public BaseObject105 class ShellCommand : public BaseObject 103 106 { 104 107 friend class ShellCommandClass; … … 106 109 static bool execute (const char* executionString); 107 110 108 ShellCommand Base* describe(const char* description);109 ShellCommand Base* setAlias(const char* alias);110 ShellCommand Base* defaultValues(unsigned int count, ...);111 ShellCommand* describe(const char* description); 112 ShellCommand* setAlias(const char* alias); 113 ShellCommand* defaultValues(unsigned int count, ...); 111 114 112 /** @returns the CommandList of the Shell */ 115 static ShellCommand* registerCommand(const char* commandName, const char* className, Executor* executor); 116 113 117 static void unregisterCommand(const char* commandName, const char* className); 114 118 … … 116 120 117 121 protected: 118 ShellCommand Base(const char* commandName, const char* className, unsigned int paramCount, ...);119 ~ShellCommand Base();122 ShellCommand(const char* commandName, const char* className, unsigned int paramCount, ...); 123 ~ShellCommand(); 120 124 121 125 /** @returns the Type of this Function (either static or objective) */ … … 141 145 142 146 const char* description; //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla"); 143 };147 Executor* executor; 144 148 145 ///////////////////////////////////////////////////146 ///////////////////////////////////////////////////147 148 /////////////////////////////////149 // MACRO DEFINITION EXTENSIONS //150 /////////////////////////////////151 //! where to chek for default BOOL values152 #define l_BOOL_DEFGRAB(i) this->defaultValue[i].getBool()153 //! where to chek for default INT values154 #define l_INT_DEFGRAB(i) this->defaultValue[i].getInt()155 //! where to chek for default UINT values156 #define l_UINT_DEFGRAB(i) (unsigned int)this->defaultValue[i].getInt()157 //! where to chek for default LONG values158 #define l_LONG_DEFGRAB(i) (long)this->defaultValue[i].getInt()159 //! where to chek for default FLOAT values160 #define l_FLOAT_DEFGRAB(i) this->defaultValue[i].getFloat()161 //! where to chek for default STRING values162 #define l_STRING_DEFGRAB(i) this->defaultValue[i].getString()163 164 //////////////////////////165 // COMMAND REGISTRATION //166 //////////////////////////167 // SHELLCOMMAND can be redefined as ShellCommand or ShellCommandStatic168 // SHELLCOMMANDEXECUTER can be redefined too.169 // SHELLCOMMANDINCLASS170 // SHELLCOMMANDTYPE171 //! registers a command without any parameters172 #define ShellCommandRegister0() \173 static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)()) \174 { \175 if (isRegistered(commandName, className, 0)== true) \176 return NULL; \177 return new SHELLCOMMAND<T>(commandName, className, function); \178 }179 180 //! registers a command with 1 parameter181 #define ShellCommandRegister1(t1) \182 static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE)) \183 { \184 if (isRegistered(commandName, className, 1, t1##_PARAM)== true) \185 return NULL; \186 return new SHELLCOMMAND<T>(commandName, className, function); \187 }188 189 //! registers a command with 2 parameters190 #define ShellCommandRegister2(t1,t2) \191 static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \192 { \193 if (isRegistered(commandName, className, 2, t1##_PARAM, t2##_PARAM)== true) \194 return NULL; \195 return new SHELLCOMMAND<T>(commandName, className, function); \196 }197 198 //! registers a command with 3 parameters199 #define ShellCommandRegister3(t1,t2,t3) \200 static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \201 { \202 if (isRegistered(commandName, className, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM)== true) \203 return NULL; \204 return new SHELLCOMMAND<T>(commandName, className, function); \205 }206 207 //! registers a command with 4 parameters208 #define ShellCommandRegister4(t1,t2,t3,t4) \209 static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \210 { \211 if (isRegistered(commandName, className, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM)== true) \212 return NULL; \213 return new SHELLCOMMAND<T>(commandName, className, function); \214 }215 216 //! registers a command with 5 parameters217 #define ShellCommandRegister5(t1,t2,t3,t4,t5) \218 static SHELLCOMMAND<T>* registerCommand(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \219 { \220 if (isRegistered(commandName, className, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM)== true) \221 return NULL; \222 return new ShellCommand<T>(commandName, className, function); \223 }224 225 ///////////////////////226 // FUNCTION POINTERS //227 ///////////////////////228 #define ShellCommandFunctionPoiter0() \229 void SHELLCOMMANDINCLASS(*functionPointer_0)();230 231 #define ShellCommandFunctionPoiter1(t1) \232 void SHELLCOMMANDINCLASS(*functionPointer_1_##t1)(t1##_TYPE);233 234 #define ShellCommandFunctionPoiter2(t1, t2) \235 void SHELLCOMMANDINCLASS(*functionPointer_2_##t1##_##t2)(t1##_TYPE, t2##_TYPE);236 237 238 #define ShellCommandFunctionPoiter3(t1, t2, t3) \239 void SHELLCOMMANDINCLASS(*functionPointer_3_##t1##_##t2##_##t3)(t1##_TYPE, t2##_TYPE, t3##_TYPE);240 241 #define ShellCommandFunctionPoiter4(t1, t2, t3, t4) \242 void SHELLCOMMANDINCLASS(*functionPointer_4_##t1##_##t2##_##t3##_##t4)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE);243 244 245 #define ShellCommandFunctionPoiter5(t1, t2, t3, t4, t5) \246 void SHELLCOMMANDINCLASS(*functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE); \247 248 249 //////////////////250 // CONSTRUCTORS //251 /////////////////252 //! creates a command that takes no parameters253 #define ShellCommandConstructor0() \254 SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)()) \255 : ShellCommandBase(commandName, className, 0) \256 { \257 this->functorType = SHELLCOMMANDTYPE; \258 this->fp.functionPointer_0 = function; \259 }260 261 //! creates a command that takes one parameter262 #define ShellCommandConstructor1(t1) \263 SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE)) \264 : ShellCommandBase(commandName, className, 1, t1##_PARAM) \265 { \266 this->functorType = SHELLCOMMANDTYPE; \267 this->fp.functionPointer_1_##t1 = function; \268 }269 270 //! creates a command that takes two parameters271 #define ShellCommandConstructor2(t1,t2) \272 SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE)) \273 : ShellCommandBase(commandName, className, 2, t1##_PARAM, t2##_PARAM) \274 { \275 this->functorType = SHELLCOMMANDTYPE; \276 this->fp.functionPointer_2_##t1##_##t2 = function; \277 }278 279 //! creates a command that takes three parameter280 #define ShellCommandConstructor3(t1,t2,t3) \281 SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE)) \282 : ShellCommandBase(commandName, className, 3, t1##_PARAM, t2##_PARAM, t3##_PARAM) \283 { \284 this->functorType = SHELLCOMMANDTYPE; \285 this->fp.functionPointer_3_##t1##_##t2##_##t3 = function; \286 }287 288 //! creates a command that takes four parameter289 #define ShellCommandConstructor4(t1,t2,t3,t4) \290 SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE)) \291 : ShellCommandBase(commandName, className, 4, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM) \292 { \293 this->functorType = SHELLCOMMANDTYPE; \294 this->fp.functionPointer_4_##t1##_##t2##_##t3##_##t4 = function; \295 }296 297 //! creates a command that takes five parameter298 #define ShellCommandConstructor5(t1,t2,t3,t4,t5) \299 SHELLCOMMAND(const char* commandName, const char* className, void SHELLCOMMANDINCLASS(*function)(t1##_TYPE, t2##_TYPE, t3##_TYPE, t4##_TYPE, t5##_TYPE)) \300 : ShellCommandBase(commandName, className, 5, t1##_PARAM, t2##_PARAM, t3##_PARAM, t4##_PARAM, t5##_PARAM) \301 { \302 this->functorType = SHELLCOMMANDTYPE; \303 this->fp.functionPointer_5_##t1##_##t2##_##t3##_##t4##_##t5 = function; \304 }305 306 ///////////////307 // EXECUTION //308 ///////////////309 //! execute-macro for functions with no parameters310 #define ShellCommandExecute0() \311 if (this->paramCount == 0) \312 SHELLCOMMANDEXECUTER(_0)()313 314 //! execute-macro for functions with one parameter315 #define ShellCommandExecute1(t1) \316 else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \317 SHELLCOMMANDEXECUTER(_1_##t1)(t1##_FUNC(parameters, t1##_DEFGRAB(0)))318 319 //! execute-macro for functions with two parameters320 #define ShellCommandExecute2(t1,t2) \321 else if (this->paramCount == 2 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM) \322 SHELLCOMMANDEXECUTER(_2_##t1##_##t2)(t1##_FUNC(sub.getString(0), t1##_DEFGRAB(0)), t2##_FUNC(sub.getString(1), t2##_DEFGRAB(1)))323 324 //! execute-macro for functions with three parameters325 #define ShellCommandExecute3(t1,t2,t3) \326 else if (this->paramCount == 3 && this->parameters[0] == t1##_PARAM && this->parameters[1] == t2##_PARAM && this->parameters[2] == t3##_PARAM) \327 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)))328 329 //! execute-macro for functions with four parameters330 #define ShellCommandExecute4(t1,t2,t3,t4) \331 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) \332 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)))333 334 //! execute-macro for functions with five parameters335 #define ShellCommandExecute5(t1,t2,t3,t4,t5) \336 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) \337 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)))338 339 340 //! keeps information about a ShellCommand341 template<class T> class ShellCommand : public ShellCommandBase342 {343 public:344 #ifdef FUNCTOR_LIST345 #undef FUNCTOR_LIST346 #endif347 #ifdef SHELLCOMMAND348 #undef SHELLCOMMAND349 #endif350 #define SHELLCOMMAND ShellCommand351 #ifdef SHELLCOMMANDEXECUTER352 #undef SHELLCOMMANDEXECUTER353 #endif354 #define SHELLCOMMANDEXECUTER(nameExt) (dynamic_cast<T*>(object)->*(fp.functionPointer##nameExt))355 #ifdef SHELLCOMMANDINCLASS356 #undef SHELLCOMMANDINCLASS357 #endif358 #define SHELLCOMMANDINCLASS(FUNCTION) (T::FUNCTION)359 #ifdef SHELLCOMMANDTYPE360 #undef SHELLCOMMANDTYPE361 #endif362 #define SHELLCOMMANDTYPE ShellCommand_Objective363 //! FUNCTOR_LIST is the List of command-registerers364 #define FUNCTOR_LIST(x) ShellCommandRegister ## x365 #include "functor_list.h"366 #undef FUNCTOR_LIST367 368 369 private:370 //! FUNCTOR_LIST is the List of FunctionPointers371 union FunctionPointers {372 #define FUNCTOR_LIST(x) ShellCommandFunctionPoiter ## x373 #include "functor_list.h"374 #undef FUNCTOR_LIST375 } fp;376 377 //! FUNCTOR_LIST is the List of CommandConstructors378 #define FUNCTOR_LIST(x) ShellCommandConstructor ## x379 #include "functor_list.h"380 #undef FUNCTOR_LIST381 382 virtual void executeCommand (BaseObject* object, const char* parameters)383 {384 SubString sub(parameters, true);385 //! FUNCTOR_LIST is the List of Executive Functions386 #define FUNCTOR_LIST(x) ShellCommandExecute ## x387 #include "functor_list.h"388 #undef FUNCTOR_LIST389 }390 };391 392 //! keeps information about a ShellCommand, that points to a Static Function393 template<class T> class ShellCommandStatic : public ShellCommandBase394 {395 public:396 #ifdef FUNCTOR_LIST397 #undef FUNCTOR_LIST398 #endif399 #ifdef SHELLCOMMAND400 #undef SHELLCOMMAND401 #endif402 #define SHELLCOMMAND ShellCommandStatic403 #ifdef SHELLCOMMANDEXECUTER404 #undef SHELLCOMMANDEXECUTER405 #endif406 #define SHELLCOMMANDEXECUTER(nameExt) fp.functionPointer##nameExt407 #ifdef SHELLCOMMANDINCLASS408 #undef SHELLCOMMANDINCLASS409 #endif410 #define SHELLCOMMANDINCLASS(FUNCTION) (FUNCTION)411 #ifdef SHELLCOMMANDTYPE412 #undef SHELLCOMMANDTYPE413 #endif414 #define SHELLCOMMANDTYPE ShellCommand_Static415 416 //! FUNCTOR_LIST is the List of command-registerers417 #define FUNCTOR_LIST(x) ShellCommandRegister ## x418 #include "functor_list.h"419 #undef FUNCTOR_LIST420 421 private:422 //! FUNCTOR_LIST is the List of FunctionPointers423 union FunctionPointers {424 #define FUNCTOR_LIST(x) ShellCommandFunctionPoiter ## x425 #include "functor_list.h"426 #undef FUNCTOR_LIST427 } fp;428 429 //! FUNCTOR_LIST is the List of CommandConstructors430 #define FUNCTOR_LIST(x) ShellCommandConstructor ## x431 #include "functor_list.h"432 #undef FUNCTOR_LIST433 434 virtual void executeCommand (BaseObject* object, const char* parameters)435 {436 SubString sub(parameters, true);437 //! FUNCTOR_LIST is the List of Executive Functions438 #define FUNCTOR_LIST(x) ShellCommandExecute ## x439 #include "functor_list.h"440 #undef FUNCTOR_LIST441 }442 149 }; 443 150 … … 445 152 class ShellCommandAlias 446 153 { 447 friend class ShellCommand Base;154 friend class ShellCommand; 448 155 public: 449 156 /** @returns the Name of the Alias. */ 450 157 const char* getName() const { return this->aliasName; }; 451 158 /** @returns the Command, this Alias is asociated with */ 452 ShellCommand Base* getCommand() const { return this->command; };159 ShellCommand* getCommand() const { return this->command; }; 453 160 454 161 private: 455 162 /** @param aliasName the name of the Alias @param command the Command, to associate this alias with */ 456 ShellCommandAlias(const char* aliasName, ShellCommand Base* command) { this->aliasName = aliasName; this->command = command; };163 ShellCommandAlias(const char* aliasName, ShellCommand* command) { this->aliasName = aliasName; this->command = command; }; 457 164 458 165 private: 459 166 const char* aliasName; //!< the name of the Alias 460 ShellCommand Base* command; //!< a pointer to the command, this alias executes.167 ShellCommand* command; //!< a pointer to the command, this alias executes. 461 168 }; 462 169
Note: See TracChangeset
for help on using the changeset viewer.