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