| 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 | 
 | 
|---|
| 46 | // some derived names: if CATEGORY is not given, we don't prefix with it
 | 
|---|
| 47 | #ifdef CATEGORY
 | 
|---|
| 48 | #define REACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
 | 
|---|
| 49 | #define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
 | 
|---|
| 50 | #define STATE BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, State))
 | 
|---|
| 51 | #define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
 | 
|---|
| 52 | #else
 | 
|---|
| 53 | #define REACTION BOOST_PP_CAT(ACTIONNAME, Action)
 | 
|---|
| 54 | #define COMMAND ACTIONNAME
 | 
|---|
| 55 | #define STATE BOOST_PP_CAT(ACTIONNAME, State)
 | 
|---|
| 56 | #define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
 | 
|---|
| 57 | #endif
 | 
|---|
| 58 | 
 | 
|---|
| 59 | // check if no lists given
 | 
|---|
| 60 | #ifndef paramtypes
 | 
|---|
| 61 | #define MAXPARAMTYPES 0
 | 
|---|
| 62 | #else
 | 
|---|
| 63 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
 | 
|---|
| 64 | #endif
 | 
|---|
| 65 | #ifndef statetypes
 | 
|---|
| 66 | #define MAXSTATETYPES 0
 | 
|---|
| 67 | #else
 | 
|---|
| 68 | #define MAXSTATETYPES BOOST_PP_SEQ_SIZE(statetypes)
 | 
|---|
| 69 | #endif
 | 
|---|
| 70 | 
 | 
|---|
| 71 | // check user has given name and category
 | 
|---|
| 72 | #ifndef ACTIONNAME
 | 
|---|
| 73 | ERROR: No "ACTIONNAME" defined in: __FILE__
 | 
|---|
| 74 | #endif
 | 
|---|
| 75 | 
 | 
|---|
| 76 | // calculate numbers and check whether all have same size
 | 
|---|
| 77 | #ifdef paramtokens
 | 
|---|
| 78 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
 | 
|---|
| 79 |   ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
 | 
|---|
| 80 | )
 | 
|---|
| 81 | #endif
 | 
|---|
| 82 | #ifdef paramreferences
 | 
|---|
| 83 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
 | 
|---|
| 84 |   ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
 | 
|---|
| 85 | )
 | 
|---|
| 86 | #endif
 | 
|---|
| 87 | 
 | 
|---|
| 88 | #ifdef statetypes
 | 
|---|
| 89 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXSTATETYPES, BOOST_PP_SEQ_SIZE(statereferences)),\
 | 
|---|
| 90 |   ERROR: There are not the same number of "statetypes" and "statereferences" in: __FILE__ \
 | 
|---|
| 91 | )
 | 
|---|
| 92 | #endif
 | 
|---|
| 93 | 
 | 
|---|
| 94 | // set the return type
 | 
|---|
| 95 | #ifndef returntype
 | 
|---|
| 96 | BOOST_PP_ASSERT_MSG(0,\
 | 
|---|
| 97 |   ERROR: No returntype is defined in __FILE__ \
 | 
|---|
| 98 | )
 | 
|---|
| 99 | #endif
 | 
|---|
| 100 | #define RETURNTYPE returntype
 | 
|---|
| 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 |   >(\
 | 
|---|
| 124 |   BOOST_PP_SEQ_ELEM(n, paramtokens)\
 | 
|---|
| 125 |   , Traits.getDescription()\
 | 
|---|
| 126 |   );
 | 
|---|
| 127 | 
 | 
|---|
| 128 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
 | 
|---|
| 129 | #define value_print(z, n, container, prefix) \
 | 
|---|
| 130 |   prefix \
 | 
|---|
| 131 |   BOOST_PP_SEQ_ELEM(n, container)\
 | 
|---|
| 132 |   .set(\
 | 
|---|
| 133 |   BOOST_PP_SEQ_ELEM(n, container)\
 | 
|---|
| 134 |   );
 | 
|---|
| 135 | 
 | 
|---|
| 136 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
 | 
|---|
| 137 | #define valuetype_print(z,n,container, types, prefix) \
 | 
|---|
| 138 |   prefix \
 | 
|---|
| 139 |   BOOST_PP_SEQ_ELEM(n, container) \
 | 
|---|
| 140 |   .setAsString( \
 | 
|---|
| 141 |   BOOST_PP_SEQ_ELEM(n, container) \
 | 
|---|
| 142 |   );
 | 
|---|
| 143 | 
 | 
|---|
| 144 | #define stringtype std::string
 | 
|---|
| 145 | 
 | 
|---|
| 146 | #define type2string(s, data, elem) \
 | 
|---|
| 147 |         stringtype
 | 
|---|
| 148 | 
 | 
|---|
| 149 | 
 | 
|---|
| 150 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 151 | //#include "Actions/ActionTraits.hpp"
 | 
|---|
| 152 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 153 | 
 | 
|---|
| 154 | #ifdef paramtokens
 | 
|---|
| 155 | #define statenecessary 1
 | 
|---|
| 156 | #endif
 | 
|---|
| 157 | #ifndef statetokens
 | 
