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 "helper_functions.h" |
---|
12 | #include "functor_list.h" |
---|
13 | #include "substring.h" |
---|
14 | |
---|
15 | #include <stdarg.h> |
---|
16 | |
---|
17 | #define SHELL_COMMAND_MAX_SIZE |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | // FORWARD DECLARATION |
---|
22 | template<class T> class tList; |
---|
23 | |
---|
24 | |
---|
25 | /////////////////////// |
---|
26 | // MACRO DEFINITIONS // |
---|
27 | /////////////////////// |
---|
28 | |
---|
29 | // COMMAND REGISTRATION |
---|
30 | // NO ARGUMENTS |
---|
31 | #define ShellCommandRegister0 \ |
---|
32 | static void registerCommand(const char* commandName, ClassID classID, void (T::*function)()) \ |
---|
33 | { \ |
---|
34 | if (isRegistered(commandName, classID, 0)== true) \ |
---|
35 | return; \ |
---|
36 | new ShellCommand<T>(commandName, classID, function); \ |
---|
37 | } |
---|
38 | |
---|
39 | #define ShellCommandRegister1(t1) \ |
---|
40 | static void registerCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE), t1##_TYPE d1 = t1##_DEFAULT) \ |
---|
41 | { \ |
---|
42 | if (isRegistered(commandName, classID, 1, t1##_PARAM)== true) \ |
---|
43 | return; \ |
---|
44 | new ShellCommand<T>(commandName, classID, function, d1); \ |
---|
45 | } |
---|
46 | |
---|
47 | // CONSTRUCTORS |
---|
48 | #define ShellCommandConstructor0 \ |
---|
49 | void (T::*functionPointer_0)(); \ |
---|
50 | ShellCommand(const char* commandName, ClassID classID, void (T::*function)()) \ |
---|
51 | : ShellCommandBase(commandName, classID, 0) \ |
---|
52 | { \ |
---|
53 | this->functionPointer_0 = function; \ |
---|
54 | } |
---|
55 | |
---|
56 | #define ShellCommandConstructor1(t1) \ |
---|
57 | void (T::*functionPointer_1_##t1)(t1##_TYPE); \ |
---|
58 | ShellCommand(const char* commandName, ClassID classID, void (T::*function)(t1##_TYPE), t1##_TYPE d1) \ |
---|
59 | : ShellCommandBase(commandName, classID, 1, t1##_PARAM, d1) \ |
---|
60 | { \ |
---|
61 | this->functionPointer_1_##t1 = function; \ |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | #define ShellCommandExecute0 \ |
---|
67 | if (this->paramCount == 0) \ |
---|
68 | (dynamic_cast<T*>(object)->*functionPointer_0)() |
---|
69 | |
---|
70 | #define ShellCommandExecute1(t1) \ |
---|
71 | else if (this->paramCount == 1 && this->parameters[0] == t1##_PARAM) \ |
---|
72 | (dynamic_cast<T*>(object)->*functionPointer_1_##t1)(t1##_FUNC(parameters, 0)) |
---|
73 | |
---|
74 | |
---|
75 | //////////////// |
---|
76 | // BASE CLASS // |
---|
77 | //////////////// |
---|
78 | //! a baseClass for all possible ShellCommands |
---|
79 | class ShellCommandBase : public BaseObject |
---|
80 | { |
---|
81 | public: |
---|
82 | static bool execute (const char* executionString); |
---|
83 | |
---|
84 | static const tList<ShellCommandBase>* getCommandList() { return ShellCommandBase::commandList; }; |
---|
85 | |
---|
86 | |
---|
87 | protected: |
---|
88 | ShellCommandBase(const char* commandName, ClassID classID, unsigned int paramCount, ...); |
---|
89 | ~ShellCommandBase(); |
---|
90 | |
---|
91 | static bool isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...); |
---|
92 | static const char* paramToString(long parameter); |
---|
93 | |
---|
94 | void debug(); |
---|
95 | private: |
---|
96 | virtual void executeCommand (BaseObject* object, const char* parameters) = NULL; |
---|
97 | |
---|
98 | protected: |
---|
99 | void* functionPointer; //!< The pointeer to the function of the Class (or static pointer if ClassID == CL_NULL ) |
---|
100 | unsigned int paramCount; //!< the count of parameters |
---|
101 | unsigned int* parameters; //!< Parameters |
---|
102 | bool isSingleton; //!< if the Class is Singleton @todo autocheck |
---|
103 | |
---|
104 | private: |
---|
105 | long classID; //!< The ID of the Class associated to this Command |
---|
106 | const char* className; //!< The Name of the Class associated to this Command |
---|
107 | |
---|
108 | char* defaultStrings[FUNCTOR_MAX_ARGUMENTS]; |
---|
109 | int defaultInts[FUNCTOR_MAX_ARGUMENTS]; |
---|
110 | float defaultFloats[FUNCTOR_MAX_ARGUMENTS]; |
---|
111 | bool defaultBools[FUNCTOR_MAX_ARGUMENTS]; |
---|
112 | |
---|
113 | |
---|
114 | // STATIC MEMBERS |
---|
115 | static tList<ShellCommandBase>* commandList; //!< A list of availiable commands. |
---|
116 | }; |
---|
117 | |
---|
118 | template<class T> class ShellCommand; |
---|
119 | /* |
---|
120 | template<class T> class ShellCommandSingleton : public ShellCommandBase |
---|
121 | { |
---|
122 | friend class ShellCommand<T>; |
---|
123 | private: |
---|
124 | ShellCommandSingleton(const char* commandName, ClassID classID, void (T::*functionPointer)()) |
---|
125 | : ShellCommandBase (commandName, classID, 0) |
---|
126 | { |
---|
127 | this->functionPointer_NULL = functionPointer; |
---|
128 | } |
---|
129 | void (T::*functionPointer_NULL)(); |
---|
130 | |
---|
131 | virtual void executeCommand (const char* parameters) |
---|
132 | { |
---|
133 | if (this->paramCount == 0) |
---|
134 | (T::getInstance()->*functionPointer_NULL)(); |
---|
135 | } |
---|
136 | };*/ |
---|
137 | |
---|
138 | |
---|
139 | //! keeps information about a ShellCommand |
---|
140 | template<class T> class ShellCommand : public ShellCommandBase |
---|
141 | { |
---|
142 | public: |
---|
143 | static void unregisterCommand(const char* commandNaame, ClassID classID); |
---|
144 | |
---|
145 | #define FUNCTOR_LIST(x) ShellCommandRegister ## x |
---|
146 | //#include "functor_list.h" |
---|
147 | FUNCTOR_LIST(0); |
---|
148 | FUNCTOR_LIST(1)(l_INT); |
---|
149 | FUNCTOR_LIST(1)(l_STRING); |
---|
150 | #undef FUNCTOR_LIST |
---|
151 | |
---|
152 | |
---|
153 | private: |
---|
154 | #define FUNCTOR_LIST(x) ShellCommandConstructor ## x |
---|
155 | //#include "functor_list.h" |
---|
156 | FUNCTOR_LIST(0); |
---|
157 | FUNCTOR_LIST(1)(l_INT); |
---|
158 | FUNCTOR_LIST(1)(l_STRING); |
---|
159 | #undef FUNCTOR_LIST |
---|
160 | |
---|
161 | virtual void executeCommand (BaseObject* object, const char* parameters) |
---|
162 | { |
---|
163 | // if (parameters != NULL) |
---|
164 | //SubString params(parameters, ','); |
---|
165 | #define FUNCTOR_LIST(x) ShellCommandExecute ## x |
---|
166 | //#include "functor_list.h" |
---|
167 | FUNCTOR_LIST(0); |
---|
168 | FUNCTOR_LIST(1)(l_INT); |
---|
169 | FUNCTOR_LIST(1)(l_STRING); |
---|
170 | #undef FUNCTOR_LIST |
---|
171 | } |
---|
172 | }; |
---|
173 | |
---|
174 | #endif /* _SHELL_COMMAND_H */ |
---|