1 | /*
|
---|
2 | * Action_impl.hpp
|
---|
3 | *
|
---|
4 | * Created on: Aug 25, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | /** These macros define the following functions, necessary but repetitive for
|
---|
9 | * every Action:
|
---|
10 | * -# Dialog* fillDialog()
|
---|
11 | * -# action command (e.g. AnalysisMolecularVolume() )
|
---|
12 | * -# void getParametersfromValuStorage()
|
---|
13 | * -# struct Action...Parameters
|
---|
14 | *
|
---|
15 | * For this, the user has the define the following values, each with
|
---|
16 | * parenthesis, for the values/parameters the action needs
|
---|
17 | * -# paramtypes, e.g. (int)(double)
|
---|
18 | * -# paramtokens, e.g. ("Z")("length")
|
---|
19 | * -# paramreferences, e.g. (Z)(length)
|
---|
20 | * and for additional values/parameters to save in the state
|
---|
21 | * -# statetypes, e.g. (int)(double)
|
---|
22 | * -# statereferences, e.g. (Z)(length)
|
---|
23 | * and the name and category of the action
|
---|
24 | * -# CATEGORY, e.g. Analysis
|
---|
25 | * -# ACTIONNAME, e.g. MolecularVolume
|
---|
26 | */
|
---|
27 |
|
---|
28 | // include config.h
|
---|
29 | #ifdef HAVE_CONFIG_H
|
---|
30 | #include <config.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include "CodePatterns/Chronos.hpp"
|
---|
34 |
|
---|
35 | #include <boost/preprocessor/cat.hpp>
|
---|
36 | #include <boost/preprocessor/expand.hpp>
|
---|
37 | #include <boost/preprocessor/comparison/equal.hpp>
|
---|
38 | #include <boost/preprocessor/comparison/not_equal.hpp>
|
---|
39 | #include <boost/preprocessor/control/if.hpp>
|
---|
40 | #include <boost/preprocessor/debug/assert.hpp>
|
---|
41 | #include <boost/preprocessor/iteration/local.hpp>
|
---|
42 | #include <boost/preprocessor/punctuation/comma_if.hpp>
|
---|
43 | #include <boost/preprocessor/repetition/repeat.hpp>
|
---|
44 | #include <boost/preprocessor/seq/elem.hpp>
|
---|
45 | #include <boost/preprocessor/seq/push_back.hpp>
|
---|
46 | #include <boost/preprocessor/seq/seq.hpp>
|
---|
47 | #include <boost/preprocessor/seq/size.hpp>
|
---|
48 | #include <boost/preprocessor/seq/transform.hpp>
|
---|
49 |
|
---|
50 | #include "Parameters/Parameter.hpp"
|
---|
51 |
|
---|
52 |
|
---|
53 | // some derived names: if CATEGORY is not given, we don't prefix with it
|
---|
54 | #ifdef CATEGORY
|
---|
55 | #define ACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
|
---|
56 | #define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
|
---|
57 | #define STATE BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, State))
|
---|
58 | #define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
|
---|
59 | #else
|
---|
60 | #define ACTION BOOST_PP_CAT(ACTIONNAME, Action)
|
---|
61 | #define COMMAND ACTIONNAME
|
---|
62 | #define STATE BOOST_PP_CAT(ACTIONNAME, State)
|
---|
63 | #define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
|
---|
64 | #endif
|
---|
65 | #define INSTANCE BOOST_PP_CAT(this_, BOOST_PP_CAT(ACTIONNAME, _instance))
|
---|
66 |
|
---|
67 | // check if no lists given
|
---|
68 | #ifndef paramtypes
|
---|
69 | #define MAXPARAMTYPES 0
|
---|
70 | #else
|
---|
71 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
|
---|
72 | #endif
|
---|
73 | #ifndef statetypes
|
---|
74 | #define MAXSTATETYPES 0
|
---|
75 | #else
|
---|
76 | #define MAXSTATETYPES BOOST_PP_SEQ_SIZE(statetypes)
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | // check user has given name and category
|
---|
80 | #ifndef ACTIONNAME
|
---|
81 | ERROR: No "ACTIONNAME" defined in: __FILE__
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | // calculate numbers and check whether all have same size
|
---|
85 | #ifdef paramtokens
|
---|
86 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
|
---|
87 | ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
|
---|
88 | )
|
---|
89 | #endif
|
---|
90 | #ifdef paramreferences
|
---|
91 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
|
---|
92 | ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
|
---|
93 | )
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | #ifdef statetypes
|
---|
97 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXSTATETYPES, BOOST_PP_SEQ_SIZE(statereferences)),\
|
---|
98 | ERROR: There are not the same number of "statetypes" and "statereferences" in: __FILE__ \
|
---|
99 | )
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | // print a list of type ref followed by a separator, i.e. "int i;"
|
---|
103 | #define initialiser_print(z,n,initialiserlist) \
|
---|
104 | BOOST_PP_SEQ_ELEM(n, initialiserlist) \
|
---|
105 | (BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(n, initialiserlist))),
|
---|
106 |
|
---|
107 | // print a list of ref(_ref) followed by a separator, i.e. "id(_id),"
|
---|
108 | #define type_print(z,n,TYPELIST, VARLIST, separator) \
|
---|
109 | BOOST_PP_SEQ_ELEM(n, TYPELIST) \
|
---|
110 | BOOST_PP_SEQ_ELEM(n, VARLIST)\
|
---|
111 | separator
|
---|
112 |
|
---|
113 | // print a list of type ref followed, i.e. "int i, double position"
|
---|
114 | #define type_list(z,n,TYPELIST,VARLIST) \
|
---|
115 | BOOST_PP_COMMA_IF(n)\
|
---|
116 | BOOST_PP_SEQ_ELEM(n, TYPELIST) \
|
---|
117 | BOOST_PP_SEQ_ELEM(n, VARLIST)
|
---|
118 |
|
---|
119 | // prints dialog->query calls for paramtypes with tokens
|
---|
120 | #define dialog_print(z,n,unused) \
|
---|
121 | dialog->query<\
|
---|
122 | BOOST_PP_SEQ_ELEM(n, paramtypes)\
|
---|
123 | >( params. \
|
---|
124 | BOOST_PP_SEQ_ELEM(n, paramreferences)\
|
---|
125 | ,\
|
---|
126 | BOOST_PP_SEQ_ELEM(n, paramtokens)\
|
---|
127 | , Traits.getDescription()\
|
---|
128 | );
|
---|
129 |
|
---|
130 | // print an initialiser list, i.e. "var( token, valid )(,)"
|
---|
131 | #define valid_print(z,n,TOKENLIST, VARLIST, VALIDLIST) \
|
---|
132 | BOOST_PP_COMMA_IF(n) \
|
---|
133 | BOOST_PP_SEQ_ELEM(n, VARLIST) \
|
---|
134 | ( \
|
---|
135 | BOOST_PP_SEQ_ELEM(n, TOKENLIST) \
|
---|
136 | , \
|
---|
137 | BOOST_PP_SEQ_ELEM(n, VALIDLIST) \
|
---|
138 | )
|
---|
139 |
|
---|
140 | // print an initialiser list, i.e. "var( token, valid )(,)"
|
---|
141 | #define validcopy_print(z,n,TOKENLIST, VARLIST, VALID) \
|
---|
142 | BOOST_PP_COMMA_IF(n) \
|
---|
143 | BOOST_PP_SEQ_ELEM(n, VARLIST) \
|
---|
144 | ( \
|
---|
145 | VALID . \
|
---|
146 | BOOST_PP_SEQ_ELEM(n, VARLIST) \
|
---|
147 | )
|
---|
148 |
|
---|
149 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
|
---|
150 | #define value_print(z, n, container, prefix) \
|
---|
151 | prefix \
|
---|
152 | BOOST_PP_SEQ_ELEM(n, container)\
|
---|
153 | .set(\
|
---|
154 | BOOST_PP_SEQ_ELEM(n, container)\
|
---|
155 | );
|
---|
156 |
|
---|
157 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
|
---|
158 | #define valuetype_print(z,n,container, types, prefix) \
|
---|
159 | prefix \
|
---|
160 | BOOST_PP_SEQ_ELEM(n, container) \
|
---|
161 | .setAsString( \
|
---|
162 | BOOST_PP_SEQ_ELEM(n, container) \
|
---|
163 | );
|
---|
164 |
|
---|
165 | #define stringtype std::string
|
---|
166 |
|
---|
167 | #define type2string(s, data, elem) \
|
---|
168 | stringtype
|
---|
169 |
|
---|
170 |
|
---|
171 | #include "Actions/ActionRegistry.hpp"
|
---|
172 | //#include "Actions/ActionTraits.hpp"
|
---|
173 | #include "UIElements/Dialog.hpp"
|
---|
174 |
|
---|
175 | #ifdef paramtokens
|
---|
176 | #define statenecessary 1
|
---|
177 | #endif
|
---|
178 | #ifndef statetokens
|
---|
179 | #define statenecessary 1
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | namespace MoleCuilder {
|
---|
183 |
|
---|
184 | // =========== memento to remember the state when undoing ===========
|
---|
185 | #ifdef statenecessary
|
---|
186 | class STATE : public ActionState {
|
---|
187 | public:
|
---|
188 | STATE(
|
---|
189 | #if defined statetypes && defined statereferences // if we have parameters, we have to add "_" before each reference and add the params as the last one
|
---|
190 | #define OP(s,data,elem) BOOST_PP_CAT(data, elem) // OP to add "_"
|
---|
191 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_PUSH_BACK(statetypes, const ACTION::PARAMS &), BOOST_PP_SEQ_TRANSFORM(OP, _, BOOST_PP_SEQ_PUSH_BACK(statereferences, params)))
|
---|
192 | #else /// if not, params is only list
|
---|
193 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, (const ACTION::PARAMS &), (_params))
|
---|
194 | #endif
|
---|
195 | #define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES)
|
---|
196 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
197 | ) :
|
---|
198 | #if defined statetypes && defined statereferences // do we have parameters at all?
|
---|
199 | BOOST_PP_REPEAT(MAXSTATETYPES, initialiser_print, statereferences)
|
---|
200 | #endif
|
---|
201 | params(_params)
|
---|
202 | {}
|
---|
203 |
|
---|
204 | #if defined statetypes && defined statereferences // do we have parameters at all?
|
---|
205 | #define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, statetypes, statereferences, ;)
|
---|
206 | #define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES-1)
|
---|
207 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
208 | #endif
|
---|
209 | ACTION::PARAMS params;
|
---|
210 | };
|
---|
211 | #endif /* statenecessary */
|
---|
212 |
|
---|
213 | // (const) prototype to be placed into the ActionRegistry (must be deleted by registry itself)
|
---|
214 | //const ACTION INSTANCE;
|
---|
215 | //boost::shared_ptr< ACTION > INSTANCE( new ACTION() );
|
---|
216 |
|
---|
217 | // =========== constructor ===========
|
---|
218 | ACTION::ACTION () :
|
---|
219 | Action(ActionTraits< ACTION >(), false)
|
---|
220 | {}
|
---|
221 |
|
---|
222 | // =========== destructor ===========
|
---|
223 | ACTION::~ACTION ()
|
---|
224 | {
|
---|
225 | //std::cout << "Action ACTION is being destroyed." << std::endl;
|
---|
226 | }
|
---|
227 |
|
---|
228 | // =========== parameter constructor ===========
|
---|
229 | ACTION::PARAMS::PARAMS()
|
---|
230 | #if defined paramtokens && defined paramreferences && defined paramvalids
|
---|
231 | :
|
---|
232 | #define BOOST_PP_LOCAL_MACRO(n) valid_print(~, n, paramtokens, paramreferences, paramvalids)
|
---|
233 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
234 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
235 | #endif
|
---|
236 | {}
|
---|
237 |
|
---|
238 | ACTION::PARAMS::PARAMS(const PARAMS &p)
|
---|
239 | #if defined paramtokens && defined paramreferences
|
---|
240 | :
|
---|
241 | #define BOOST_PP_LOCAL_MACRO(n) validcopy_print(~, n, paramtokens, paramreferences, p)
|
---|
242 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
243 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
244 | #endif
|
---|
245 | {}
|
---|
246 |
|
---|
247 | // =========== fill a dialog ===========
|
---|
248 | Dialog* ACTION::fillDialog(Dialog *dialog) {
|
---|
249 | ASSERT(dialog,"No Dialog given when filling actionname's dialog");
|
---|
250 | #if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
|
---|
251 | dialog->queryEmpty(TOKEN, Traits.getDescription());
|
---|
252 | #else
|
---|
253 | #define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
|
---|
254 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
255 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
256 | #endif
|
---|
257 | return dialog;
|
---|
258 | };
|
---|
259 |
|
---|
260 | // =========== time the action ===========
|
---|
261 | // we need this here to have the correct function name
|
---|
262 | void ACTION::startTimer() const { Chronos::getInstance().startTiming( std::string( TOKEN ) ); }
|
---|
263 | void ACTION::endTimer() const { Chronos::getInstance().endTiming( std::string( TOKEN ) ); }
|
---|
264 |
|
---|
265 | // =========== command for calling action directly ===========
|
---|
266 | void COMMAND(
|
---|
267 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
268 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
|
---|
269 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
270 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
271 | #endif
|
---|
272 | )
|
---|
273 | {
|
---|
274 | ACTION *ToCall = dynamic_cast<ACTION*>(ActionRegistry::getInstance().getActionByName( TOKEN )); //->clone(params);
|
---|
275 | //ACTION::PARAMS params;
|
---|
276 | #if defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
277 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, paramreferences, ToCall->params.)
|
---|
278 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
279 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
280 | #endif
|
---|
281 | ToCall->call(Action::NonInteractive);
|
---|
282 | };
|
---|
283 |
|
---|
284 | void BOOST_PP_CAT( COMMAND, _stringargs)(
|
---|
285 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
286 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences)
|
---|
287 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
288 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
289 | #endif
|
---|
290 | ) {
|
---|
291 | ACTION *ToCall = dynamic_cast<ACTION*>(ActionRegistry::getInstance().getActionByName( TOKEN )); //->clone(params);
|
---|
292 | //ACTION::PARAMS params;
|
---|
293 | #if defined paramtypes && defined paramtypes && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
294 | #define BOOST_PP_LOCAL_MACRO(n) valuetype_print(~, n, paramreferences, paramtypes, ToCall->params. )
|
---|
295 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
296 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
297 | #endif
|
---|
298 | ToCall->call(MoleCuilder::Action::NonInteractive);
|
---|
299 | };
|
---|
300 |
|
---|
301 | }
|
---|
302 |
|
---|
303 | // free up defines
|
---|
304 | #undef paramvalids
|
---|
305 | #undef paramtypes
|
---|
306 | #undef paramtokens
|
---|
307 | #undef paramreferences
|
---|
308 | #undef paramdescriptions
|
---|
309 | #undef paramdefaults
|
---|
310 | #undef MAXPARAMTYPES
|
---|
311 | #undef statetypes
|
---|
312 | #undef statereferences
|
---|
313 | #undef MAXSTATETYPES
|
---|
314 |
|
---|
315 | #undef type2string
|
---|
316 | #undef stringtype
|
---|
317 | #undef initialiser_print
|
---|
318 | #undef type_print
|
---|
319 | #undef type_list
|
---|
320 | #undef dialog_print
|
---|
321 | #undef valid_print
|
---|
322 | #undef validcopy_print
|
---|
323 | #undef value_print
|
---|
324 | #undef valuetype_print
|
---|
325 |
|
---|
326 | #undef ACTION
|
---|
327 | #undef COMMAND
|
---|
328 | #undef PARAMS
|
---|
329 | #undef STATE
|
---|
330 | #undef INSTANCE
|
---|
331 |
|
---|
332 | #undef ACTIONNAME
|
---|
333 | #undef CATEGORY
|
---|
334 | #undef TOKEN
|
---|