| 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/if.hpp>
 | 
|---|
| 17 | #include <boost/preprocessor/debug/assert.hpp>
 | 
|---|
| 18 | #include <boost/preprocessor/iteration/local.hpp>
 | 
|---|
| 19 | #include <boost/preprocessor/punctuation/comma_if.hpp>
 | 
|---|
| 20 | #include <boost/preprocessor/repetition/repeat.hpp>
 | 
|---|
| 21 | #include <boost/preprocessor/seq/elem.hpp>
 | 
|---|
| 22 | #include <boost/preprocessor/seq/push_back.hpp>
 | 
|---|
| 23 | #include <boost/preprocessor/seq/seq.hpp>
 | 
|---|
| 24 | #include <boost/preprocessor/seq/size.hpp>
 | 
|---|
| 25 | #include <boost/preprocessor/seq/transform.hpp>
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include <iostream>
 | 
|---|
| 28 | #include <typeinfo>
 | 
|---|
| 29 | 
 | 
|---|
| 30 | #include "Actions/ActionTraits.hpp"
 | 
|---|
| 31 | #include "Actions/OptionTrait.hpp"
 | 
|---|
| 32 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 33 | 
 | 
|---|
| 34 | // some derived names: if CATEGORY is not given, we don't prefix with it
 | 
|---|
| 35 | #ifdef CATEGORY
 | 
|---|
| 36 | #define ACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
 | 
|---|
| 37 | #define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
 | 
|---|
| 38 | #define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
 | 
|---|
| 39 | #else
 | 
|---|
| 40 | #define ACTION BOOST_PP_CAT(ACTIONNAME, Action)
 | 
|---|
| 41 | #define COMMAND ACTIONNAME
 | 
|---|
| 42 | #define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
 | 
|---|
| 43 | #endif
 | 
|---|
| 44 | // check if no lists given
 | 
|---|
| 45 | #ifndef paramtypes
 | 
|---|
| 46 | #define MAXPARAMTYPES 0
 | 
|---|
| 47 | #else
 | 
|---|
| 48 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
 | 
|---|
| 49 | #endif
 | 
|---|
| 50 | 
 | 
|---|
| 51 | // check user has given name and category
 | 
|---|
| 52 | #ifndef ACTIONNAME
 | 
|---|
| 53 | ERROR: No "ACTIONNAME" defined in: __FILE__
 | 
|---|
| 54 | #endif
 | 
|---|
| 55 | 
 | 
|---|
| 56 | // calculate numbers and check whether all have same size
 | 
|---|
| 57 | #ifdef paramtokens
 | 
|---|
| 58 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
 | 
|---|
| 59 |   ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
 | 
|---|
| 60 | )
 | 
|---|
| 61 | #endif
 | 
|---|
| 62 | #ifdef paramreferences
 | 
|---|
| 63 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
 | 
|---|
| 64 |   ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
 | 
|---|
| 65 | )
 | 
|---|
| 66 | #endif
 | 
|---|
| 67 | #ifdef paramdescriptions
 | 
|---|
| 68 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramdescriptions)),\
 | 
|---|
| 69 |   ERROR: There are not the same number of "paramtokens" and "paramdescriptions" in: __FILE__ \
 | 
|---|
| 70 | )
 | 
|---|
| 71 | #endif
 | 
|---|
| 72 | 
 | 
|---|
| 73 | // check for mandatory defines
 | 
|---|
| 74 | #ifndef DESCRIPTION
 | 
|---|
| 75 | BOOST_PP_ASSERT_MSG(0, \
 | 
|---|
| 76 |     "ERROR: Description is mandatory for Actions, here for ACTION " \
 | 
|---|
| 77 | )
 | 
|---|
| 78 | #endif
 | 
|---|
| 79 | 
 | 
|---|
| 80 | // check if paramdefaults is given, otherwise fill list with NODEFAULT
 | 
|---|
| 81 | // this does not work: paramdefaults has to be completely defined before
 | 
|---|
| 82 | // being used within option_print (used as an array there and not as
 | 
|---|
| 83 | // some function call still to be expanded)
 | 
|---|
| 84 | //#define paramdefaults (NODEFAULT)
 | 
|---|
| 85 | //#define tempvalue(z,n,value)
 | 
|---|
| 86 | //  BOOST_PP_CAT(value,(NODEFAULT))
 | 
|---|
| 87 | //BOOST_PP_REPEAT(tempvalue, MAXPARAMTYPES, paramdefaults)
 | 
|---|
| 88 | //#undef tempvalue
 | 
|---|
| 89 | //#else
 | 
|---|
| 90 | 
 | 
|---|
| 91 | // if present, check if correct number of arguments
 | 
|---|
| 92 | #ifdef paramdefaults
 | 
|---|
| 93 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramdefaults)),\
 | 
