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