| 1 | /*! |
|---|
| 2 | * @file shell_command.h |
|---|
| 3 | * Definition of a on-screen-shell |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #ifndef _SHELL_COMMAND_H |
|---|
| 7 | #define _SHELL_COMMAND_H |
|---|
| 8 | |
|---|
| 9 | #include "base_object.h" |
|---|
| 10 | |
|---|
| 11 | #include "executor/executor.h" |
|---|
| 12 | #include "shell_completion_plugin.h" |
|---|
| 13 | |
|---|
| 14 | #define SHELL_COMMAND_MAX_SIZE //!< The maximum size of a Shell Command |
|---|
| 15 | |
|---|
| 16 | namespace OrxShell |
|---|
| 17 | { |
|---|
| 18 | // FORWARD DECLARATION |
|---|
| 19 | class ShellCommandClass; |
|---|
| 20 | class ShellCommandAlias; |
|---|
| 21 | class CompletorPlugin; |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * @brief an easy to use Macro to create a Command |
|---|
| 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) |
|---|
| 38 | #define SHELL_COMMAND(command, class, function) \ |
|---|
| 39 | OrxShell::ShellCommand* shell_command_##class##_##command = OrxShell::ShellCommand::registerCommand(#command, #class, ExecutorObjective<class>(&class::function)) |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * @brief an easy to use Macro to create a Command |
|---|
| 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 | */ |
|---|
| 54 | #define SHELL_COMMAND_STATIC(command, class, function) \ |
|---|
| 55 | OrxShell::ShellCommand* shell_command_##class##_##command = OrxShell::ShellCommand::registerCommand(#command, #class, ExecutorStatic<class>(function)) |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | //! a baseClass for all possible ShellCommands |
|---|
| 60 | class ShellCommand : public BaseObject |
|---|
| 61 | { |
|---|
| 62 | friend class ShellCommandClass; |
|---|
| 63 | public: |
|---|
| 64 | static bool execute (const std::string& executionString); |
|---|
| 65 | |
|---|
| 66 | ShellCommand* describe(const std::string& description); |
|---|
| 67 | ShellCommand* setAlias(const std::string& alias); |
|---|
| 68 | ShellCommand* defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL, |
|---|
| 69 | const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL, |
|---|
| 70 | const MultiType& value4 = MT_NULL); |
|---|
| 71 | ShellCommand* completionPlugin(unsigned int parameter, const CompletorPlugin& completorPlugin); |
|---|
| 72 | |
|---|
| 73 | static ShellCommand* registerCommand(const std::string& commandName, const std::string& className, const Executor& executor); |
|---|
| 74 | static void unregisterCommand(const std::string& commandName, const std::string& className); |
|---|
| 75 | static const ShellCommand* const getCommand(const std::string& commandName, const std::string& className); |
|---|
| 76 | static const ShellCommand* const getCommand(const std::string& commandName, const ShellCommandClass* cmdClass); |
|---|
| 77 | static bool exists(const std::string& commandName, const std::string& className); |
|---|
| 78 | static const ShellCommand* const getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin, std::vector<BaseObject*>* boList = NULL); |
|---|
| 79 | static const ShellCommand* const getCommandFromInput(const SubString& strings, unsigned int& paramBegin, std::vector<BaseObject*>* boList = NULL); |
|---|
| 80 | |
|---|
| 81 | const ShellCommandClass* const getCommandClass() const { return this->shellClass; }; |
|---|
| 82 | const ShellCommandAlias* const getAlias() const { return this->alias; } |
|---|
| 83 | unsigned int getParamCount() const { return this->executor->getParamCount(); } |
|---|
| 84 | const CompletorPlugin* const getCompletorPlugin(unsigned int i) const { return this->completors[i]; }; |
|---|
| 85 | |
|---|
| 86 | static void debug(); |
|---|
| 87 | |
|---|
| 88 | protected: |
|---|
| 89 | ShellCommand(const std::string& commandName, const std::string& className, const Executor& executor); |
|---|
| 90 | virtual ~ShellCommand(); |
|---|
| 91 | |
|---|
| 92 | static bool fillObjectList(const std::string& objectDescriptor, const ShellCommand* cmd, std::vector<BaseObject*>* boList); |
|---|
| 93 | static const std::string& paramToString(long parameter); |
|---|
| 94 | |
|---|
| 95 | private: |
|---|
| 96 | ShellCommandClass* shellClass; //!< A Pointer to the Shell-Class this Command belongs to. |
|---|
| 97 | ShellCommandAlias* alias; //!< An Alias for the Class. |
|---|
| 98 | |
|---|
| 99 | std::string description; //!< A description for this commnand. (initially ""). Assigned with (create)->describe("blablabla"); |
|---|
| 100 | std::vector<CompletorPlugin*> completors; //!< Completors for the Parameters |
|---|
| 101 | Executor* executor; //!< The Executor, that really executes the Function. |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | //! A Class, that handles aliases. |
|---|
| 105 | class ShellCommandAlias |
|---|
| 106 | { |
|---|
| 107 | public: |
|---|
| 108 | ShellCommandAlias(const std::string& aliasName, ShellCommand* command); |
|---|
| 109 | ~ShellCommandAlias(); |
|---|
| 110 | |
|---|
| 111 | /** @returns the Name of the Alias. */ |
|---|
| 112 | const std::string& getName() const { return this->aliasName; }; |
|---|
| 113 | /** @returns the Command, this Alias is asociated with */ |
|---|
| 114 | ShellCommand* getCommand() const { return this->command; }; |
|---|
| 115 | static bool getCommandListOfAlias(std::list<std::string>& stringList); |
|---|
| 116 | static const std::vector<ShellCommandAlias*>& getAliases() { return ShellCommandAlias::aliasList; }; |
|---|
| 117 | |
|---|
| 118 | private: |
|---|
| 119 | std::string aliasName; //!< the name of the Alias |
|---|
| 120 | ShellCommand* command; //!< a pointer to the command, this alias executes. |
|---|
| 121 | |
|---|
| 122 | static std::vector<ShellCommandAlias*> aliasList; //!< A list of Aliases to A Commands. |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | #endif /* _SHELL_COMMAND_H */ |
|---|