|---|
| 94 |   ERROR: There are not the same number of "paramtokens" and "paramdefaults" in: __FILE__ \
 | 
|---|
| 95 | )
 | 
|---|
| 96 | #endif
 | 
|---|
| 97 | 
 | 
|---|
| 98 | // print a list of type ref followed by a separator, i.e. "int i;"
 | 
|---|
| 99 | #define type_print(z,n,TYPELIST, VARLIST, separator) \
 | 
|---|
| 100 |   BOOST_PP_SEQ_ELEM(n, TYPELIST) \
 | 
|---|
| 101 |   BOOST_PP_SEQ_ELEM(n, VARLIST)\
 | 
|---|
| 102 |   separator
 | 
|---|
| 103 | 
 | 
|---|
| 104 | // print a list of type ref followed, i.e. "int i, double position"
 | 
|---|
| 105 | #define type_list(z,n,TYPELIST, VARLIST) \
 | 
|---|
| 106 |   BOOST_PP_COMMA_IF(n)\
 | 
|---|
| 107 |   BOOST_PP_SEQ_ELEM(n, TYPELIST) \
 | 
|---|
| 108 |   BOOST_PP_SEQ_ELEM(n, VARLIST)
 | 
|---|
| 109 | 
 | 
|---|
| 110 | // prints Options.insert
 | 
|---|
| 111 | #ifdef paramdefaults
 | 
|---|
| 112 | #define option_print(z,n,unused, unused2) \
 | 
|---|
| 113 |   tester = Options. insert (\
 | 
|---|
| 114 |       std::pair< std::string, OptionTrait *> ( \
 | 
|---|
| 115 |       BOOST_PP_SEQ_ELEM(n, paramtokens), \
 | 
|---|
| 116 |       new OptionTrait(\
 | 
|---|
| 117 |           BOOST_PP_SEQ_ELEM(n, paramtokens), \
 | 
|---|
| 118 |           &typeid( BOOST_PP_SEQ_ELEM(n, paramtypes) ), \
 | 
|---|
| 119 |           BOOST_PP_SEQ_ELEM(n, paramdescriptions), \
 | 
|---|
| 120 |           std::string( BOOST_PP_SEQ_ELEM(n, paramdefaults) ) )\
 | 
|---|
| 121 |       )\
 | 
|---|
| 122 |   ); \
 | 
|---|
| 123 |   ASSERT(tester.second, "ActionTrait<ACTION>::ActionTrait<ACTION>() option token present twice!");
 | 
|---|
| 124 | #else
 | 
|---|
| 125 | #define option_print(z,n,unused, unused2) \
 | 
|---|
| 126 |   tester = Options. insert (\
 | 
|---|
| 127 |       std::pair< std::string, OptionTrait *> ( \
 | 
|---|
| 128 |       BOOST_PP_SEQ_ELEM(n, paramtokens), \
 | 
|---|
| 129 |       new OptionTrait(\
 | 
|---|
| 130 |           BOOST_PP_SEQ_ELEM(n, paramtokens), \
 | 
|---|
| 131 |           &typeid( BOOST_PP_SEQ_ELEM(n, paramtypes) ), \
 | 
|---|
| 132 |           BOOST_PP_SEQ_ELEM(n, paramdescriptions), \
 | 
|---|
| 133 |           NODEFAULT )\
 | 
|---|
| 134 |       )\
 | 
|---|
| 135 |   ); \
 | 
|---|
| 136 |   ASSERT(tester.second, "ActionTrait<ACTION>::ActionTrait<ACTION>() option token present twice!");
 | 
|---|
| 137 | #endif
 | 
|---|
| 138 | 
 | 
|---|
| 139 | #if defined paramtypes && defined paramreferences
 | 
|---|
| 140 | void COMMAND(
 | 
|---|
| 141 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
 | 
|---|
| 142 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 143 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 144 |     );
 | 
|---|
| 145 | # else
 | 
|---|
| 146 | void COMMAND();
 | 
|---|
| 147 | #endif
 | 
|---|
| 148 | 
 | 
|---|
| 149 | class ACTION;
 | 
|---|
| 150 | 
 | 
|---|
| 151 | template <>
 | 
