[0b2ce9] | 1 | /*
|
---|
| 2 | * Action_impl.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Aug 25, 2010
|
---|
| 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
|
---|
[b4fa106] | 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
|
---|
[0b2ce9] | 24 | * -# CATEGORY, e.g. Analysis
|
---|
| 25 | * -# ACTIONNAME, e.g. MolecularVolume
|
---|
| 26 | */
|
---|
| 27 |
|
---|
[56f73b] | 28 | // include config.h
|
---|
| 29 | #ifdef HAVE_CONFIG_H
|
---|
| 30 | #include <config.h>
|
---|
| 31 | #endif
|
---|
[9ee38b] | 32 |
|
---|
[862b6a] | 33 | #include "CodePatterns/Chronos.hpp"
|
---|
| 34 |
|
---|
[9ee38b] | 35 | #include <boost/preprocessor/cat.hpp>
|
---|
[b4fa106] | 36 | #include <boost/preprocessor/expand.hpp>
|
---|
[9ee38b] | 37 | #include <boost/preprocessor/comparison/equal.hpp>
|
---|
| 38 | #include <boost/preprocessor/comparison/not_equal.hpp>
|
---|
| 39 | #include <boost/preprocessor/control/if.hpp>
|
---|
| 40 | #include <boost/preprocessor/debug/assert.hpp>
|
---|
| 41 | #include <boost/preprocessor/iteration/local.hpp>
|
---|
| 42 | #include <boost/preprocessor/punctuation/comma_if.hpp>
|
---|
| 43 | #include <boost/preprocessor/repetition/repeat.hpp>
|
---|
| 44 | #include <boost/preprocessor/seq/elem.hpp>
|
---|
| 45 | #include <boost/preprocessor/seq/push_back.hpp>
|
---|
| 46 | #include <boost/preprocessor/seq/seq.hpp>
|
---|
| 47 | #include <boost/preprocessor/seq/size.hpp>
|
---|
| 48 | #include <boost/preprocessor/seq/transform.hpp>
|
---|
| 49 |
|
---|
[f10b0c] | 50 | #include "Parameters/Parameter.hpp"
|
---|
| 51 |
|
---|
[649aaa] | 52 |
|
---|
[e4afb4] | 53 | // some derived names: if CATEGORY is not given, we don't prefix with it
|
---|
| 54 | #ifdef CATEGORY
|
---|
[9ee38b] | 55 | #define ACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
|
---|
| 56 | #define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
|
---|
| 57 | #define STATE BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, State))
|
---|
| 58 | #define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
|
---|
[e4afb4] | 59 | #else
|
---|
| 60 | #define ACTION BOOST_PP_CAT(ACTIONNAME, Action)
|
---|
| 61 | #define COMMAND ACTIONNAME
|
---|
| 62 | #define STATE BOOST_PP_CAT(ACTIONNAME, State)
|
---|
| 63 | #define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
|
---|
| 64 | #endif
|
---|
[2a6a2c] | 65 | #define INSTANCE BOOST_PP_CAT(this_, BOOST_PP_CAT(ACTIONNAME, _instance))
|
---|
[9ee38b] | 66 |
|
---|
| 67 | // check if no lists given
|
---|
[b4fa106] | 68 | #ifndef paramtypes
|
---|
| 69 | #define MAXPARAMTYPES 0
|
---|
[9ee38b] | 70 | #else
|
---|
[b4fa106] | 71 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
|
---|
| 72 | #endif
|
---|
| 73 | #ifndef statetypes
|
---|
| 74 | #define MAXSTATETYPES 0
|
---|
| 75 | #else
|
---|
| 76 | #define MAXSTATETYPES BOOST_PP_SEQ_SIZE(statetypes)
|
---|
[9ee38b] | 77 | #endif
|
---|
| 78 |
|
---|
| 79 | // check user has given name and category
|
---|
| 80 | #ifndef ACTIONNAME
|
---|
| 81 | ERROR: No "ACTIONNAME" defined in: __FILE__
|
---|
| 82 | #endif
|
---|
| 83 |
|
---|
| 84 | // calculate numbers and check whether all have same size
|
---|
[b4fa106] | 85 | #ifdef paramtokens
|
---|
| 86 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
|
---|
| 87 | ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
|
---|
[9ee38b] | 88 | )
|
---|
| 89 | #endif
|
---|
[b4fa106] | 90 | #ifdef paramreferences
|
---|
| 91 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
|
---|
| 92 | ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
|
---|
| 93 | )
|
---|
| 94 | #endif
|
---|
| 95 |
|
---|
| 96 | #ifdef statetypes
|
---|
| 97 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXSTATETYPES, BOOST_PP_SEQ_SIZE(statereferences)),\
|
---|
| 98 | ERROR: There are not the same number of "statetypes" and "statereferences" in: __FILE__ \
|
---|
[9ee38b] | 99 | )
|
---|
| 100 | #endif
|
---|
| 101 |
|
---|
| 102 | // print a list of type ref followed by a separator, i.e. "int i;"
|
---|
[b4fa106] | 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)\
|
---|
[9ee38b] | 111 | separator
|
---|
| 112 |
|
---|
| 113 | // print a list of type ref followed, i.e. "int i, double position"
|
---|
[b4fa106] | 114 | #define type_list(z,n,TYPELIST,VARLIST) \
|
---|
[9ee38b] | 115 | BOOST_PP_COMMA_IF(n)\
|
---|
[b4fa106] | 116 | BOOST_PP_SEQ_ELEM(n, TYPELIST) \
|
---|
| 117 | BOOST_PP_SEQ_ELEM(n, VARLIST)
|
---|
[9ee38b] | 118 |
|
---|
[b4fa106] | 119 | // prints dialog->query calls for paramtypes with tokens
|
---|
[9ee38b] | 120 | #define dialog_print(z,n,unused) \
|
---|
| 121 | dialog->query<\
|
---|
[b4fa106] | 122 | BOOST_PP_SEQ_ELEM(n, paramtypes)\
|
---|
[f10b0c] | 123 | >( params. \
|
---|
| 124 | BOOST_PP_SEQ_ELEM(n, paramreferences)\
|
---|
| 125 | ,\
|
---|
[b4fa106] | 126 | BOOST_PP_SEQ_ELEM(n, paramtokens)\
|
---|
[e4afb4] | 127 | , Traits.getDescription()\
|
---|
| 128 | );
|
---|
[9ee38b] | 129 |
|
---|
[649aaa] | 130 | // print an initialiser list, i.e. "var( token, valid )(,)"
|
---|
| 131 | #define valid_print(z,n,TOKENLIST, VARLIST, VALIDLIST) \
|
---|
| 132 | BOOST_PP_COMMA_IF(n) \
|
---|
| 133 | BOOST_PP_SEQ_ELEM(n, VARLIST) \
|
---|
| 134 | ( \
|
---|
| 135 | BOOST_PP_SEQ_ELEM(n, TOKENLIST) \
|
---|
| 136 | , \
|
---|
| 137 | BOOST_PP_SEQ_ELEM(n, VALIDLIST) \
|
---|
| 138 | )
|
---|
| 139 |
|
---|
| 140 | // print an initialiser list, i.e. "var( token, valid )(,)"
|
---|
| 141 | #define validcopy_print(z,n,TOKENLIST, VARLIST, VALID) \
|
---|
| 142 | BOOST_PP_COMMA_IF(n) \
|
---|
| 143 | BOOST_PP_SEQ_ELEM(n, VARLIST) \
|
---|
| 144 | ( \
|
---|
| 145 | VALID . \
|
---|
| 146 | BOOST_PP_SEQ_ELEM(n, VARLIST) \
|
---|
| 147 | )
|
---|
| 148 |
|
---|
[9ee38b] | 149 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
|
---|
[f10b0c] | 150 | #define value_print(z, n, container, prefix) \
|
---|
| 151 | prefix \
|
---|
| 152 | BOOST_PP_SEQ_ELEM(n, container)\
|
---|
| 153 | .set(\
|
---|
| 154 | BOOST_PP_SEQ_ELEM(n, container)\
|
---|
[9ee38b] | 155 | );
|
---|
| 156 |
|
---|
[88ba1f] | 157 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
|
---|
[f10b0c] | 158 | #define valuetype_print(z,n,container, types, prefix) \
|
---|
| 159 | prefix \
|
---|
| 160 | BOOST_PP_SEQ_ELEM(n, container) \
|
---|
| 161 | .setAsString( \
|
---|
| 162 | BOOST_PP_SEQ_ELEM(n, container) \
|
---|
[88ba1f] | 163 | );
|
---|
| 164 |
|
---|
| 165 | #define stringtype std::string
|
---|
| 166 |
|
---|
| 167 | #define type2string(s, data, elem) \
|
---|
| 168 | stringtype
|
---|
| 169 |
|
---|
| 170 |
|
---|
[0b2ce9] | 171 | #include "Actions/ActionRegistry.hpp"
|
---|
[3139b2] | 172 | //#include "Actions/ActionTraits.hpp"
|
---|
[0b2ce9] | 173 | #include "UIElements/Dialog.hpp"
|
---|
| 174 |
|
---|
[e4afb4] | 175 | #ifdef paramtokens
|
---|
| 176 | #define statenecessary 1
|
---|
| 177 | #endif
|
---|
| 178 | #ifndef statetokens
|
---|
| 179 | #define statenecessary 1
|
---|
| 180 | #endif
|
---|
[9ee38b] | 181 |
|
---|
[ce7fdc] | 182 | namespace MoleCuilder {
|
---|
| 183 |
|
---|
[b4fa106] | 184 | // =========== memento to remember the state when undoing ===========
|
---|
[e4afb4] | 185 | #ifdef statenecessary
|
---|
[b4fa106] | 186 | class STATE : public ActionState {
|
---|
| 187 | public:
|
---|
| 188 | STATE(
|
---|
| 189 | #if defined statetypes && defined statereferences // if we have parameters, we have to add "_" before each reference and add the params as the last one
|
---|
| 190 | #define OP(s,data,elem) BOOST_PP_CAT(data, elem) // OP to add "_"
|
---|
| 191 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_PUSH_BACK(statetypes, const ACTION::PARAMS &), BOOST_PP_SEQ_TRANSFORM(OP, _, BOOST_PP_SEQ_PUSH_BACK(statereferences, params)))
|
---|
| 192 | #else /// if not, params is only list
|
---|
| 193 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, (const ACTION::PARAMS &), (_params))
|
---|
| 194 | #endif
|
---|
| 195 | #define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES)
|
---|
| 196 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 197 | ) :
|
---|
| 198 | #if defined statetypes && defined statereferences // do we have parameters at all?
|
---|
| 199 | BOOST_PP_REPEAT(MAXSTATETYPES, initialiser_print, statereferences)
|
---|
| 200 | #endif
|
---|
| 201 | params(_params)
|
---|
| 202 | {}
|
---|
[0b2ce9] | 203 |
|
---|
[b4fa106] | 204 | #if defined statetypes && defined statereferences // do we have parameters at all?
|
---|
| 205 | #define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, statetypes, statereferences, ;)
|
---|
| 206 | #define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES-1)
|
---|
| 207 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 208 | #endif
|
---|
| 209 | ACTION::PARAMS params;
|
---|
| 210 | };
|
---|
[e4afb4] | 211 | #endif /* statenecessary */
|
---|
[0b2ce9] | 212 |
|
---|
[2a6a2c] | 213 | // (const) prototype to be placed into the ActionRegistry (must be deleted by registry itself)
|
---|
[83e90c] | 214 | //const ACTION INSTANCE;
|
---|
| 215 | //boost::shared_ptr< ACTION > INSTANCE( new ACTION() );
|
---|
[2a6a2c] | 216 |
|
---|
[b4fa106] | 217 | // =========== constructor ===========
|
---|
[0b2ce9] | 218 | ACTION::ACTION () :
|
---|
[3139b2] | 219 | Action(ActionTraits< ACTION >(), false)
|
---|
[0b2ce9] | 220 | {}
|
---|
| 221 |
|
---|
[b4fa106] | 222 | // =========== destructor ===========
|
---|
[0b2ce9] | 223 | ACTION::~ACTION ()
|
---|
[e4afb4] | 224 | {
|
---|
| 225 | //std::cout << "Action ACTION is being destroyed." << std::endl;
|
---|
| 226 | }
|
---|
[0b2ce9] | 227 |
|
---|
[649aaa] | 228 | // =========== parameter constructor ===========
|
---|
| 229 | ACTION::PARAMS::PARAMS()
|
---|
| 230 | #if defined paramtokens && defined paramreferences && defined paramvalids
|
---|
| 231 | :
|
---|
| 232 | #define BOOST_PP_LOCAL_MACRO(n) valid_print(~, n, paramtokens, paramreferences, paramvalids)
|
---|
| 233 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
| 234 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 235 | #endif
|
---|
| 236 | {}
|
---|
| 237 |
|
---|
| 238 | ACTION::PARAMS::PARAMS(const PARAMS &p)
|
---|
| 239 | #if defined paramtokens && defined paramreferences
|
---|
| 240 | :
|
---|
| 241 | #define BOOST_PP_LOCAL_MACRO(n) validcopy_print(~, n, paramtokens, paramreferences, p)
|
---|
| 242 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
| 243 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 244 | #endif
|
---|
| 245 | {}
|
---|
| 246 |
|
---|
[b4fa106] | 247 | // =========== fill a dialog ===========
|
---|
[0b2ce9] | 248 | Dialog* ACTION::fillDialog(Dialog *dialog) {
|
---|
| 249 | ASSERT(dialog,"No Dialog given when filling actionname's dialog");
|
---|
[b4fa106] | 250 | #if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
|
---|
[e4afb4] | 251 | dialog->queryEmpty(TOKEN, Traits.getDescription());
|
---|
[0b2ce9] | 252 | #else
|
---|
| 253 | #define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
|
---|
[b4fa106] | 254 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
[0b2ce9] | 255 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 256 | #endif
|
---|
| 257 | return dialog;
|
---|
| 258 | };
|
---|
| 259 |
|
---|
[862b6a] | 260 | // =========== time the action ===========
|
---|
| 261 | // we need this here to have the correct function name
|
---|
| 262 | void ACTION::startTimer() const { Chronos::getInstance().startTiming( std::string( TOKEN ) ); }
|
---|
| 263 | void ACTION::endTimer() const { Chronos::getInstance().endTiming( std::string( TOKEN ) ); }
|
---|
| 264 |
|
---|
[b4fa106] | 265 | // =========== command for calling action directly ===========
|
---|
| 266 | void COMMAND(
|
---|
[f10b0c] | 267 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
[b4fa106] | 268 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
|
---|
| 269 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
| 270 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 271 | #endif
|
---|
[05736a] | 272 | )
|
---|
[0b2ce9] | 273 | {
|
---|
[f10b0c] | 274 | ACTION *ToCall = dynamic_cast<ACTION*>(ActionRegistry::getInstance().getActionByName( TOKEN )); //->clone(params);
|
---|
[ddde10] | 275 | //ACTION::PARAMS params;
|
---|
[f10b0c] | 276 | #if defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
| 277 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, paramreferences, ToCall->params.)
|
---|
[b4fa106] | 278 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
[0b2ce9] | 279 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 280 | #endif
|
---|
[9ee38b] | 281 | ToCall->call(Action::NonInteractive);
|
---|
[0b2ce9] | 282 | };
|
---|
| 283 |
|
---|
[88ba1f] | 284 | void BOOST_PP_CAT( COMMAND, _stringargs)(
|
---|
[f10b0c] | 285 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
[88ba1f] | 286 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences)
|
---|
| 287 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
| 288 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 289 | #endif
|
---|
| 290 | ) {
|
---|
[f10b0c] | 291 | ACTION *ToCall = dynamic_cast<ACTION*>(ActionRegistry::getInstance().getActionByName( TOKEN )); //->clone(params);
|
---|
[88ba1f] | 292 | //ACTION::PARAMS params;
|
---|
[f10b0c] | 293 | #if defined paramtypes && defined paramtypes && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
| 294 | #define BOOST_PP_LOCAL_MACRO(n) valuetype_print(~, n, paramreferences, paramtypes, ToCall->params. )
|
---|
[88ba1f] | 295 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
| 296 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
| 297 | #endif
|
---|
| 298 | ToCall->call(MoleCuilder::Action::NonInteractive);
|
---|
| 299 | };
|
---|
| 300 |
|
---|
[ce7fdc] | 301 | }
|
---|
| 302 |
|
---|
[b4fa106] | 303 | // free up defines
|
---|
[649aaa] | 304 | #undef paramvalids
|
---|
[b4fa106] | 305 | #undef paramtypes
|
---|
| 306 | #undef paramtokens
|
---|
| 307 | #undef paramreferences
|
---|
[649aaa] | 308 | #undef paramdescriptions
|
---|
| 309 | #undef paramdefaults
|
---|
[b4fa106] | 310 | #undef MAXPARAMTYPES
|
---|
| 311 | #undef statetypes
|
---|
| 312 | #undef statereferences
|
---|
| 313 | #undef MAXSTATETYPES
|
---|
[0b2ce9] | 314 |
|
---|
[88ba1f] | 315 | #undef type2string
|
---|
| 316 | #undef stringtype
|
---|
[a02f78] | 317 | #undef initialiser_print
|
---|
| 318 | #undef type_print
|
---|
| 319 | #undef type_list
|
---|
| 320 | #undef dialog_print
|
---|
[649aaa] | 321 | #undef valid_print
|
---|
| 322 | #undef validcopy_print
|
---|
[a02f78] | 323 | #undef value_print
|
---|
[88ba1f] | 324 | #undef valuetype_print
|
---|
[a02f78] | 325 |
|
---|
[9ee38b] | 326 | #undef ACTION
|
---|
| 327 | #undef COMMAND
|
---|
| 328 | #undef PARAMS
|
---|
| 329 | #undef STATE
|
---|
[2a6a2c] | 330 | #undef INSTANCE
|
---|
[b4fa106] | 331 |
|
---|
| 332 | #undef ACTIONNAME
|
---|
| 333 | #undef CATEGORY
|
---|
[9ee38b] | 334 | #undef TOKEN
|
---|