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