|---|
| 152 | class ActionTrait<ACTION> : public ActionTraits {
 | 
|---|
| 153 | public:
 | 
|---|
| 154 |   ActionTrait() :
 | 
|---|
| 155 | #ifndef SHORTFORM
 | 
|---|
| 156 |     ActionTraits(OptionTrait(TOKEN, &typeid(void), DESCRIPTION, std::string()))
 | 
|---|
| 157 | #else
 | 
|---|
| 158 |     ActionTraits(OptionTrait(TOKEN, &typeid(void), DESCRIPTION, std::string(), SHORTFORM))
 | 
|---|
| 159 | #endif /* SHORTFORM */
 | 
|---|
| 160 |   {
 | 
|---|
| 161 |   // initialize remainder of action info
 | 
|---|
| 162 | #ifdef MENUNAME
 | 
|---|
| 163 |     MenuTitle = MENUNAME;
 | 
|---|
| 164 | #endif
 | 
|---|
| 165 | #ifdef MENUPOSITION
 | 
|---|
| 166 |     MenuPosition = MENUPOSITION;
 | 
|---|
| 167 | #endif
 | 
|---|
| 168 | 
 | 
|---|
| 169 |   // initialize action's options
 | 
|---|
| 170 |   std::pair< ActionTraits::options_iterator, bool > tester;
 | 
|---|
| 171 | #ifdef paramtokens
 | 
|---|
| 172 | #define BOOST_PP_LOCAL_MACRO(n) option_print(~, n, ~, )
 | 
|---|
| 173 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 174 | #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 175 | #endif
 | 
|---|
| 176 |   // associate action's short form also with option
 | 
|---|
| 177 | #ifdef SHORTFORM
 | 
|---|
| 178 |   if (Options.find(TOKEN) != Options.end())
 | 
|---|
| 179 |     Options[TOKEN]->setShortForm(std::string(SHORTFORM));
 | 
|---|
| 180 | #endif /* SHORTFORM */
 | 
|---|
| 181 |   //std::cout << "ActionTrait<ACTION>::ActionTrait() with " << getName() << ", type " << getTypeName() << " and description " << getDescription() << std::endl;
 | 
|---|
| 182 |   }
 | 
|---|
| 183 | 
 | 
|---|
| 184 |   ~ActionTrait() {}
 | 
|---|
| 185 | };
 | 
|---|
| 186 | 
 | 
|---|
| 187 | class ACTION : public Action {
 | 
|---|
| 188 |   #if defined paramtypes && defined paramreferences
 | 
|---|
| 189 |   friend void COMMAND(
 | 
|---|
| 190 |   #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
 | 
|---|
| 191 |   #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 192 |   #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 193 |       );
 | 
|---|
| 194 |   # else
 | 
|---|
| 195 |   void COMMAND();
 | 
|---|
| 196 |   #endif
 | 
|---|
| 197 | 
 | 
|---|
| 198 | public:
 | 
|---|
| 199 |   ACTION();
 | 
|---|
| 200 |   virtual ~ACTION();
 | 
|---|
| 201 | 
 | 
|---|
| 202 |   bool canUndo();
 | 
|---|
| 203 |   bool shouldUndo();
 | 
|---|
| 204 | 
 | 
|---|
| 205 |   struct PARAMS : ActionParameters {
 | 
|---|
| 206 |   #if defined paramtypes && defined paramreferences
 | 
|---|
| 207 |   #define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, paramtypes, paramreferences, ;)
 | 
|---|
| 208 |   #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
| 209 |   #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
| 210 |   #endif
 | 
|---|
| 211 |   } params;
 | 
|---|
| 212 | 
 | 
|---|
| 213 | protected:
 | 
|---|
| 214 |   virtual Dialog * fillDialog(Dialog*);
 | 
|---|
| 215 | private:
 | 
|---|
| 216 |   virtual void getParametersfromValueStorage();
 | 
|---|
| 217 |   virtual Action::state_ptr performCall();
 | 
|---|
| 218 |   virtual Action::state_ptr performUndo(Action::state_ptr);
 | 
|---|
| 219 |   virtual Action::state_ptr performRedo(Action::state_ptr);
 | 
|---|
| 220 | };
 | 
|---|
| 221 | 
 | 
|---|
| 222 | #undef paramtypes
 | 
|---|
| 223 | #undef paramtokens
 | 
|---|
| 224 | #undef paramreferences
 | 
|---|
| 225 | #undef paramdescriptions
 | 
|---|
| 226 | #undef paramdefaults
 | 
|---|
| 227 | #undef MAXPARAMTYPES
 | 
|---|
| 228 | #undef statetypes
 | 
|---|
| 229 | #undef statereferences
 | 
|---|
| 230 | #undef MAXSTATETYPES
 | 
|---|
| 231 | 
 | 
|---|
| 232 | #undef ACTION
 | 
|---|
| 233 | #undef COMMAND
 | 
|---|
| 234 | #undef COMMANDFULL
 | 
|---|
| 235 | #undef PARAMS
 | 
|---|
| 236 | 
 | 
|---|
| 237 | #undef ACTIONNAME
 | 
|---|
| 238 | #undef CATEGORY
 | 
|---|
| 239 | #undef MENUNAME
 | 
|---|
| 240 | #undef MENUPOSITION
 | 
|---|
| 241 | #undef TOKEN
 | 
|---|
| 242 | 
 | 
|---|
| 243 | #undef DESCRIPTION
 | 
|---|
| 244 | #undef SHORTFORM
 | 
|---|