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 | |
---|
13 | /// THIS IS USED TO LOAD CONSTANT AND STATIC FUNCTIONS EASILY. |
---|
14 | #include "executor/executor_functional.h" |
---|
15 | #define EXECUTOR_FUNCTIONAL_USE_CONST |
---|
16 | #include "executor/executor_functional.h" |
---|
17 | #define EXECUTOR_FUNCTIONAL_USE_STATIC |
---|
18 | #include "executor/executor_functional.h" |
---|
19 | |
---|
20 | |
---|
21 | #include "shell_completion_plugin.h" |
---|
22 | |
---|
23 | #define SHELL_COMMAND_MAX_SIZE //!< The maximum size of a Shell Command |
---|
24 | |
---|
25 | namespace OrxShell |
---|
26 | { |
---|
27 | // FORWARD DECLARATION |
---|
28 | class ShellCommandClass; |
---|
29 | class ShellCommandAlias; |
---|
30 | class CompletorPlugin; |
---|
31 | |
---|
32 | /** |
---|
33 | * @brief an easy to use Macro to create a Command |
---|
34 | * @param command the name of the command (without "" around the string) |
---|
35 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
36 | * @param function the function to call |
---|
37 | * |
---|
38 | * MEANING: |
---|
39 | * ShellCommand* someUniqueVarName = |
---|
40 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
41 | * |
---|
42 | * In the Shell you would call this Command using: |
---|
43 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
44 | */ |
---|
45 | #define SHELL_COMMAND(command, class, function) \ |
---|
46 | OrxShell::ShellCommand* shell_command_##class##_##command = OrxShell::ShellCommand::registerCommand(#command, #class, createExecutor<class>(&class::function)) |
---|
47 | |
---|
48 | /** |
---|
49 | * @brief an easy to use Macro to create a Command |
---|
50 | * @param command the name of the command (without "" around the string) |
---|
51 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
52 | * @param function the function to call |
---|
53 | * |
---|
54 | * MEANING: |
---|
55 | * ShellCommand* someUniqueVarName = |
---|
56 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
57 | * |
---|
58 | * In the Shell you would call this Command using: |
---|
59 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
60 | */ |
---|
61 | #define SHELL_COMMAND_STATIC(command, class, function) \ |
---|
62 | OrxShell::ShellCommand* shell_command_##class##_##command = OrxShell::ShellCommand::registerCommand(#command, #class, createExecutor<class>(function)) |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | //! a baseClass for all possible ShellCommands |
---|
67 | class ShellCommand : public BaseObject |
---|
68 | { |
---|
69 | ObjectListDeclaration(ShellCommand); |
---|
70 | |
---|
71 | friend class ShellCommandClass; |
---|
72 | public: |
---|
73 | static bool execute (const std::string& executionString); |
---|
74 | |
---|
75 | ShellCommand* describe(const std::string& description); |
---|
76 | ShellCommand* setAlias(const std::string& alias); |
---|
77 | ShellCommand* defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL, |
---|
78 | const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL, |
---|
79 | const MultiType& value4 = MT_NULL); |
---|
80 | ShellCommand* completionPlugin(unsigned int parameter, const CompletorPlugin& completorPlugin); |
---|
81 | |
---|
82 | static ShellCommand* registerCommand(const std::string& commandName, const std::string& className, Executor<const SubString>* executor); |
---|
83 | static void unregisterCommand(const std::string& commandName, const std::string& className); |
---|
84 | static const ShellCommand* const getCommand(const std::string& commandName, const std::string& className); |
---|
85 | static const ShellCommand* const getCommand(const std::string& commandName, const ShellCommandClass* cmdClass); |
---|
86 | static bool exists(const std::string& commandName, const std::string& className); |
---|
87 | static const ShellCommand* const getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin, std::vector<BaseObject*>* boList = NULL); |
---|
88 | static const ShellCommand* const getCommandFromInput(const SubString& strings, unsigned int& paramBegin, std::vector<BaseObject*>* boList = NULL); |
---|
89 | |
---|
90 | const ShellCommandClass* const getCommandClass() const { return this->shellClass; }; |
---|
91 | const ShellCommandAlias* const getAlias() const { return this->alias; } |
---|
92 | unsigned int getParamCount() const { return this->executor->getParamCount(); } |
---|
93 | const CompletorPlugin* const getCompletorPlugin(unsigned int i) const { return this->completors[i]; }; |
---|
94 | |
---|
95 | void help() const; |
---|
96 | |
---|
97 | protected: |
---|
98 | ShellCommand(const std::string& commandName, const std::string& className, Executor<const SubString>* executor); |
---|
99 | virtual ~ShellCommand(); |
---|
100 | |
---|
101 | static bool fillObjectList(const std::string& objectDescriptor, const ShellCommand* cmd, std::vector<BaseObject*>* boList); |
---|
102 | static const std::string& paramToString(long parameter); |
---|
103 | |
---|
104 | private: |
---|
105 | ShellCommandClass* shellClass; //!< A Pointer to the Shell-Class this Command belongs to. |
---|
106 | ShellCommandAlias* alias; //!< An Alias for the Class. |
---|
107 | |
---|
108 | std::string description; //!< A description for this commnand. (initially ""). Assigned with (create)->describe("blablabla"); |
---|
109 | std::vector<CompletorPlugin*> completors; //!< Completors for the Parameters |
---|
110 | Executor<const SubString>* executor; //!< The Executor, that really executes the Function. |
---|
111 | }; |
---|
112 | |
---|
113 | //! A Class, that handles aliases. |
---|
114 | class ShellCommandAlias |
---|
115 | { |
---|
116 | public: |
---|
117 | ShellCommandAlias(const std::string& aliasName, ShellCommand* command); |
---|
118 | ~ShellCommandAlias(); |
---|
119 | |
---|
120 | /** @returns the Name of the Alias. */ |
---|
121 | const std::string& getName() const { return this->aliasName; }; |
---|
122 | /** @returns the Command, this Alias is asociated with */ |
---|
123 | ShellCommand* getCommand() const { return this->command; }; |
---|
124 | static bool getCommandListOfAlias(std::list<std::string>& stringList); |
---|
125 | static const std::vector<ShellCommandAlias*>& getAliases() { return ShellCommandAlias::aliasList; }; |
---|
126 | |
---|
127 | private: |
---|
128 | std::string aliasName; //!< the name of the Alias |
---|
129 | ShellCommand* command; //!< a pointer to the command, this alias executes. |
---|
130 | |
---|
131 | static std::vector<ShellCommandAlias*> aliasList; //!< A list of Aliases to A Commands. |
---|
132 | }; |
---|
133 | |
---|
134 | } |
---|
135 | |
---|
136 | #endif /* _SHELL_COMMAND_H */ |
---|