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