[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 | |
---|
[5636] | 11 | #include "executor/executor.h" |
---|
[7407] | 12 | #include "shell_completion_plugin.h" |
---|
[5068] | 13 | |
---|
[5166] | 14 | #define SHELL_COMMAND_MAX_SIZE //!< The maximum size of a Shell Command |
---|
[5130] | 15 | |
---|
[7374] | 16 | namespace OrxShell |
---|
| 17 | { |
---|
| 18 | // FORWARD DECLARATION |
---|
| 19 | class ShellCommandClass; |
---|
| 20 | class ShellCommandAlias; |
---|
[7407] | 21 | class CompletorPlugin; |
---|
[3543] | 22 | |
---|
[7374] | 23 | /** |
---|
[7398] | 24 | * @brief an easy to use Macro to create a Command |
---|
[7374] | 25 | * @param command the name of the command (without "" around the string) |
---|
| 26 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
| 27 | * @param function the function to call |
---|
| 28 | * |
---|
| 29 | * MEANING: |
---|
| 30 | * ShellCommand* someUniqueVarName = |
---|
| 31 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
| 32 | * |
---|
| 33 | * In the Shell you would call this Command using: |
---|
| 34 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
| 35 | */ |
---|
| 36 | //#define SHELL_COMMAND(command, class, function) \ |
---|
| 37 | // ShellCommand* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function) |
---|
[5162] | 38 | #define SHELL_COMMAND(command, class, function) \ |
---|
[7374] | 39 | OrxShell::ShellCommand* shell_command_##class##_##command = OrxShell::ShellCommand::registerCommand(#command, #class, ExecutorObjective<class>(&class::function)) |
---|
[5636] | 40 | |
---|
[7374] | 41 | /** |
---|
[7398] | 42 | * @brief an easy to use Macro to create a Command |
---|
[7374] | 43 | * @param command the name of the command (without "" around the string) |
---|
| 44 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
| 45 | * @param function the function to call |
---|
| 46 | * |
---|
| 47 | * MEANING: |
---|
| 48 | * ShellCommand* someUniqueVarName = |
---|
| 49 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
| 50 | * |
---|
| 51 | * In the Shell you would call this Command using: |
---|
| 52 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
| 53 | */ |
---|
[5329] | 54 | #define SHELL_COMMAND_STATIC(command, class, function) \ |
---|
[7374] | 55 | OrxShell::ShellCommand* shell_command_##class##_##command = OrxShell::ShellCommand::registerCommand(#command, #class, ExecutorStatic<class>(function)) |
---|
[5135] | 56 | |
---|
[5162] | 57 | |
---|
[5328] | 58 | |
---|
[7374] | 59 | //! a baseClass for all possible ShellCommands |
---|
| 60 | class ShellCommand : public BaseObject |
---|
| 61 | { |
---|
| 62 | friend class ShellCommandClass; |
---|
[5161] | 63 | public: |
---|
[7411] | 64 | //! an enumerator for different types the Shell parses. |
---|
| 65 | typedef enum { |
---|
| 66 | NullRecognition = 0, |
---|
| 67 | ClassRecognition = 1, |
---|
| 68 | ObjectRecognition = 2, |
---|
| 69 | FunctionRecognition = 4, |
---|
| 70 | AliasRecognition = 8, |
---|
| 71 | ParameterRecognition = 16, |
---|
| 72 | } RecognitionType; |
---|
| 73 | |
---|
[7221] | 74 | static bool execute (const std::string& executionString); |
---|
[5161] | 75 | |
---|
[7221] | 76 | ShellCommand* describe(const std::string& description); |
---|
[7225] | 77 | ShellCommand* setAlias(const std::string& alias); |
---|
[7198] | 78 | ShellCommand* defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL, |
---|
| 79 | const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL, |
---|
| 80 | const MultiType& value4 = MT_NULL); |
---|
[5164] | 81 | |
---|
[7225] | 82 | static ShellCommand* registerCommand(const std::string& commandName, const std::string& className, const Executor& executor); |
---|
| 83 | static void unregisterCommand(const std::string& commandName, const std::string& className); |
---|
[7408] | 84 | static const ShellCommand* const getCommand(const std::string& commandName, const std::string& className); |
---|
[7403] | 85 | static bool exists(const std::string& commandName, const std::string& className); |
---|
[5161] | 86 | |
---|
[7409] | 87 | const ShellCommandClass* const getCommandClass() const { return this->shellClass; }; |
---|
| 88 | const ShellCommandAlias* const getAlias() const { return this->alias; } |
---|
[7407] | 89 | unsigned int getParamCount() const { return this->executor->getParamCount(); } |
---|
| 90 | const CompletorPlugin* const getCompletorPlugin(unsigned int i) const { return this->completors[i]; }; |
---|
| 91 | |
---|
[5161] | 92 | static void debug(); |
---|
| 93 | |
---|
| 94 | protected: |
---|
[7225] | 95 | ShellCommand(const std::string& commandName, const std::string& className, const Executor& executor); |
---|
[7386] | 96 | virtual ~ShellCommand(); |
---|
[5161] | 97 | |
---|
[7401] | 98 | static const std::string& paramToString(long parameter); |
---|
[5161] | 99 | |
---|
| 100 | private: |
---|
[7397] | 101 | ShellCommandClass* shellClass; //!< A Pointer to the Shell-Class this Command belongs to. |
---|
| 102 | ShellCommandAlias* alias; //!< An Alias for the Class. |
---|
[5161] | 103 | |
---|
[7397] | 104 | std::string description; //!< A description for this commnand. (initially ""). Assigned with (create)->describe("blablabla"); |
---|
[7407] | 105 | std::vector<CompletorPlugin*> completors; //!< Completors for the Parameters |
---|
[7397] | 106 | Executor* executor; //!< The Executor, that really executes the Function. |
---|
[7374] | 107 | }; |
---|
[5113] | 108 | |
---|
[7374] | 109 | //! A Class, that handles aliases. |
---|
| 110 | class ShellCommandAlias |
---|
| 111 | { |
---|
[5190] | 112 | public: |
---|
[7397] | 113 | ShellCommandAlias(const std::string& aliasName, ShellCommand* command); |
---|
| 114 | ~ShellCommandAlias(); |
---|
| 115 | |
---|
[5197] | 116 | /** @returns the Name of the Alias. */ |
---|
[7221] | 117 | const std::string& getName() const { return this->aliasName; }; |
---|
[5197] | 118 | /** @returns the Command, this Alias is asociated with */ |
---|
[5636] | 119 | ShellCommand* getCommand() const { return this->command; }; |
---|
[7389] | 120 | static bool getCommandListOfAlias(std::list<std::string>& stringList); |
---|
[7397] | 121 | static const std::vector<ShellCommandAlias*>& getAliases() { return ShellCommandAlias::aliasList; }; |
---|
[5196] | 122 | |
---|
[5190] | 123 | private: |
---|
[7221] | 124 | std::string aliasName; //!< the name of the Alias |
---|
[5636] | 125 | ShellCommand* command; //!< a pointer to the command, this alias executes. |
---|
[7389] | 126 | |
---|
| 127 | static std::vector<ShellCommandAlias*> aliasList; //!< A list of Aliases to A Commands. |
---|
[7374] | 128 | }; |
---|
[5190] | 129 | |
---|
[7374] | 130 | } |
---|
| 131 | |
---|
[5129] | 132 | #endif /* _SHELL_COMMAND_H */ |
---|