| [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 | 
 | 
|---|
| [9ee38b] | 28 | 
 | 
|---|
 | 29 | #include <boost/preprocessor/cat.hpp>
 | 
|---|
| [b4fa106] | 30 | #include <boost/preprocessor/expand.hpp>
 | 
|---|
| [9ee38b] | 31 | #include <boost/preprocessor/comparison/equal.hpp>
 | 
|---|
 | 32 | #include <boost/preprocessor/comparison/not_equal.hpp>
 | 
|---|
 | 33 | #include <boost/preprocessor/control/if.hpp>
 | 
|---|
 | 34 | #include <boost/preprocessor/debug/assert.hpp>
 | 
|---|
 | 35 | #include <boost/preprocessor/iteration/local.hpp>
 | 
|---|
 | 36 | #include <boost/preprocessor/punctuation/comma_if.hpp>
 | 
|---|
 | 37 | #include <boost/preprocessor/repetition/repeat.hpp>
 | 
|---|
 | 38 | #include <boost/preprocessor/seq/elem.hpp>
 | 
|---|
 | 39 | #include <boost/preprocessor/seq/push_back.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
 | 
|---|
 | 45 | #define ACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
 | 
|---|
 | 46 | #define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
 | 
|---|
 | 47 | #define STATE BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, State))
 | 
|---|
 | 48 | #define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
 | 
|---|
| [2a6a2c] | 49 | #define INSTANCE BOOST_PP_CAT(this_, BOOST_PP_CAT(ACTIONNAME, _instance))
 | 
|---|
| [9ee38b] | 50 | 
 | 
|---|
 | 51 | // check if no lists given
 | 
|---|
| [b4fa106] | 52 | #ifndef paramtypes
 | 
|---|
 | 53 | #define MAXPARAMTYPES 0
 | 
|---|
| [9ee38b] | 54 | #else
 | 
|---|
| [b4fa106] | 55 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
 | 
|---|
 | 56 | #endif
 | 
|---|
 | 57 | #ifndef statetypes
 | 
|---|
 | 58 | #define MAXSTATETYPES 0
 | 
|---|
 | 59 | #else
 | 
|---|
 | 60 | #define MAXSTATETYPES BOOST_PP_SEQ_SIZE(statetypes)
 | 
|---|
| [9ee38b] | 61 | #endif
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | // check user has given name and category
 | 
|---|
 | 64 | #ifndef CATEGORY
 | 
|---|
 | 65 | ERROR: No "CATEGORY" defined in: __FILE__
 | 
|---|
 | 66 | #endif
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | #ifndef ACTIONNAME
 | 
|---|
 | 69 | ERROR: No "ACTIONNAME" defined in: __FILE__
 | 
|---|
 | 70 | #endif
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | // calculate numbers and check whether all have same size
 | 
|---|
| [b4fa106] | 73 | #ifdef paramtokens
 | 
|---|
 | 74 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
 | 
|---|
 | 75 |   ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
 | 
|---|
| [9ee38b] | 76 | )
 | 
|---|
 | 77 | #endif
 | 
|---|
| [b4fa106] | 78 | #ifdef paramreferences
 | 
|---|
 | 79 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
 | 
|---|
 | 80 |   ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
 | 
|---|
 | 81 | )
 | 
|---|
 | 82 | #endif
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 | #ifdef statetypes
 | 
|---|
 | 85 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXSTATETYPES, BOOST_PP_SEQ_SIZE(statereferences)),\
 | 
|---|
 | 86 |   ERROR: There are not the same number of "statetypes" and "statereferences" in: __FILE__ \
 | 
|---|
| [9ee38b] | 87 | )
 | 
|---|
 | 88 | #endif
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 | // print a list of type ref followed by a separator, i.e. "int i;"
 | 
|---|
| [b4fa106] | 91 | #define initialiser_print(z,n,initialiserlist) \
 | 
|---|
 | 92 |   BOOST_PP_SEQ_ELEM(n, initialiserlist) \
 | 
|---|
 | 93 |   (BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(n, initialiserlist))),
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 | // print a list of ref(_ref) followed by a separator, i.e. "id(_id),"
 | 
|---|
 | 96 | #define type_print(z,n,TYPELIST, VARLIST, separator) \
 | 
|---|
 | 97 |   BOOST_PP_SEQ_ELEM(n, TYPELIST) \
 | 
|---|
 | 98 |   BOOST_PP_SEQ_ELEM(n, VARLIST)\
 | 
