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