[4838] | 1 | /*! |
---|
[5129] | 2 | * @file shell_command.h |
---|
[5068] | 3 | * Definition of a on-screen-shell |
---|
[5391] | 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" |
---|
[5552] | 12 | #include "multi_type.h" |
---|
[5155] | 13 | #include "substring.h" |
---|
[5143] | 14 | #include "functor_list.h" |
---|
[5636] | 15 | #include "executor/executor.h" |
---|
[5068] | 16 | #include <stdarg.h> |
---|
| 17 | |
---|
[5166] | 18 | #define SHELL_COMMAND_MAX_SIZE //!< The maximum size of a Shell Command |
---|
[5130] | 19 | |
---|
[5127] | 20 | |
---|
[5130] | 21 | |
---|
[4838] | 22 | // FORWARD DECLARATION |
---|
[5068] | 23 | template<class T> class tList; |
---|
[3543] | 24 | |
---|
[5166] | 25 | /** |
---|
| 26 | * an easy to use Macro to create a Command |
---|
| 27 | * @param command the name of the command (without "" around the string) |
---|
| 28 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
| 29 | * @param function the function to call |
---|
[5179] | 30 | * |
---|
| 31 | * MEANING: |
---|
[5636] | 32 | * ShellCommand* someUniqueVarName = |
---|
[5179] | 33 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
| 34 | * |
---|
| 35 | * In the Shell you would call this Command using: |
---|
| 36 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
[5166] | 37 | */ |
---|
[5636] | 38 | //#define SHELL_COMMAND(command, class, function) \ |
---|
| 39 | // ShellCommand* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function) |
---|
[5162] | 40 | #define SHELL_COMMAND(command, class, function) \ |
---|
[5636] | 41 | ShellCommand* shell_command_##class##_##command = ShellCommand::registerCommand(#command, #class, new ExecutorObjective<class>(&class::function)) |
---|
| 42 | |
---|
[5329] | 43 | /** |
---|
| 44 | * an easy to use Macro to create a Command |
---|
| 45 | * @param command the name of the command (without "" around the string) |
---|
| 46 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
| 47 | * @param function the function to call |
---|
| 48 | * |
---|
| 49 | * MEANING: |
---|
[5636] | 50 | * ShellCommand* someUniqueVarName = |
---|
[5329] | 51 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
| 52 | * |
---|
| 53 | * In the Shell you would call this Command using: |
---|
| 54 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
| 55 | */ |
---|
| 56 | #define SHELL_COMMAND_STATIC(command, class, function) \ |
---|
[5636] | 57 | ShellCommand* shell_command_##class##_##command = ShellCommand::registerCommand(#command, #class, new ExecutorStatic<class>(function)) |
---|
[5135] | 58 | |
---|
[5162] | 59 | |
---|
[5328] | 60 | //! an enumerator for the definition of the Type. |
---|
| 61 | typedef enum { |
---|
| 62 | ShellCommand_Objective = 1, |
---|
| 63 | ShellCommand_Static = 2, |
---|
| 64 | } ShellCommand_Type; |
---|
| 65 | |
---|
[5161] | 66 | //////////////// |
---|
| 67 | // BASE CLASS // |
---|
| 68 | //////////////// |
---|
[5636] | 69 | class ShellCommand; |
---|
[5190] | 70 | class ShellCommandAlias; |
---|
[5170] | 71 | |
---|
| 72 | //! A class to hold all Classes that have (once) registered Commands. |
---|
| 73 | class ShellCommandClass : public BaseObject |
---|
| 74 | { |
---|
[5636] | 75 | friend class ShellCommand; |
---|
[5170] | 76 | |
---|
| 77 | public: |
---|
[5197] | 78 | /** @returns the CommandClassList */ |
---|
[5170] | 79 | static const tList<ShellCommandClass>* getCommandClassList() { return ShellCommandClass::commandClassList; }; |
---|
[5195] | 80 | static bool getCommandListOfClass(const char* className, tList<const char>* stringList); |
---|
| 81 | static bool getCommandListOfAlias(tList<const char>* aliasList); |
---|
[5190] | 82 | |
---|
[5170] | 83 | static ShellCommandClass* getCommandClass(const char* className); |
---|
[5171] | 84 | static void unregisterAllCommands(); |
---|
[5170] | 85 | |
---|
[5204] | 86 | static void help (const char* className); |
---|
| 87 | |
---|
[5170] | 88 | private: |
---|
| 89 | ShellCommandClass(const char* className); |
---|
| 90 | ~ShellCommandClass(); |
---|
| 91 | |
---|
| 92 | static const ShellCommandClass* isRegistered(const char* className); |
---|
| 93 | static void initCommandClassList(); |
---|
| 94 | |
---|
| 95 | private: |
---|
[5171] | 96 | const char* className; //!< The Name of the Class. This should match the ClassName of the Commands Class. |
---|
| 97 | long classID; //!< The classID of this Class |
---|
[5636] | 98 | tList<ShellCommand>* commandList; //!< A list of Commands from this Class |
---|
[5171] | 99 | static tList<ShellCommandClass>* commandClassList; //!< A list of Classes |
---|
[5195] | 100 | static tList<ShellCommandAlias>* aliasList; //!< An Alias to A Command. (only for classes with one Instance) |
---|
[5170] | 101 | }; |
---|
| 102 | |
---|
| 103 | |
---|
[5161] | 104 | //! a baseClass for all possible ShellCommands |
---|
[5636] | 105 | class ShellCommand : public BaseObject |
---|
[5161] | 106 | { |
---|
[5170] | 107 | friend class ShellCommandClass; |
---|
[5161] | 108 | public: |
---|
| 109 | static bool execute (const char* executionString); |
---|
| 110 | |
---|
[5636] | 111 | ShellCommand* describe(const char* description); |
---|
| 112 | ShellCommand* setAlias(const char* alias); |
---|
| 113 | ShellCommand* defaultValues(unsigned int count, ...); |
---|
[5164] | 114 | |
---|
[5636] | 115 | static ShellCommand* registerCommand(const char* commandName, const char* className, Executor* executor); |
---|
| 116 | |
---|
[5166] | 117 | static void unregisterCommand(const char* commandName, const char* className); |
---|
[5161] | 118 | |
---|
| 119 | static void debug(); |
---|
| 120 | |
---|
| 121 | protected: |
---|
[5637] | 122 | ShellCommand(const char* commandName, const char* className, Executor* executor); |
---|
[5636] | 123 | ~ShellCommand(); |
---|
[5161] | 124 | |
---|
[5328] | 125 | /** @returns the Type of this Function (either static or objective) */ |
---|
| 126 | inline ShellCommand_Type getType() { return this->functorType; }; |
---|
| 127 | |
---|
[5637] | 128 | static bool isRegistered(const char* commandName, const char* className, Executor* executor); |
---|
[5161] | 129 | static const char* paramToString(long parameter); |
---|
| 130 | |
---|
| 131 | protected: |
---|
[5328] | 132 | ShellCommand_Type functorType; //!< The type of Function we've got (either static or objective). |
---|
[5163] | 133 | void* functionPointer; //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL ) |
---|
[5166] | 134 | unsigned int paramCount; //!< the count of parameters. |
---|
| 135 | unsigned int* parameters; //!< Parameters the function of this Command takes. |
---|
[5552] | 136 | MultiType* defaultValue; //!< Default Values. |
---|
[5161] | 137 | |
---|
| 138 | private: |
---|
[5170] | 139 | ShellCommandClass* shellClass; //!< A Pointer to the Shell-Class this Command belongs to. |
---|
[5196] | 140 | ShellCommandAlias* alias; //!< An Alias for the Class. |
---|
[5161] | 141 | |
---|
[5166] | 142 | const char* description; //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla"); |
---|
[5636] | 143 | Executor* executor; |
---|
[5161] | 144 | |
---|
[5129] | 145 | }; |
---|
[5113] | 146 | |
---|
[5197] | 147 | //! A Class, that handles aliases. |
---|
[5190] | 148 | class ShellCommandAlias |
---|
| 149 | { |
---|
[5636] | 150 | friend class ShellCommand; |
---|
[5190] | 151 | public: |
---|
[5197] | 152 | /** @returns the Name of the Alias. */ |
---|
[5195] | 153 | const char* getName() const { return this->aliasName; }; |
---|
[5197] | 154 | /** @returns the Command, this Alias is asociated with */ |
---|
[5636] | 155 | ShellCommand* getCommand() const { return this->command; }; |
---|
[5196] | 156 | |
---|
[5190] | 157 | private: |
---|
[5197] | 158 | /** @param aliasName the name of the Alias @param command the Command, to associate this alias with */ |
---|
[5636] | 159 | ShellCommandAlias(const char* aliasName, ShellCommand* command) { this->aliasName = aliasName; this->command = command; }; |
---|
[5196] | 160 | |
---|
| 161 | private: |
---|
[5197] | 162 | const char* aliasName; //!< the name of the Alias |
---|
[5636] | 163 | ShellCommand* command; //!< a pointer to the command, this alias executes. |
---|
[5190] | 164 | }; |
---|
| 165 | |
---|
[5129] | 166 | #endif /* _SHELL_COMMAND_H */ |
---|