|---|
| [9ee38b] | 99 |   separator
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | // print a list of type ref followed, i.e. "int i, double position"
 | 
|---|
| [b4fa106] | 102 | #define type_list(z,n,TYPELIST,VARLIST) \
 | 
|---|
| [9ee38b] | 103 |   BOOST_PP_COMMA_IF(n)\
 | 
|---|
| [b4fa106] | 104 |   BOOST_PP_SEQ_ELEM(n, TYPELIST) \
 | 
|---|
 | 105 |   BOOST_PP_SEQ_ELEM(n, VARLIST)
 | 
|---|
| [9ee38b] | 106 | 
 | 
|---|
| [b4fa106] | 107 | // prints dialog->query calls for paramtypes with tokens
 | 
|---|
| [9ee38b] | 108 | #define dialog_print(z,n,unused) \
 | 
|---|
 | 109 |   dialog->query<\
 | 
|---|
| [b4fa106] | 110 |   BOOST_PP_SEQ_ELEM(n, paramtypes)\
 | 
|---|
| [9ee38b] | 111 |   >(\
 | 
|---|
| [b4fa106] | 112 |   BOOST_PP_SEQ_ELEM(n, paramtokens)\
 | 
|---|
| [9ee38b] | 113 |   , ValueStorage::getInstance().getDescription(\
 | 
|---|
| [b4fa106] | 114 |   BOOST_PP_SEQ_ELEM(n, paramtokens)\
 | 
|---|
| [9ee38b] | 115 |   ));
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
 | 
|---|
 | 118 | #define value_print(z,n,command, prefix) \
 | 
|---|
 | 119 |   ValueStorage::getInstance(). command (\
 | 
|---|
| [b4fa106] | 120 |   BOOST_PP_SEQ_ELEM(n, paramtokens)\
 | 
|---|
| [9ee38b] | 121 |   , \
 | 
|---|
 | 122 |   prefix\
 | 
|---|
| [b4fa106] | 123 |   BOOST_PP_SEQ_ELEM(n, paramreferences)\
 | 
|---|
| [9ee38b] | 124 |   );
 | 
|---|
 | 125 | 
 | 
|---|
| [0b2ce9] | 126 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
 | 127 | #include "UIElements/Dialog.hpp"
 | 
|---|
 | 128 | #include "Actions/ValueStorage.hpp"
 | 
|---|
 | 129 | 
 | 
|---|
| [9ee38b] | 130 | 
 | 
|---|
| [b4fa106] | 131 | // =========== memento to remember the state when undoing ===========
 | 
|---|
 | 132 | class STATE : public ActionState {
 | 
|---|
 | 133 | public:
 | 
|---|
 | 134 |   STATE(
 | 
|---|
 | 135 | #if defined statetypes && defined statereferences // if we have parameters, we have to add "_" before each reference and add the params as the last one
 | 
|---|
 | 136 | #define OP(s,data,elem) BOOST_PP_CAT(data, elem)  // OP to add "_"
 | 
|---|
 | 137 | #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)))
 | 
|---|
 | 138 | #else /// if not, params is only list
 | 
|---|
 | 139 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, (const ACTION::PARAMS &), (_params))
 | 
|---|
 | 140 | #endif
 | 
|---|
 | 141 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXSTATETYPES)
 | 
|---|
 | 142 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
 | 143 | ) :
 | 
|---|
 | 144 | #if defined statetypes && defined statereferences // do we have parameters at all?
 | 
|---|
 | 145 | BOOST_PP_REPEAT(MAXSTATETYPES, initialiser_print, statereferences)
 | 
|---|
 | 146 | #endif
 | 
|---|
 | 147 | params(_params)
 | 
|---|
 | 148 |   {}
 | 
|---|
| [0b2ce9] | 149 | 
 | 
|---|
| [b4fa106] | 150 | #if defined statetypes && defined statereferences // do we have parameters at all?
 | 
|---|
 | 151 | #define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, statetypes, statereferences, ;)
 | 
|---|
 | 152 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXSTATETYPES-1)
 | 
|---|
 | 153 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
 | 154 | #endif
 | 
|---|
 | 155 |   ACTION::PARAMS params;
 | 
|---|
 | 156 | };
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | // =========== name of action ===========
 | 
|---|
 | 159 | const char ACTION::NAME[] = TOKEN;
 | 
|---|
| [0b2ce9] | 160 | 
 | 
|---|
| [2a6a2c] | 161 | // (const) prototype to be placed into the ActionRegistry (must be deleted by registry itself)
 | 
