| 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, BOOST_PP_STRINGIZE( ) , BOOST_PP_STRINGIZE() ) \
|
|---|
| 133 | << "--" << \
|
|---|
| 134 | BOOST_PP_SEQ_ELEM(n, paramtokens) \
|
|---|
| 135 | << " \"" << toCLIString(params. \
|
|---|
| 136 | BOOST_PP_SEQ_ELEM(n, paramreferences) \
|
|---|
| 137 | .get()) \
|
|---|
| 138 | << "\"";
|
|---|
| 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 set/queryCurrentValue (command) for paramreferences and paramtokens
|
|---|
| 148 | #define value_print(z, n, container, prefix) \
|
|---|
| 149 | prefix \
|
|---|
| 150 | BOOST_PP_SEQ_ELEM(n, container)\
|
|---|
| 151 | .set(\
|
|---|
| 152 | BOOST_PP_SEQ_ELEM(n, container)\
|
|---|
| 153 | );
|
|---|
| 154 |
|
|---|
| 155 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
|
|---|
| 156 | #define valuetype_print(z,n,container, types, prefix) \
|
|---|
| 157 | prefix \
|
|---|
| 158 | BOOST_PP_SEQ_ELEM(n, container) \
|
|---|
| 159 | .setAsString( \
|
|---|
| 160 | BOOST_PP_SEQ_ELEM(n, container) \
|
|---|
| 161 | );
|
|---|
| 162 |
|
|---|
| 163 | #define stringtype std::string
|
|---|
| 164 |
|
|---|
| 165 | #define type2string(s, data, elem) \
|
|---|
| 166 | stringtype
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | #include "Actions/ActionRegistry.hpp"
|
|---|
| 170 | //#include "Actions/ActionTraits.hpp"
|
|---|
| 171 | #include "UIElements/Dialog.hpp"
|
|---|
| 172 |
|
|---|
| 173 | #ifdef paramtokens
|
|---|
| 174 | #define statenecessary 1
|
|---|
| 175 | #endif
|
|---|
| 176 | #ifndef statetokens
|
|---|
| 177 | #define statenecessary 1
|
|---|
| 178 | #endif
|
|---|
| 179 |
|
|---|
| 180 | namespace MoleCuilder {
|
|---|
| 181 |
|
|---|
| 182 | // =========== constructor ===========
|
|---|
| 183 | REACTION::REACTION () :
|
|---|
| 184 | Reaction< RETURNTYPE >(ActionTraits< REACTION >())
|
|---|
| 185 | {}
|
|---|
| 186 |
|
|---|
| 187 | // =========== destructor ===========
|
|---|
| 188 | REACTION::~REACTION ()
|
|---|
| 189 | {
|
|---|
| 190 | //std::cout << "Action REACTION is being destroyed." << std::endl;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | // =========== fill a dialog ===========
|
|---|
| 194 | Dialog* REACTION::fillDialog(Dialog *dialog) {
|
|---|
| 195 | ASSERT(dialog,"No Dialog given when filling actionname's dialog");
|
|---|
| 196 | #if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 197 | dialog->queryEmpty(TOKEN, Traits.getDescription());
|
|---|
| 198 | #else
|
|---|
| 199 | #define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
|
|---|
| 200 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 201 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 202 | #endif
|
|---|
| 203 | return dialog;
|
|---|
| 204 | };
|
|---|
| 205 |
|
|---|
| 206 | // =========== output as CLI ===========
|
|---|
| 207 | void REACTION::outputAsCLI(std::ostream &ost) const {
|
|---|
| 208 | // check whether TOKEN is also an option
|
|---|
| 209 | // is a bit ugly as preprocessor cannot compare strings
|
|---|
| 210 | bool status = true;
|
|---|
| 211 | #if defined paramtokens && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 212 | #define BOOST_PP_LOCAL_MACRO(n) checkpresenttoken_print(~, n, TOKEN, status)
|
|---|
| 213 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 214 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 215 | #endif
|
|---|
| 216 | if (status) { ost << "--" << TOKEN; }
|
|---|
| 217 | // then print option along with each argument if set
|
|---|
| 218 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 219 | #define BOOST_PP_LOCAL_MACRO(n) outputAsCLI_print(~, n, ost)
|
|---|
| 220 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 221 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 222 | #endif
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | // =========== command for calling action directly ===========
|
|---|
| 226 | RETURNTYPE COMMAND(
|
|---|
| 227 | #if defined paramtypes && defined paramreferences
|
|---|
| 228 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
|
|---|
| 229 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 230 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 231 | #endif
|
|---|
| 232 | )
|
|---|
| 233 | {
|
|---|
| 234 | Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
|
|---|
| 235 | //REACTION::PARAMS params;
|
|---|
| 236 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 237 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, setCurrentValue, )
|
|---|
| 238 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 239 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 240 | #endif
|
|---|
| 241 | ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
|
|---|
| 242 | Reaction< RETURNTYPE > * resultaction = static_cast<Reaction< RETURNTYPE > *>(ToCall);
|
|---|
| 243 | ASSERT( resultaction != NULL,
|
|---|
| 244 | " COMMAND - static cast to Reaction type failed.");
|
|---|
| 245 | ASSERT( resultaction->hasResult (),
|
|---|
| 246 | " _COMMAND - result is not done yet?");
|
|---|
| 247 | return resultaction->getResult();
|
|---|
| 248 | };
|
|---|
| 249 |
|
|---|
| 250 | RETURNTYPE BOOST_PP_CAT( COMMAND, _stringargs)(
|
|---|
| 251 | #if defined paramtypes && defined paramreferences
|
|---|
| 252 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences)
|
|---|
| 253 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 254 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 255 | #endif
|
|---|
| 256 | ) {
|
|---|
| 257 | Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
|
|---|
| 258 | //REACTION::PARAMS params;
|
|---|
| 259 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
|---|
| 260 | #define BOOST_PP_LOCAL_MACRO(n) valuetype_print(~, n, setCurrentValueByString, )
|
|---|
| 261 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
|---|
| 262 | #include BOOST_PP_LOCAL_ITERATE()
|
|---|
| 263 | #endif
|
|---|
| 264 | ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
|
|---|
| 265 | Reaction< RETURNTYPE > * resultaction = static_cast<Reaction< RETURNTYPE > *>(ToCall);
|
|---|
| 266 | ASSERT( resultaction != NULL,
|
|---|
| 267 | " COMMAND - static cast to Reaction type failed.");
|
|---|
| 268 | ASSERT( resultaction->hasResult (),
|
|---|
| 269 | " _COMMAND - result is not done yet?");
|
|---|
| 270 | return resultaction->getResult();
|
|---|
| 271 | };
|
|---|
| 272 |
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | // free up defines
|
|---|
| 276 | #undef paramtypes
|
|---|
| 277 | #undef paramtokens
|
|---|
| 278 | #undef paramreferences
|
|---|
| 279 | #undef MAXPARAMTYPES
|
|---|
| 280 |
|
|---|
| 281 | #undef returntype
|
|---|
| 282 | #undef RETURNTYPE
|
|---|
| 283 |
|
|---|
| 284 | #undef type2string
|
|---|
| 285 | #undef stringtype
|
|---|
| 286 | #undef initialiser_print
|
|---|
| 287 | #undef type_print
|
|---|
| 288 | #undef type_list
|
|---|
| 289 | #undef dialog_print
|
|---|
| 290 | #undef value_print
|
|---|
| 291 | #undef valuetype_print
|
|---|
| 292 |
|
|---|
| 293 | #undef REACTION
|
|---|
| 294 | #undef COMMAND
|
|---|
| 295 | #undef PARAMS
|
|---|
| 296 | #undef STATE
|
|---|
| 297 |
|
|---|
| 298 | #undef ACTIONNAME
|
|---|
| 299 | #undef CATEGORY
|
|---|
| 300 | #undef TOKEN
|
|---|