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 <stdarg.h> |
---|
13 | |
---|
14 | #define SHELL_COMMAND_MAX_SIZE //!< The maximum size of a Shell Command |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | namespace OrxShell |
---|
19 | { |
---|
20 | // FORWARD DECLARATION |
---|
21 | class ShellCommandClass; |
---|
22 | class ShellCommandAlias; |
---|
23 | |
---|
24 | /** |
---|
25 | * an easy to use Macro to create a Command |
---|
26 | * @param command the name of the command (without "" around the string) |
---|
27 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
28 | * @param function the function to call |
---|
29 | * |
---|
30 | * MEANING: |
---|
31 | * ShellCommand* someUniqueVarName = |
---|
32 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
33 | * |
---|
34 | * In the Shell you would call this Command using: |
---|
35 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
36 | */ |
---|
37 | //#define SHELL_COMMAND(command, class, function) \ |
---|
38 | // ShellCommand* shell_command_##class##_##command = ShellCommand<class>::registerCommand(#command, #class, &class::function) |
---|
39 | #define SHELL_COMMAND(command, class, function) \ |
---|
40 | OrxShell::ShellCommand* shell_command_##class##_##command = OrxShell::ShellCommand::registerCommand(#command, #class, ExecutorObjective<class>(&class::function)) |
---|
41 | |
---|
42 | /** |
---|
43 | * an easy to use Macro to create a Command |
---|
44 | * @param command the name of the command (without "" around the string) |
---|
45 | * @param class the name of the class to apply this command to (without the "" around the string) |
---|
46 | * @param function the function to call |
---|
47 | * |
---|
48 | * MEANING: |
---|
49 | * ShellCommand* someUniqueVarName = |
---|
50 | * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); |
---|
51 | * |
---|
52 | * In the Shell you would call this Command using: |
---|
53 | * $ ClassName [ObjectName] commandNameInShell [parameters] |
---|
54 | */ |
---|
55 | #define SHELL_COMMAND_STATIC(command, class, function) \ |
---|
56 | OrxShell::ShellCommand* shell_command_##class##_##command = OrxShell::ShellCommand::registerCommand(#command, #class, ExecutorStatic<class>(function)) |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | //! a baseClass for all possible ShellCommands |
---|
61 | class ShellCommand : public BaseObject |
---|
62 | { |
---|
63 | friend class ShellCommandClass; |
---|
64 | public: |
---|
65 | static bool execute (const std::string& executionString); |
---|
66 | |
---|
67 | ShellCommand* describe(const std::string& description); |
---|
68 | ShellCommand* setAlias(const std::string& alias); |
---|
69 | ShellCommand* defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL, |
---|
70 | const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL, |
---|
71 | const MultiType& value4 = MT_NULL); |
---|
72 | |
---|
73 | static ShellCommand* registerCommand(const std::string& commandName, const std::string& className, const Executor& executor); |
---|
74 | |
---|
75 | static void unregisterCommand(const std::string& commandName, const std::string& className); |
---|
76 | |
---|
77 | static void debug(); |
---|
78 | |
---|
79 | protected: |
---|
80 | ShellCommand(const std::string& commandName, const std::string& className, const Executor& executor); |
---|
81 | ~ShellCommand(); |
---|
82 | |
---|
83 | static bool isRegistered(const std::string& commandName, const std::string& className); |
---|
84 | static const char* paramToString(long parameter); |
---|
85 | |
---|
86 | private: |
---|
87 | ShellCommandClass* shellClass; //!< A Pointer to the Shell-Class this Command belongs to. |
---|
88 | ShellCommandAlias* alias; //!< An Alias for the Class. |
---|
89 | |
---|
90 | std::string description; //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla"); |
---|
91 | Executor* executor; //!< The Executor, that really executes the Function. |
---|
92 | |
---|
93 | }; |
---|
94 | |
---|
95 | //! A Class, that handles aliases. |
---|
96 | class ShellCommandAlias |
---|
97 | { |
---|
98 | friend class ShellCommand; |
---|
99 | public: |
---|
100 | /** @returns the Name of the Alias. */ |
---|
101 | const std::string& getName() const { return this->aliasName; }; |
---|
102 | /** @returns the Command, this Alias is asociated with */ |
---|
103 | ShellCommand* getCommand() const { return this->command; }; |
---|
104 | |
---|
105 | private: |
---|
106 | /** @param aliasName the name of the Alias @param command the Command, to associate this alias with */ |
---|
107 | ShellCommandAlias(const std::string& aliasName, ShellCommand* command) { this->aliasName = aliasName; this->command = command; }; |
---|
108 | |
---|
109 | private: |
---|
110 | std::string aliasName; //!< the name of the Alias |
---|
111 | ShellCommand* command; //!< a pointer to the command, this alias executes. |
---|
112 | }; |
---|
113 | |
---|
114 | } |
---|
115 | |
---|
116 | #endif /* _SHELL_COMMAND_H */ |
---|