1 | /*
|
---|
2 | * Reaction_impl.hpp
|
---|
3 | *
|
---|
4 | * Created on: Oct 13, 2011
|
---|
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 <boost/preprocessor/cat.hpp>
|
---|
34 | #include <boost/preprocessor/comparison/equal.hpp>
|
---|
35 | #include <boost/preprocessor/comparison/not_equal.hpp>
|
---|
36 | #include <boost/preprocessor/debug/assert.hpp>
|
---|
37 | #include <boost/preprocessor/iteration/local.hpp>
|
---|
38 | #include <boost/preprocessor/punctuation/comma_if.hpp>
|
---|
39 | #include <boost/preprocessor/seq/elem.hpp>
|
---|
40 | #include <boost/preprocessor/seq/seq.hpp>
|
---|
41 | #include <boost/preprocessor/seq/size.hpp>
|
---|
42 | #include <boost/preprocessor/seq/transform.hpp>
|
---|
43 |
|
---|
44 | #include "Actions/ActionQueue.hpp"
|
---|
45 | #include "Actions/toCLIString.hpp"
|
---|
46 |
|
---|
47 | // some derived names: if CATEGORY is not given, we don't prefix with it
|
---|
48 | #ifdef CATEGORY
|
---|
49 | #define REACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
|
---|
50 | #define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
|
---|
51 | #define STATE BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, State))
|
---|
52 | #define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
|
---|
53 | #else
|
---|
54 | #define REACTION BOOST_PP_CAT(ACTIONNAME, Action)
|
---|
55 | #define COMMAND ACTIONNAME
|
---|
56 | #define STATE BOOST_PP_CAT(ACTIONNAME, State)
|
---|
57 | #define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | // check if no lists given
|
---|
61 | #ifndef paramtypes
|
---|
62 | #define MAXPARAMTYPES 0
|
---|
63 | #else
|
---|
64 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
|
---|
65 | #endif
|
---|
66 | #ifndef statetypes
|
---|
67 | #define MAXSTATETYPES 0
|
---|
68 | #else
|
---|
69 | #define MAXSTATETYPES BOOST_PP_SEQ_SIZE(statetypes)
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | // check user has given name and category
|
---|
73 | #ifndef ACTIONNAME
|
---|
74 | ERROR: No "ACTIONNAME" defined in: __FILE__
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | // calculate numbers and check whether all have same size
|
---|
78 | #ifdef paramtokens
|
---|
79 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
|
---|
80 | ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
|
---|
81 | )
|
---|
82 | #endif
|
---|
83 | #ifdef paramreferences
|
---|
84 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
|
---|
85 | ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
|
---|
86 | )
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | #ifdef statetypes
|
---|
90 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXSTATETYPES, BOOST_PP_SEQ_SIZE(statereferences)),\
|
---|
91 | ERROR: There are not the same number of "statetypes" and "statereferences" in: __FILE__ \
|
---|
92 | )
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | // set the return type
|
---|
96 | #ifndef returntype
|
---|
97 | BOOST_PP_ASSERT_MSG(0,\
|
---|
98 | ERROR: No returntype is defined in __FILE__ \
|
---|
99 | )
|
---|
100 | #endif
|
---|
101 | #define RETURNTYPE returntype
|
---|
102 |
|
---|
103 | // print a list of type ref followed by a separator, i.e. "int i;"
|
---|
104 | #define initialiser_print(z,n,initialiserlist) \
|
---|
105 | BOOST_PP_SEQ_ELEM(n, initialiserlist) \
|
---|
106 | (BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(n, initialiserlist))),
|
---|
107 |
|
---|
108 | // print a list of ref(_ref) followed by a separator, i.e. "id(_id),"
|
---|
109 | #define type_print(z,n,TYPELIST, VARLIST, separator) \
|
---|
110 | BOOST_PP_SEQ_ELEM(n, TYPELIST) \
|
---|
111 | BOOST_PP_SEQ_ELEM(n, VARLIST)\
|
---|
112 | separator
|
---|
113 |
|
---|
114 | // print a list of type ref followed, i.e. "int i, double position"
|
---|
115 | #define type_list(z,n,TYPELIST,VARLIST) \
|
---|
116 | BOOST_PP_COMMA_IF(n)\
|
---|
117 | BOOST_PP_SEQ_ELEM(n, TYPELIST) \
|
---|
118 | BOOST_PP_SEQ_ELEM(n, VARLIST)
|
---|
119 |
|
---|
120 | // prints dialog->query calls for paramtypes with tokens
|
---|
121 | #define dialog_print(z,n,unused) \
|
---|
122 | dialog->query<\
|
---|
123 | BOOST_PP_SEQ_ELEM(n, paramtypes)\
|
---|
124 | >(\
|
---|
125 | BOOST_PP_SEQ_ELEM(n, paramtokens)\
|
---|
126 | , Traits.getDescription()\
|
---|
127 | );
|
---|
128 |
|
---|
129 | // prints command line call for this Action for paramtypes with tokens
|
---|
130 | #define outputAsCLI_print(z,n,output) \
|
---|
131 | output << \
|
---|
132 | BOOST_PP_IF(n, " --", "--") \
|
---|
133 | << \
|
---|
134 | BOOST_PP_SEQ_ELEM(n, paramtokens) \
|
---|
135 | << " " << toCLIString(params. \
|
---|
136 | BOOST_PP_SEQ_ELEM(n, paramreferences) \
|
---|
137 | .get());
|
---|
138 |
|
---|
139 | // prints if statement to check two strings (paramtokens[n] vs. TOKEN)
|
---|
140 | #define checkpresenttoken_print(z, n, TOKEN, booltoken) \
|
---|
141 | if ( std::string(\
|
---|
142 | BOOST_PP_SEQ_ELEM(n, paramtokens) )\
|
---|
143 | == getName()) \
|
---|
144 | booltoken = false;
|
---|
145 |
|
---|
146 | // prints command line call for this Action for paramtypes with tokens
|
---|
147 | #define outputAsPython_print(z,n,output) \
|
---|
148 | output << \
|
---|
149 | BOOST_PP_IF(n, ", ", "") \
|
---|
150 | << "\"" << toCLIString(params. \
|
---|
151 | BOOST_PP_SEQ_ELEM(n, paramreferences) \
|
---|
152 | .get()) \
|
---|
153 | << "\"";
|
---|
154 |
|
---|
155 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
|
---|
156 | #define value_print(z, n, container, prefix) \
|
---|
157 | prefix \
|
---|
158 | BOOST_PP_SEQ_ELEM(n, container)\
|
---|
159 | .set(\
|
---|
160 | BOOST_PP_SEQ_ELEM(n, container)\
|
---|
161 | );
|
---|
162 |
|
---|
163 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
|
---|
164 | #define valuetype_print(z,n,container, types, prefix) \
|
---|
165 | prefix \
|
---|
166 | BOOST_PP_SEQ_ELEM(n, container) \
|
---|
167 | .setAsString( \
|
---|
168 | BOOST_PP_SEQ_ELEM(n, container) \
|
---|
169 | );
|
---|
170 |
|
---|
171 | #define stringtype std::string
|
---|
172 |
|
---|
173 | #define type2string(s, data, elem) \
|
---|
174 | stringtype
|
---|
175 |
|
---|
176 |
|
---|
177 | #include "Actions/ActionRegistry.hpp"
|
---|
178 | //#include "Actions/ActionTraits.hpp"
|
---|
179 | #include "UIElements/Dialog.hpp"
|
---|
180 |
|
---|
181 | #ifdef paramtokens
|
---|
182 | #define statenecessary 1
|
---|
183 | #endif
|
---|
184 | #ifndef statetokens
|
---|
185 | #define statenecessary 1
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | namespace MoleCuilder {
|
---|
189 |
|
---|
190 | // =========== constructor ===========
|
---|
191 | REACTION::REACTION () :
|
---|
192 | Reaction< RETURNTYPE >(ActionTraits< REACTION >())
|
---|
193 | {}
|
---|
194 |
|
---|
195 | // =========== destructor ===========
|
---|
196 | REACTION::~REACTION ()
|
---|
197 | {
|
---|
198 | //std::cout << "Action REACTION is being destroyed." << std::endl;
|
---|
199 | }
|
---|
200 |
|
---|
201 | // =========== fill a dialog ===========
|
---|
202 | Dialog* REACTION::fillDialog(Dialog *dialog) {
|
---|
203 | ASSERT(dialog,"No Dialog given when filling actionname's dialog");
|
---|
204 | #if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
|
---|
205 | dialog->queryEmpty(TOKEN, Traits.getDescription());
|
---|
206 | #else
|
---|
207 | #define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
|
---|
208 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
209 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
210 | #endif
|
---|
211 | return dialog;
|
---|
212 | };
|
---|
213 |
|
---|
214 | // =========== output as CLI ===========
|
---|
215 | void REACTION::outputAsCLI(std::ostream &ost) const {
|
---|
216 | // check whether TOKEN is also an option
|
---|
217 | // is a bit ugly as preprocessor cannot compare strings
|
---|
218 | bool status = true;
|
---|
219 | #if defined paramtokens && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
220 | #define BOOST_PP_LOCAL_MACRO(n) checkpresenttoken_print(~, n, TOKEN, status)
|
---|
221 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
222 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
223 | #endif
|
---|
224 | if (status) {
|
---|
225 | ost << "--" << TOKEN;
|
---|
226 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
227 | ost << " ";
|
---|
228 | #endif
|
---|
229 | }
|
---|
230 | // then print option along with each argument if set
|
---|
231 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
232 | #define BOOST_PP_LOCAL_MACRO(n) outputAsCLI_print(~, n, ost)
|
---|
233 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
234 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
235 | #endif
|
---|
236 | }
|
---|
237 |
|
---|
238 | // =========== output as PYTHTON ===========
|
---|
239 | void REACTION::outputAsPython(std::ostream &ost, const std::string &prefix) const {
|
---|
240 | // print prefix and action command
|
---|
241 | ost << prefix << "." << BOOST_PP_STRINGIZE( COMMAND ) << "(";
|
---|
242 | // then print option along with each argument if set
|
---|
243 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
244 | #define BOOST_PP_LOCAL_MACRO(n) outputAsCLI_print(~, n, ost)
|
---|
245 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
246 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
247 | #endif
|
---|
248 | ost << ")" << std::endl;
|
---|
249 | }
|
---|
250 |
|
---|
251 | // =========== command for calling action directly ===========
|
---|
252 | RETURNTYPE COMMAND(
|
---|
253 | #if defined paramtypes && defined paramreferences
|
---|
254 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
|
---|
255 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
256 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
257 | #endif
|
---|
258 | )
|
---|
259 | {
|
---|
260 | Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
|
---|
261 | //REACTION::PARAMS params;
|
---|
262 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
263 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, setCurrentValue, )
|
---|
264 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
265 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
266 | #endif
|
---|
267 | ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
|
---|
268 | Reaction< RETURNTYPE > * resultaction = static_cast<Reaction< RETURNTYPE > *>(ToCall);
|
---|
269 | ASSERT( resultaction != NULL,
|
---|
270 | " COMMAND - static cast to Reaction type failed.");
|
---|
271 | ASSERT( resultaction->hasResult (),
|
---|
272 | " _COMMAND - result is not done yet?");
|
---|
273 | return resultaction->getResult();
|
---|
274 | };
|
---|
275 |
|
---|
276 | RETURNTYPE BOOST_PP_CAT( COMMAND, _stringargs)(
|
---|
277 | #if defined paramtypes && defined paramreferences
|
---|
278 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences)
|
---|
279 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
280 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
281 | #endif
|
---|
282 | ) {
|
---|
283 | Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
|
---|
284 | //REACTION::PARAMS params;
|
---|
285 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
286 | #define BOOST_PP_LOCAL_MACRO(n) valuetype_print(~, n, setCurrentValueByString, )
|
---|
287 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
288 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
289 | #endif
|
---|
290 | ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
|
---|
291 | Reaction< RETURNTYPE > * resultaction = static_cast<Reaction< RETURNTYPE > *>(ToCall);
|
---|
292 | ASSERT( resultaction != NULL,
|
---|
293 | " COMMAND - static cast to Reaction type failed.");
|
---|
294 | ASSERT( resultaction->hasResult (),
|
---|
295 | " _COMMAND - result is not done yet?");
|
---|
296 | return resultaction->getResult();
|
---|
297 | };
|
---|
298 |
|
---|
299 | }
|
---|
300 |
|
---|
301 | // free up defines
|
---|
302 | #undef paramtypes
|
---|
303 | #undef paramtokens
|
---|
304 | #undef paramreferences
|
---|
305 | #undef MAXPARAMTYPES
|
---|
306 |
|
---|
307 | #undef returntype
|
---|
308 | #undef RETURNTYPE
|
---|
309 |
|
---|
310 | #undef type2string
|
---|
311 | #undef stringtype
|
---|
312 | #undef initialiser_print
|
---|
313 | #undef type_print
|
---|
314 | #undef type_list
|
---|
315 | #undef dialog_print
|
---|
316 | #undef value_print
|
---|
317 | #undef valuetype_print
|
---|
318 |
|
---|
319 | #undef REACTION
|
---|
320 | #undef COMMAND
|
---|
321 | #undef PARAMS
|
---|
322 | #undef STATE
|
---|
323 |
|
---|
324 | #undef ACTIONNAME
|
---|
325 | #undef CATEGORY
|
---|
326 | #undef TOKEN
|
---|