|---|
| 158 | #define statenecessary 1
 | 
|---|
| 159 | #endif
 | 
|---|
| 160 | 
 | 
|---|
| 161 | namespace MoleCuilder {
 | 
|---|
| 162 | 
 | 
|---|
| 163 | // =========== constructor ===========
 | 
|---|
| 164 | REACTION::REACTION () :
 | 
|---|
| 165 |   Reaction< RETURNTYPE >(ActionTraits< REACTION >(), NULL)
 | 
|---|
| 166 | {}
 | 
|---|
| 167 | 
 | 
|---|
| 168 | // =========== destructor ===========
 | 
|---|
| 169 | REACTION::~REACTION ()
 | 
|---|
| 170 | {
 | 
|---|
| 171 |   //std::cout << "Action REACTION is being destroyed." << std::endl;
 | 
|---|
| 172 | }
 | 
|---|
| 173 | 
 | 
|---|
| 174 | // =========== fill a dialog ===========
 | 
|---|
| 175 | Dialog* REACTION::fillDialog(Dialog *dialog) {
 | 
|---|
| 176 |         ASSERT(dialog,"No Dialog given when filling actionname's dialog");
 | 
|---|
| 177 | #if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
 | 
|---|
| 178 |         dialog->queryEmpty(TOKEN, Traits.getDescription());
 | 
|---|
| 179 | #else
 | 
|---|
| 180 | #define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
 | 
|---|
| 181 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 182 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 183 | #endif
 | 
|---|
| 184 |         return dialog;
 | 
|---|
| 185 | };
 | 
|---|
| 186 | 
 | 
|---|
| 187 | // =========== command for calling action directly ===========
 | 
|---|
| 188 | RETURNTYPE COMMAND(
 | 
|---|
| 189 | #if defined paramtypes && defined paramreferences
 | 
|---|
| 190 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
 | 
|---|
| 191 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 192 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 193 | #endif
 | 
|---|
| 194 | )
 | 
|---|
| 195 | {
 | 
|---|
| 196 |   Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
 | 
|---|
| 197 |   //REACTION::PARAMS params;
 | 
|---|
| 198 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
 | 
|---|
| 199 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, setCurrentValue, )
 | 
|---|
| 200 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 201 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 202 | #endif
 | 
|---|
| 203 |   ToCall->call(Action::NonInteractive);
 | 
|---|
| 204 | 
 | 
|---|
| 205 |   return static_cast<Reaction< RETURNTYPE > *>(ToCall)->getResult();
 | 
|---|
| 206 | };
 | 
|---|
| 207 | 
 | 
|---|
| 208 | RETURNTYPE BOOST_PP_CAT( COMMAND, _stringargs)(
 | 
|---|
| 209 | #if defined paramtypes && defined paramreferences
 | 
|---|
| 210 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences)
 | 
|---|
| 211 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 212 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 213 | #endif
 | 
|---|
| 214 |         ) {
 | 
|---|
| 215 |   Action *ToCall = ActionQueue::getInstance().getActionByName( TOKEN ); //->clone(params);
 | 
|---|
| 216 |   //REACTION::PARAMS params;
 | 
|---|
| 217 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
 | 
|---|
| 218 | #define BOOST_PP_LOCAL_MACRO(n) valuetype_print(~, n, setCurrentValueByString, )
 | 
|---|
| 219 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 220 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 221 | #endif
 | 
|---|
| 222 |   ToCall->call(MoleCuilder::Action::NonInteractive);
 | 
|---|
| 223 | 
 | 
|---|
| 224 |   return static_cast<Reaction< RETURNTYPE > *>(ToCall)->getResult();
 | 
|---|
| 225 | };
 | 
|---|
| 226 | 
 | 
|---|
| 227 | }
 | 
|---|
| 228 | 
 | 
|---|
| 229 | // free up defines
 | 
|---|
| 230 | #undef paramtypes
 | 
|---|
| 231 | #undef paramtokens
 | 
|---|
| 232 | #undef paramreferences
 | 
|---|
| 233 | #undef MAXPARAMTYPES
 | 
|---|
| 234 | 
 | 
|---|
| 235 | #undef returntype
 | 
|---|
| 236 | #undef RETURNTYPE
 | 
|---|
| 237 | 
 | 
|---|
| 238 | #undef type2string
 | 
|---|
| 239 | #undef stringtype
 | 
|---|
| 240 | #undef initialiser_print
 | 
|---|
| 241 | #undef type_print
 | 
|---|
| 242 | #undef type_list
 | 
|---|
| 243 | #undef dialog_print
 | 
|---|
| 244 | #undef value_print
 | 
|---|
| 245 | #undef valuetype_print
 | 
|---|
| 246 | 
 | 
|---|
| 247 | #undef REACTION
 | 
|---|
| 248 | #undef COMMAND
 | 
|---|
| 249 | #undef PARAMS
 | 
|---|
| 250 | #undef STATE
 | 
|---|
| 251 | 
 | 
|---|
| 252 | #undef ACTIONNAME
 | 
|---|
| 253 | #undef CATEGORY
 | 
|---|
| 254 | #undef TOKEN
 | 
|---|