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