/*! * @file shell_command.h * Definition of a on-screen-shell */ #ifndef _SHELL_COMMAND_H #define _SHELL_COMMAND_H #include "base_object.h" #include "helper_functions.h" #include "functor_list.h" #include "substring.h" #include #define SHELL_COMMAND_MAX_SIZE // FORWARD DECLARATION template class tList; /////////////////////// // MACRO DEFINITIONS // /////////////////////// // COMMAND REGISTRATION // NO ARGUMENTS #define ShellCommandRegister0 \ static void registerCommand(const char* commandName, ClassID classID, void (T::*function)()) \ { \ if (isRegistered(commandName, classID, 0)== true) \ return; \ new ShellCommand(commandName, classID, function); \ } #define ShellCommandRegister1(t1) \ static void registerCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \ { \ if (isRegistered(commandName, classID, 1, t1##_PARAM)== true) \ return; \ new ShellCommand(commandName, classID, function, d1); \ } // CONSTRUCTORS #define ShellCommandConstructor0 \ void (T::*functionPointer_0)(); \ ShellCommand(const char* commandName, ClassID classID, void (T::*function)()) \ : ShellCommandBase(commandName, classID, 0) \ { \ this->functionPointer_0 = function; \ } #define ShellCommandConstructor1(t1) \ void (T::*functionPointer_1_##t1)(t1##_TYPE); \ ShellCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE), t1##_TYPE d1) \ : ShellCommandBase(commandName, classID, 1, t1##_PARAM, d1) \ { \ this->functionPointer_1_##t1 = function; \ } #define ShellCommandExecute0 \ if (this->paramCount == 0) \ (dynamic_cast(object)->*functionPointer_0)() #define ShellCommandExecute1(t1) \ else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \ (dynamic_cast(object)->*functionPointer_1_##t1)(t1##_FUNC(parameters, 0)) //////////////// // BASE CLASS // //////////////// //! a baseClass for all possible ShellCommands class ShellCommandBase : public BaseObject { public: static bool execute (const char* executionString); static const tList* getCommandList() { return ShellCommandBase::commandList; }; protected: ShellCommandBase(const char* commandName, ClassID classID, unsigned int paramCount, ...); ~ShellCommandBase(); static bool isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...); static const char* paramToString(long parameter); void debug(); private: virtual void executeCommand (BaseObject* object, const char* parameters) = NULL; protected: void* functionPointer; //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL ) unsigned int paramCount; //!< the count of parameters unsigned int* parameters; //!< Parameters bool isSingleton; //!< if the Class is Singleton @todo autocheck private: long classID; //!< The ID of the Class associated to this Command const char* className; //!< The Name of the Class associated to this Command char* defaultStrings[FUNCTOR_MAX_ARGUMENTS]; int defaultInts[FUNCTOR_MAX_ARGUMENTS]; float defaultFloats[FUNCTOR_MAX_ARGUMENTS]; bool defaultBools[FUNCTOR_MAX_ARGUMENTS]; // STATIC MEMBERS static tList* commandList; //!< A list of availiable commands. }; template class ShellCommand; /* template class ShellCommandSingleton : public ShellCommandBase { friend class ShellCommand; private: ShellCommandSingleton(const char* commandName, ClassID classID, void (T::*functionPointer)()) : ShellCommandBase (commandName, classID, 0) { this->functionPointer_NULL = functionPointer; } void (T::*functionPointer_NULL)(); virtual void executeCommand (const char* parameters) { if (this->paramCount == 0) (T::getInstance()->*functionPointer_NULL)(); } };*/ //! keeps information about a ShellCommand template class ShellCommand : public ShellCommandBase { public: static void unregisterCommand(const char* commandNaame, ClassID classID); #define FUNCTOR_LIST(x) ShellCommandRegister ## x //#include "functor_list.h" FUNCTOR_LIST(0); FUNCTOR_LIST(1)(l_INT); FUNCTOR_LIST(1)(l_STRING); #undef FUNCTOR_LIST private: #define FUNCTOR_LIST(x) ShellCommandConstructor ## x //#include "functor_list.h" FUNCTOR_LIST(0); FUNCTOR_LIST(1)(l_INT); FUNCTOR_LIST(1)(l_STRING); #undef FUNCTOR_LIST virtual void executeCommand (BaseObject* object, const char* parameters) { if (parameters != NULL) //SubString params(parameters, ','); #define FUNCTOR_LIST(x) ShellCommandExecute ## x //#include "functor_list.h" FUNCTOR_LIST(0); FUNCTOR_LIST(1)(l_INT); //FUNCTOR_LIST(1)(l_STRING); #undef FUNCTOR_LIST } }; #endif /* _SHELL_COMMAND_H */