|---|
 | 162 | const ACTION INSTANCE;
 | 
|---|
 | 163 | 
 | 
|---|
| [b4fa106] | 164 | // =========== constructor ===========
 | 
|---|
| [0b2ce9] | 165 | ACTION::ACTION () :
 | 
|---|
 | 166 |   Action(NAME)
 | 
|---|
 | 167 | {}
 | 
|---|
 | 168 | 
 | 
|---|
| [b4fa106] | 169 | // =========== destructor ===========
 | 
|---|
| [0b2ce9] | 170 | ACTION::~ACTION ()
 | 
|---|
 | 171 | {}
 | 
|---|
 | 172 | 
 | 
|---|
| [b4fa106] | 173 | // =========== fill a dialog ===========
 | 
|---|
| [0b2ce9] | 174 | Dialog* ACTION::fillDialog(Dialog *dialog) {
 | 
|---|
 | 175 |         ASSERT(dialog,"No Dialog given when filling actionname's dialog");
 | 
|---|
| [b4fa106] | 176 | #if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
 | 
|---|
| [bc2990] | 177 |         dialog->queryEmpty(NAME, Traits.getDescription());
 | 
|---|
| [0b2ce9] | 178 | #else
 | 
|---|
 | 179 | #define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
 | 
|---|
| [b4fa106] | 180 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| [0b2ce9] | 181 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
 | 182 | #endif
 | 
|---|
 | 183 |         return dialog;
 | 
|---|
 | 184 | };
 | 
|---|
 | 185 | 
 | 
|---|
| [b4fa106] | 186 | // =========== command for calling action directly ===========
 | 
|---|
 | 187 | #if defined paramtypes && defined paramreferences
 | 
|---|
 | 188 | void COMMAND(
 | 
|---|
 | 189 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
 | 
|---|
 | 190 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
 | 191 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
 | 192 | )
 | 
|---|
 | 193 | #else
 | 
|---|
 | 194 | void COMMAND()
 | 
|---|
 | 195 | #endif
 | 
|---|
| [0b2ce9] | 196 | {
 | 
|---|
| [9ee38b] | 197 |   ACTION::PARAMS params;
 | 
|---|
 | 198 |   Action *ToCall = ActionRegistry::getInstance().getActionByName( ACTION::NAME ); //->clone(params);
 | 
|---|
| [b4fa106] | 199 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
 | 
|---|
| [9ee38b] | 200 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, setCurrentValue, )
 | 
|---|
| [b4fa106] | 201 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| [0b2ce9] | 202 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
 | 203 | #endif
 | 
|---|
| [9ee38b] | 204 |   ToCall->call(Action::NonInteractive);
 | 
|---|
| [0b2ce9] | 205 | };
 | 
|---|
 | 206 | 
 | 
|---|
| [b4fa106] | 207 | // =========== obtain parameters from Storage, used by performCall() ===========
 | 
|---|
| [9ee38b] | 208 | void ACTION::getParametersfromValueStorage() {
 | 
|---|
| [b4fa106] | 209 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
 | 
|---|
| [9ee38b] | 210 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, queryCurrentValue, params.)
 | 
|---|
| [b4fa106] | 211 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| [0b2ce9] | 212 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
 | 213 | #endif
 | 
|---|
 | 214 | };
 | 
|---|
 | 215 | 
 | 
|---|
| [b4fa106] | 216 | // free up defines
 | 
|---|
 | 217 | #undef paramtypes
 | 
|---|
 | 218 | #undef paramtokens
 | 
|---|
 | 219 | #undef paramreferences
 | 
|---|
 | 220 | #undef MAXPARAMTYPES
 | 
|---|
 | 221 | #undef statetypes
 | 
|---|
 | 222 | #undef statereferences
 | 
|---|
 | 223 | #undef MAXSTATETYPES
 | 
|---|
| [0b2ce9] | 224 | 
 | 
|---|
| [9ee38b] | 225 | #undef ACTION
 | 
|---|
 | 226 | #undef COMMAND
 | 
|---|
 | 227 | #undef PARAMS
 | 
|---|
 | 228 | #undef STATE
 | 
|---|
| [2a6a2c] | 229 | #undef INSTANCE
 | 
|---|
| [b4fa106] | 230 | 
 | 
|---|
 | 231 | #undef ACTIONNAME
 | 
|---|
 | 232 | #undef CATEGORY
 | 
|---|
| [9ee38b] | 233 | #undef TOKEN
 | 
|---|