| 1 | /* | 
|---|
| 2 | * Action_impl_header.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Aug 25, 2010 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | // include config.h | 
|---|
| 9 | #ifdef HAVE_CONFIG_H | 
|---|
| 10 | #include <config.h> | 
|---|
| 11 | #endif | 
|---|
| 12 |  | 
|---|
| 13 | #include <boost/preprocessor/cat.hpp> | 
|---|
| 14 | #include <boost/preprocessor/comparison/equal.hpp> | 
|---|
| 15 | #include <boost/preprocessor/comparison/not_equal.hpp> | 
|---|
| 16 | #include <boost/preprocessor/control/expr_if.hpp> | 
|---|
| 17 | #include <boost/preprocessor/control/if.hpp> | 
|---|
| 18 | #include <boost/preprocessor/debug/assert.hpp> | 
|---|
| 19 | #include <boost/preprocessor/iteration/local.hpp> | 
|---|
| 20 | #include <boost/preprocessor/list/adt.hpp> | 
|---|
| 21 | #include <boost/preprocessor/punctuation/comma_if.hpp> | 
|---|
| 22 | #include <boost/preprocessor/punctuation/paren.hpp> | 
|---|
| 23 | #include <boost/preprocessor/repetition/repeat.hpp> | 
|---|
| 24 | #include <boost/preprocessor/seq/elem.hpp> | 
|---|
| 25 | #include <boost/preprocessor/seq/push_back.hpp> | 
|---|
| 26 | #include <boost/preprocessor/seq/seq.hpp> | 
|---|
| 27 | #include <boost/preprocessor/seq/size.hpp> | 
|---|
| 28 | #include <boost/preprocessor/seq/transform.hpp> | 
|---|
| 29 |  | 
|---|
| 30 | #include <iostream> | 
|---|
| 31 | #include <typeinfo> | 
|---|
| 32 |  | 
|---|
| 33 | #include "Actions/ActionSequence.hpp" | 
|---|
| 34 | #include "Actions/ActionTraits.hpp" | 
|---|
| 35 |  | 
|---|
| 36 | #include "Parameters/Parameter.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | // some derived names: if CATEGORY is not given, we don't prefix with it | 
|---|
| 39 | #ifdef CATEGORY | 
|---|
| 40 | #define ACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action)) | 
|---|
| 41 | #define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME) | 
|---|
| 42 | #define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters)) | 
|---|
| 43 | #else | 
|---|
| 44 | #define ACTION BOOST_PP_CAT(ACTIONNAME, Action) | 
|---|
| 45 | #define COMMAND ACTIONNAME | 
|---|
| 46 | #define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters) | 
|---|
| 47 | #endif | 
|---|
| 48 | // check if no lists given | 
|---|
| 49 | #ifndef paramtypes | 
|---|
| 50 | #define MAXPARAMTYPES 0 | 
|---|
| 51 | #else | 
|---|
| 52 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes) | 
|---|
| 53 | #endif | 
|---|
| 54 | #ifndef paramdefaults | 
|---|
| 55 | #define MAXPARAMDEFAULTS 0 | 
|---|
| 56 | // this is required for valid_print "else part" | 
|---|
| 57 | #define sequencer(z,n,data) \ | 
|---|
| 58 | BOOST_PP_SEQ_PUSH_BACK( data, NOPARAM_DEFAULT) | 
|---|
| 59 | #define paramdefaults BOOST_PP_REPEAT( MAXPARAMTYPES, sequencer, BOOST_PP_SEQ_NIL ) | 
|---|
| 60 | #else | 
|---|
| 61 | #define MAXPARAMDEFAULTS BOOST_PP_SEQ_SIZE(paramdefaults) | 
|---|
| 62 | #endif | 
|---|
| 63 | #define PARAM_DEFAULT(x) \ | 
|---|
| 64 | (x, BOOST_PP_NIL) | 
|---|
| 65 |  | 
|---|
| 66 | // check user has given name and category | 
|---|
| 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 | #ifdef paramdescriptions | 
|---|
| 83 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramdescriptions)),\ | 
|---|
| 84 | ERROR: There are not the same number of "paramtokens" and "paramdescriptions" in: __FILE__ \ | 
|---|
| 85 | ) | 
|---|
| 86 | #endif | 
|---|
| 87 |  | 
|---|
| 88 | // check for mandatory defines | 
|---|
| 89 | #ifndef DESCRIPTION | 
|---|
| 90 | BOOST_PP_ASSERT_MSG(0, \ | 
|---|
| 91 | "ERROR: Description is mandatory for Actions, here for ACTION " \ | 
|---|
| 92 | ) | 
|---|
| 93 | #endif | 
|---|
| 94 |  | 
|---|
| 95 | // check if paramdefaults is given, otherwise fill list with NOPARAM_DEFAULT | 
|---|
| 96 | // this does not work: paramdefaults has to be completely defined before | 
|---|
| 97 | // being used within option_print (used as an array there and not as | 
|---|
| 98 | // some function call still to be expanded) | 
|---|
| 99 | //#define paramdefaults (NOPARAM_DEFAULT) | 
|---|
| 100 | //#define tempvalue(z,n,value) | 
|---|
| 101 | //  BOOST_PP_CAT(value,(NOPARAM_DEFAULT)) | 
|---|
| 102 | //BOOST_PP_REPEAT(tempvalue, MAXPARAMTYPES, paramdefaults) | 
|---|
| 103 | //#undef tempvalue | 
|---|
| 104 | //#else | 
|---|
| 105 |  | 
|---|
| 106 | // if present, check if correct number of arguments | 
|---|
| 107 | #ifdef paramdefaults | 
|---|
| 108 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramdefaults)),\ | 
|---|
| 109 | ERROR: There are not the same number of "paramtokens" and "paramdefaults" in: __FILE__ \ | 
|---|
| 110 | ) | 
|---|
| 111 | #endif | 
|---|
| 112 |  | 
|---|
| 113 | // print a list of type ref followed by a separator, i.e. "Parameter<int> i;" | 
|---|
| 114 | #define type_print(z,n,TYPELIST, VARLIST, separator) \ | 
|---|
| 115 | Parameter < \ | 
|---|
| 116 | BOOST_PP_SEQ_ELEM(n, TYPELIST) \ | 
|---|
| 117 | > \ | 
|---|
| 118 | BOOST_PP_SEQ_ELEM(n, VARLIST) \ | 
|---|
| 119 | separator | 
|---|
| 120 |  | 
|---|
| 121 | // print a list of type ref followed, i.e. "int i, double position" | 
|---|
| 122 | #define type_list(z,n,TYPELIST, VARLIST) \ | 
|---|
| 123 | BOOST_PP_COMMA_IF(n)\ | 
|---|
| 124 | BOOST_PP_SEQ_ELEM(n, TYPELIST) \ | 
|---|
| 125 | BOOST_PP_SEQ_ELEM(n, VARLIST) | 
|---|
| 126 |  | 
|---|
| 127 | // prints Options.insert | 
|---|
| 128 | #define option_print(z,n,unused, unused2) \ | 
|---|
| 129 | tester = Options. insert (\ | 
|---|
| 130 | std::pair< std::string, OptionTrait *> ( \ | 
|---|
| 131 | BOOST_PP_SEQ_ELEM(n, paramtokens), \ | 
|---|
| 132 | new OptionTrait(\ | 
|---|
| 133 | BOOST_PP_SEQ_ELEM(n, paramtokens), \ | 
|---|
| 134 | &typeid( BOOST_PP_SEQ_ELEM(n, paramtypes) ), \ | 
|---|
| 135 | BOOST_PP_SEQ_ELEM(n, paramdescriptions) \ | 
|---|
| 136 | BOOST_PP_COMMA_IF( BOOST_PP_NOT( BOOST_PP_LIST_IS_NIL( BOOST_PP_SEQ_ELEM(n, paramdefaults) ) ) ) \ | 
|---|
| 137 | BOOST_PP_EXPR_IF( \ | 
|---|
| 138 | BOOST_PP_NOT( BOOST_PP_LIST_IS_NIL( BOOST_PP_SEQ_ELEM(n, paramdefaults) ) ), \ | 
|---|
| 139 | toString BOOST_PP_LPAREN() \ | 
|---|
| 140 | BOOST_PP_LIST_FIRST( BOOST_PP_SEQ_ELEM(n, paramdefaults) )) \ | 
|---|
| 141 | BOOST_PP_RPAREN() \ | 
|---|
| 142 | )\ | 
|---|
| 143 | )\ | 
|---|
| 144 | ); \ | 
|---|
| 145 | ASSERT(tester.second, "ActionTrait<ACTION>::ActionTrait<ACTION>() option token present twice!"); | 
|---|
| 146 |  | 
|---|
| 147 | #define stringtype std::string | 
|---|
| 148 |  | 
|---|
| 149 | #define type2string(s, data, elem) \ | 
|---|
| 150 | stringtype | 
|---|
| 151 |  | 
|---|
| 152 | namespace MoleCuilder { | 
|---|
| 153 |  | 
|---|
| 154 | void COMMAND( | 
|---|
| 155 | #if defined paramtypes && defined paramreferences | 
|---|
| 156 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences) | 
|---|
| 157 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1) | 
|---|
| 158 | #include BOOST_PP_LOCAL_ITERATE() | 
|---|
| 159 | #endif | 
|---|
| 160 | ); | 
|---|
| 161 |  | 
|---|
| 162 | void BOOST_PP_CAT( COMMAND, _stringargs)( | 
|---|
| 163 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0) | 
|---|
| 164 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences) | 
|---|
| 165 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1) | 
|---|
| 166 | #include BOOST_PP_LOCAL_ITERATE() | 
|---|
| 167 | #endif | 
|---|
| 168 | ); | 
|---|
| 169 |  | 
|---|
| 170 | class ACTION; | 
|---|
| 171 |  | 
|---|
| 172 | template <> | 
|---|
| 173 | class ActionTraits<ACTION> : public ActionTrait { | 
|---|
| 174 | public: | 
|---|
| 175 | ActionTraits() : | 
|---|
| 176 | #ifndef SHORTFORM | 
|---|
| 177 | ActionTrait(OptionTrait(TOKEN, &typeid(void), DESCRIPTION, std::string())) | 
|---|
| 178 | #else | 
|---|
| 179 | ActionTrait(OptionTrait(TOKEN, &typeid(void), DESCRIPTION, std::string(), SHORTFORM)) | 
|---|
| 180 | #endif /* SHORTFORM */ | 
|---|
| 181 | { | 
|---|
| 182 | // initialize remainder of action info | 
|---|
| 183 | #ifdef MENUNAME | 
|---|
| 184 | MenuTitle = MENUNAME; | 
|---|
| 185 | #endif | 
|---|
| 186 | #ifdef MENUPOSITION | 
|---|
| 187 | MenuPosition = MENUPOSITION; | 
|---|
| 188 | #endif | 
|---|
| 189 |  | 
|---|
| 190 | // initialize action's options | 
|---|
| 191 | std::pair< ActionTrait::options_iterator, bool > tester; | 
|---|
| 192 | #ifdef paramtokens | 
|---|
| 193 | #define BOOST_PP_LOCAL_MACRO(n) option_print(~, n, ~, ) | 
|---|
| 194 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1) | 
|---|
| 195 | #include BOOST_PP_LOCAL_ITERATE() | 
|---|
| 196 | #endif | 
|---|
| 197 | // associate action's short form also with option | 
|---|
| 198 | #ifdef SHORTFORM | 
|---|
| 199 | if (Options.find(TOKEN) != Options.end()) | 
|---|
| 200 | Options[TOKEN]->setShortForm(std::string(SHORTFORM)); | 
|---|
| 201 | #endif /* SHORTFORM */ | 
|---|
| 202 | //std::cout << "ActionTraits<" << BOOST_PP_STRINGIZE(ACTION) << ">::ActionTraits() on instance " << this << " with " << getName() << ", type " << getTypeName() << " and description " << getDescription() << std::endl; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | virtual ~ActionTraits() { | 
|---|
| 206 | //std::cout << "ActionTraits<" << BOOST_PP_STRINGIZE(ACTION) << ">::~ActionTraits() on instance " << this << " with name " << getName() << " called." << std::endl; | 
|---|
| 207 | } | 
|---|
| 208 | }; | 
|---|
| 209 |  | 
|---|
| 210 | class ACTION : public MakroAction { | 
|---|
| 211 | friend void COMMAND( | 
|---|
| 212 | #if defined paramtypes && defined paramreferences | 
|---|
| 213 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences) | 
|---|
| 214 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1) | 
|---|
| 215 | #include BOOST_PP_LOCAL_ITERATE() | 
|---|
| 216 | #endif | 
|---|
| 217 | ); | 
|---|
| 218 |  | 
|---|
| 219 | friend void BOOST_PP_CAT( COMMAND, _stringargs)( | 
|---|
| 220 | #if defined paramtypes && defined paramreferences && BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0) | 
|---|
| 221 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_TRANSFORM( type2string, , paramtypes), paramreferences) | 
|---|
| 222 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1) | 
|---|
| 223 | #include BOOST_PP_LOCAL_ITERATE() | 
|---|
| 224 | #endif | 
|---|
| 225 | ); | 
|---|
| 226 |  | 
|---|
| 227 | public: | 
|---|
| 228 | ACTION(); | 
|---|
| 229 | virtual ~ACTION(); | 
|---|
| 230 |  | 
|---|
| 231 | // must be called after all primitive actions are present | 
|---|
| 232 | void prepare(ActionRegistry &AR); | 
|---|
| 233 | // must be called before alle primitive actions are removed | 
|---|
| 234 | void unprepare(ActionRegistry &AR); | 
|---|
| 235 |  | 
|---|
| 236 | bool canUndo(); | 
|---|
| 237 | bool shouldUndo(); | 
|---|
| 238 |  | 
|---|
| 239 | virtual Action* clone(enum QueryOptions flag = Interactive) const; | 
|---|
| 240 |  | 
|---|
| 241 | void outputAsCLI(std::ostream &ost) const; | 
|---|
| 242 | void outputAsPython(std::ostream &ost, const std::string &prefix) const; | 
|---|
| 243 |  | 
|---|
| 244 | struct PARAMS : ActionParameters { | 
|---|
| 245 | //!> constructor for class PARAMS, setting valid ranges | 
|---|
| 246 | PARAMS(); | 
|---|
| 247 | //!> copy constructor for class PARAMS, setting valid ranges | 
|---|
| 248 | PARAMS(const PARAMS &p); | 
|---|
| 249 | #if defined paramtypes && defined paramreferences | 
|---|
| 250 | #define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, paramtypes, paramreferences, ;) | 
|---|
| 251 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1) | 
|---|
| 252 | #include BOOST_PP_LOCAL_ITERATE() | 
|---|
| 253 | #endif | 
|---|
| 254 | } params; | 
|---|
| 255 |  | 
|---|
| 256 | protected: | 
|---|
| 257 | virtual Dialog * fillOwnDialog(Dialog*); | 
|---|
| 258 |  | 
|---|
| 259 | void startTimer() const; | 
|---|
| 260 | void endTimer() const; | 
|---|
| 261 |  | 
|---|
| 262 | private: | 
|---|
| 263 | //!> flag to check whether actions have been prepared | 
|---|
| 264 | static bool isPrepared; | 
|---|
| 265 | //!> sequence of actions for this macro action | 
|---|
| 266 | static ActionSequence actions; | 
|---|
| 267 |  | 
|---|
| 268 | private: | 
|---|
| 269 | virtual ActionState::ptr performCall(); | 
|---|
| 270 | virtual ActionState::ptr performUndo(ActionState::ptr); | 
|---|
| 271 | virtual ActionState::ptr performRedo(ActionState::ptr); | 
|---|
| 272 | }; | 
|---|
| 273 |  | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | #undef paramvalids | 
|---|
| 277 | #undef paramtypes | 
|---|
| 278 | #undef paramtokens | 
|---|
| 279 | #undef paramreferences | 
|---|
| 280 | #undef paramdescriptions | 
|---|
| 281 | #undef paramdefaults | 
|---|
| 282 | #undef MAXPARAMTYPES | 
|---|
| 283 | #undef MAXPARAMDEFAULTS | 
|---|
| 284 | #undef statetypes | 
|---|
| 285 | #undef statereferences | 
|---|
| 286 | #undef MAXSTATETYPES | 
|---|
| 287 | #undef PARAM_DEFAULT | 
|---|
| 288 |  | 
|---|
| 289 | #undef option_print | 
|---|
| 290 | #undef sequencer | 
|---|
| 291 | #undef type_print | 
|---|
| 292 | #undef type_list | 
|---|
| 293 |  | 
|---|
| 294 | #undef ACTION | 
|---|
| 295 | #undef COMMAND | 
|---|
| 296 | #undef COMMANDFULL | 
|---|
| 297 | #undef PARAMS | 
|---|
| 298 |  | 
|---|
| 299 | #undef ACTIONNAME | 
|---|
| 300 | #undef CATEGORY | 
|---|
| 301 | #undef MENUNAME | 
|---|
| 302 | #undef MENUPOSITION | 
|---|
| 303 | #undef TOKEN | 
|---|
| 304 |  | 
|---|
| 305 | #undef DESCRIPTION | 
|---|
| 306 | #undef SHORTFORM | 
|---|