| 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 | // =========== clone Action ===========
|
|---|
| 203 | Action* REACTION::clone(enum QueryOptions flag) const
|
|---|
| 204 | {
|
|---|
| 205 | if (flag == Interactive)
|
|---|
| 206 | return new REACTION();
|
|---|
| 207 | else
|
|---|
| 208 | return new REACTION(*this);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | // =========== fill a dialog ===========
|
|---|
| 212 | Dialog* REACTION::fillDialog(Dialog *dialog) {
|
|---|
| 213 | ASSERT(dialog,"No Dialog given when filling actionname's dialog");
|
|---|
| 214 | #if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 215 | dialog->queryEmpty(TOKEN, Traits.getDescription());
|
|---|
| 216 | #else
|
|---|
| 217 | #define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
|
|---|
| 218 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 219 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 220 | #endif
|
|---|
| 221 | return dialog;
|
|---|
| 222 | };
|
|---|
| 223 |
|
|---|
| 224 | // =========== output as CLI ===========
|
|---|
| 225 | void REACTION::outputAsCLI(std::ostream &ost) const {
|
|---|
| 226 | // check whether TOKEN is also an option
|
|---|
| 227 | // is a bit ugly as preprocessor cannot compare strings
|
|---|
| 228 | bool status = true;
|
|---|
| 229 | #if defined paramtokens && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 230 | #define BOOST_PP_LOCAL_MACRO(n) checkpresenttoken_print(~, n, TOKEN, status)
|
|---|
| 231 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 232 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 233 | #endif
|
|---|
| 234 | if (status) {
|
|---|
| 235 | ost << "--" << TOKEN;
|
|---|
| 236 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 237 | ost << " ";
|
|---|
| 238 | #endif
|
|---|
| 239 | }
|
|---|
| 240 | // then print option along with each argument if set
|
|---|
| 241 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 242 | #define BOOST_PP_LOCAL_MACRO(n) outputAsCLI_print(~, n, ost)
|
|---|
| 243 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 244 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 245 | #endif
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | // =========== output as PYTHTON ===========
|
|---|
| 249 | void REACTION::outputAsPython(std::ostream &ost, const std::string &prefix) const {
|
|---|
| 250 | // print prefix and action command
|
|---|
| 251 | ost << prefix << "." << BOOST_PP_STRINGIZE( COMMAND ) << "(";
|
|---|
| 252 | // then print option along with each argument if set
|
|---|
| 253 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 254 | #define BOOST_PP_LOCAL_MACRO(n) outputAsCLI_print(~, n, ost)
|
|---|
| 255 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 256 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 257 | #endif
|
|---|
| 258 | ost << ")" << std::endl;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | // =========== command for calling action directly ===========
|
|---|
| 262 | RETURNTYPE COMMAND(
|
|---|
| 263 | #if defined paramtypes && defined paramreferences
|
|---|
| 264 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
|
|---|
| 265 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 266 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 267 | #endif
|
|---|
| 268 | )
|
|---|
| 269 | {
|
|---|
| 270 | Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
|
|---|
| 271 | //REACTION::PARAMS params;
|
|---|
| 272 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 273 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, setCurrentValue, )
|
|---|
| 274 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 275 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 276 | #endif
|
|---|
| 277 | ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
|
|---|
| 278 | RETURNTYPE result;
|
|---|
| 279 | try {
|
|---|
| 280 | const Reaction< RETURNTYPE > & resultaction =
|
|---|
| 281 | static_cast<const Reaction< RETURNTYPE > &>(ActionQueue::getInstance().getLastAction());
|
|---|
| 282 | ASSERT( resultaction.hasResult (),
|
|---|
| 283 | " _COMMAND - result is not done yet?");
|
|---|
| 284 | result = resultaction.getResult();
|
|---|
| 285 | } ASSERT_NOCATCH(" COMMAND - static cast to Reaction type failed.");
|
|---|
| 286 | return result;
|
|---|
| 287 | };
|
|---|
| 288 |
|
|---|
| 289 | RETURNTYPE BOOST_PP_CAT( COMMAND, _stringargs)(
|
|---|
| 290 | #if defined paramtypes && defined paramreferences
|
|---|
| 291 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences)
|
|---|
| 292 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 293 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 294 | #endif
|
|---|
| 295 | ) {
|
|---|
| 296 | Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
|
|---|
| 297 | //REACTION::PARAMS params;
|
|---|
| 298 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 299 | #define BOOST_PP_LOCAL_MACRO(n) valuetype_print(~, n, setCurrentValueByString, )
|
|---|
| 300 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 301 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 302 | #endif
|
|---|
| 303 | ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
|
|---|
| 304 | RETURNTYPE result;
|
|---|
| 305 | try {
|
|---|
| 306 | const Reaction< RETURNTYPE > & resultaction =
|
|---|
| 307 | static_cast<const Reaction< RETURNTYPE > &>(ActionQueue::getInstance().getLastAction());
|
|---|
| 308 | ASSERT( resultaction.hasResult (),
|
|---|
| 309 | " _COMMAND - result is not done yet?");
|
|---|
| 310 | result = resultaction.getResult();
|
|---|
| 311 | } ASSERT_NOCATCH(" COMMAND - static cast to Reaction type failed.");
|
|---|
| 312 | return result;
|
|---|
| 313 | };
|
|---|
| 314 |
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | // free up defines
|
|---|
| 318 | #undef paramtypes
|
|---|
| 319 | #undef paramtokens
|
|---|
| 320 | #undef paramreferences
|
|---|
| 321 | #undef MAXPARAMTYPES
|
|---|
| 322 |
|
|---|
| 323 | #undef returntype
|
|---|
| 324 | #undef RETURNTYPE
|
|---|
| 325 |
|
|---|
| 326 | #undef type2string
|
|---|
| 327 | #undef stringtype
|
|---|
| 328 | #undef initialiser_print
|
|---|
| 329 | #undef type_print
|
|---|
| 330 | #undef type_list
|
|---|
| 331 | #undef dialog_print
|
|---|
| 332 | #undef value_print
|
|---|
| 333 | #undef valuetype_print
|
|---|
| 334 |
|
|---|
| 335 | #undef REACTION
|
|---|
| 336 | #undef COMMAND
|
|---|
| 337 | #undef PARAMS
|
|---|
| 338 | #undef STATE
|
|---|
| 339 |
|
|---|
| 340 | #undef ACTIONNAME
|
|---|
| 341 | #undef CATEGORY
|
|---|
| 342 | #undef TOKEN
|
|---|