| 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/ActionTraits.hpp"
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "Parameters/Parameter.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | // some derived names: if CATEGORY is not given, we don't prefix with it
 | 
|---|
| 38 | #ifdef CATEGORY
 | 
|---|
| 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))
 | 
|---|
| 42 | #else
 | 
|---|
| 43 | #define ACTION BOOST_PP_CAT(ACTIONNAME, Action)
 | 
|---|
| 44 | #define COMMAND ACTIONNAME
 | 
|---|
| 45 | #define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
 | 
|---|
| 46 | #endif
 | 
|---|
| 47 | // check if no lists given
 | 
|---|
| 48 | #ifndef paramtypes
 | 
|---|
| 49 | #define MAXPARAMTYPES 0
 | 
|---|
| 50 | #else
 | 
|---|
| 51 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
 | 
|---|
| 52 | #endif
 | 
|---|
| 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)
 | 
|---|
| 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
 | 
|---|
| 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__ \
 | 
|---|
| 74 | )
 | 
|---|
| 75 | #endif
 | 
|---|
| 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__ \
 | 
|---|
| 79 | )
 | 
|---|
| 80 | #endif
 | 
|---|
| 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
 | 
|---|
| 93 | 
 | 
|---|
| 94 | // check if paramdefaults is given, otherwise fill list with NOPARAM_DEFAULT
 | 
|---|
| 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)
 | 
|---|
| 98 | //#define paramdefaults (NOPARAM_DEFAULT)
 | 
|---|
| 99 | //#define tempvalue(z,n,value)
 | 
|---|
| 100 | //  BOOST_PP_CAT(value,(NOPARAM_DEFAULT))
 | 
|---|
| 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__ \
 | 
|---|
| 109 | )
 | 
|---|
| 110 | #endif
 | 
|---|
| 111 | 
 | 
|---|
| 112 | // print a list of type ref followed by a separator, i.e. "Parameter<int> i;"
 | 
|---|
| 113 | #define type_print(z,n,TYPELIST, VARLIST, separator) \
 | 
|---|
| 114 |   Parameter < \
 | 
|---|
| 115 |   BOOST_PP_SEQ_ELEM(n, TYPELIST) \
 | 
|---|
| 116 |   > \
 | 
|---|
| 117 |   BOOST_PP_SEQ_ELEM(n, VARLIST) \
 | 
|---|
| 118 |   separator
 | 
|---|
| 119 | 
 | 
|---|
| 120 | // print a list of type ref followed, i.e. "int i, double position"
 | 
|---|
| 121 | #define type_list(z,n,TYPELIST, VARLIST) \
 | 
|---|
| 122 |   BOOST_PP_COMMA_IF(n)\
 | 
|---|
| 123 |   BOOST_PP_SEQ_ELEM(n, TYPELIST) \
 | 
|---|
| 124 |   BOOST_PP_SEQ_ELEM(n, VARLIST)
 | 
|---|
| 125 | 
 | 
|---|
| 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) ), \
 | 
|---|
| 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 |           )\
 | 
|---|
| 142 |       )\
 | 
|---|
| 143 |   ); \
 | 
|---|
| 144 |   ASSERT(tester.second, "ActionTrait<ACTION>::ActionTrait<ACTION>() option token present twice!");
 | 
|---|
| 145 | 
 | 
