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