|---|
| 146 | namespace MoleCuilder {
 | 
|---|
| 147 | 
 | 
|---|
| 148 | void COMMAND(
 | 
|---|
| 149 | #if defined paramtypes && defined paramreferences
 | 
|---|
| 150 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
 | 
|---|
| 151 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 152 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 153 | #endif
 | 
|---|
| 154 |     );
 | 
|---|
| 155 | 
 | 
|---|
| 156 | class ACTION;
 | 
|---|
| 157 | 
 | 
|---|
| 158 | template <>
 | 
|---|
| 159 | class ActionTraits<ACTION> : public ActionTrait {
 | 
|---|
| 160 | public:
 | 
|---|
| 161 |   ActionTraits() :
 | 
|---|
| 162 | #ifndef SHORTFORM
 | 
|---|
| 163 |     ActionTrait(OptionTrait(TOKEN, &typeid(void), DESCRIPTION, std::string()))
 | 
|---|
| 164 | #else
 | 
|---|
| 165 |     ActionTrait(OptionTrait(TOKEN, &typeid(void), DESCRIPTION, std::string(), SHORTFORM))
 | 
|---|
| 166 | #endif /* SHORTFORM */
 | 
|---|
| 167 |   {
 | 
|---|
| 168 |   // initialize remainder of action info
 | 
|---|
| 169 | #ifdef MENUNAME
 | 
|---|
| 170 |     MenuTitle = MENUNAME;
 | 
|---|
| 171 | #endif
 | 
|---|
| 172 | #ifdef MENUPOSITION
 | 
|---|
| 173 |     MenuPosition = MENUPOSITION;
 | 
|---|
| 174 | #endif
 | 
|---|
| 175 | 
 | 
|---|
| 176 |     // initialize action's options
 | 
|---|
| 177 |     std::pair< ActionTrait::options_iterator, bool > tester;
 | 
|---|
| 178 | #ifdef paramtokens
 | 
|---|
| 179 | #define BOOST_PP_LOCAL_MACRO(n) option_print(~, n, ~, )
 | 
|---|
| 180 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 181 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 182 | #endif
 | 
|---|
| 183 |     // associate action's short form also with option
 | 
|---|
| 184 | #ifdef SHORTFORM
 | 
|---|
| 185 |     if (Options.find(TOKEN) != Options.end())
 | 
|---|
| 186 |       Options[TOKEN]->setShortForm(std::string(SHORTFORM));
 | 
|---|
| 187 | #endif /* SHORTFORM */
 | 
|---|
| 188 |     //std::cout << "ActionTraits<" << BOOST_PP_STRINGIZE(ACTION) << ">::ActionTraits() on instance " << this << " with " << getName() << ", type " << getTypeName() << " and description " << getDescription() << std::endl;
 | 
|---|
| 189 |   }
 | 
|---|
| 190 | 
 | 
|---|
| 191 |   virtual ~ActionTraits() {
 | 
|---|
| 192 |     //std::cout << "ActionTraits<" << BOOST_PP_STRINGIZE(ACTION) << ">::~ActionTraits() on instance " << this << " with name " << getName() << " called." << std::endl;
 | 
|---|
| 193 |   }
 | 
|---|
| 194 | };
 | 
|---|
| 195 | 
 | 
|---|
| 196 | class ACTION : public Action {
 | 
|---|
| 197 |   #if defined paramtypes && defined paramreferences
 | 
|---|
| 198 |   friend void COMMAND(
 | 
|---|
| 199 |   #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
 | 
|---|
| 200 |   #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 201 |   #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 202 |       );
 | 
|---|
| 203 |   # else
 | 
|---|
| 204 |   void COMMAND();
 | 
|---|
| 205 |   #endif
 | 
|---|
| 206 | 
 | 
|---|
| 207 | public:
 | 
|---|
| 208 |   ACTION();
 | 
|---|
| 209 |   virtual ~ACTION();
 | 
|---|
| 210 | 
 | 
|---|
| 211 |   bool canUndo();
 | 
|---|
| 212 |   bool shouldUndo();
 | 
|---|
| 213 | 
 | 
|---|
| 214 |   void outputAsCLI(std::ostream &ost) const;
 | 
|---|
| 215 |   void outputAsPython(std::ostream &ost, const std::string &prefix) const;
 | 
|---|
| 216 | 
 | 
|---|
| 217 |   struct PARAMS : ActionParameters {
 | 
|---|
| 218 |     //!> constructor for class PARAMS, setting valid ranges
 | 
|---|
| 219 |     PARAMS();
 | 
|---|
| 220 |     //!> copy constructor for class PARAMS, setting valid ranges
 | 
|---|
| 221 |     PARAMS(const PARAMS &p);
 | 
|---|
| 222 |   #if defined paramtypes && defined paramreferences
 | 
|---|
| 223 |   #define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, paramtypes, paramreferences, ;)
 | 
|---|
| 224 |   #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 225 |   #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 226 |   #endif
 | 
|---|
| 227 |   } params;
 | 
|---|
| 228 | 
 | 
|---|
| 229 | protected:
 | 
|---|
| 230 |   virtual Dialog * fillDialog(Dialog*);
 | 
|---|
| 231 | 
 | 
|---|
| 232 |   void startTimer() const;
 | 
|---|
| 233 |   void endTimer() const;
 | 
|---|
| 234 | 
 | 
|---|
| 235 | private:
 | 
|---|
| 236 |   virtual ActionState::ptr performCall();
 | 
|---|
| 237 |   virtual ActionState::ptr performUndo(ActionState::ptr);
 | 
|---|
| 238 |   virtual ActionState::ptr performRedo(ActionState::ptr);
 | 
|---|
| 239 | };
 | 
|---|
| 240 | 
 | 
|---|
| 241 | }
 | 
|---|
| 242 | 
 | 
|---|
| 243 | #undef paramvalids
 | 
|---|
| 244 | #undef paramtypes
 | 
|---|
| 245 | #undef paramtokens
 | 
|---|
| 246 | #undef paramreferences
 | 
|---|
| 247 | #undef paramdescriptions
 | 
|---|
| 248 | #undef paramdefaults
 | 
|---|
| 249 | #undef MAXPARAMTYPES
 | 
|---|
| 250 | #undef MAXPARAMDEFAULTS
 | 
|---|
| 251 | #undef statetypes
 | 
|---|
| 252 | #undef statereferences
 | 
|---|
| 253 | #undef MAXSTATETYPES
 | 
|---|
| 254 | #undef PARAM_DEFAULT
 | 
|---|
| 255 | 
 | 
|---|
| 256 | #undef option_print
 | 
|---|
| 257 | #undef sequencer
 | 
|---|
| 258 | #undef type_print
 | 
|---|
| 259 | #undef type_list
 | 
|---|
| 260 | 
 | 
|---|
| 261 | #undef ACTION
 | 
|---|
| 262 | #undef COMMAND
 | 
|---|
| 263 | #undef COMMANDFULL
 | 
|---|
| 264 | #undef PARAMS
 | 
|---|
| 265 | 
 | 
|---|
| 266 | #undef ACTIONNAME
 | 
|---|
| 267 | #undef CATEGORY
 | 
|---|
| 268 | #undef MENUNAME
 | 
|---|
| 269 | #undef MENUPOSITION
 | 
|---|
| 270 | #undef TOKEN
 | 
|---|
| 271 | 
 | 
|---|
| 272 | #undef DESCRIPTION
 | 
|---|
| 273 | #undef SHORTFORM
 | 
|---|