| [1540859] | 1 | /*
 | 
|---|
 | 2 |  * Reaction_impl_header.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Oct 13, 2011
 | 
|---|
 | 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/ValueStorage.hpp"
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | // some derived names: if CATEGORY is not given, we don't prefix with it
 | 
|---|
 | 34 | #ifdef CATEGORY
 | 
|---|
 | 35 | #define REACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
 | 
|---|
 | 36 | #define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
 | 
|---|
 | 37 | #define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
 | 
|---|
 | 38 | #else
 | 
|---|
 | 39 | #define REACTION BOOST_PP_CAT(ACTIONNAME, Action)
 | 
|---|
 | 40 | #define COMMAND ACTIONNAME
 | 
|---|
 | 41 | #define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
 | 
|---|
 | 42 | #endif
 | 
|---|
 | 43 | // check if no lists given
 | 
|---|
 | 44 | #ifndef paramtypes
 | 
|---|
 | 45 | #define MAXPARAMTYPES 0
 | 
|---|
 | 46 | #else
 | 
|---|
 | 47 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
 | 
|---|
 | 48 | #endif
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | // check user has given name and category
 | 
|---|
 | 51 | #ifndef ACTIONNAME
 | 
|---|
 | 52 | ERROR: No "ACTIONNAME" defined in: __FILE__
 | 
|---|
 | 53 | #endif
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 | // calculate numbers and check whether all have same size
 | 
|---|
 | 56 | #ifdef paramtokens
 | 
|---|
 | 57 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
 | 
|---|
 | 58 |   ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
 | 
|---|
 | 59 | )
 | 
|---|
 | 60 | #endif
 | 
|---|
 | 61 | #ifdef paramreferences
 | 
|---|
 | 62 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
 | 
|---|
 | 63 |   ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
 | 
|---|
 | 64 | )
 | 
|---|
 | 65 | #endif
 | 
|---|
 | 66 | #ifdef paramdescriptions
 | 
|---|
 | 67 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramdescriptions)),\
 | 
|---|
 | 68 |   ERROR: There are not the same number of "paramtokens" and "paramdescriptions" in: __FILE__ \
 | 
|---|
 | 69 | )
 | 
|---|
 | 70 | #endif
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | // check for mandatory defines
 | 
|---|
 | 73 | #ifndef DESCRIPTION
 | 
|---|
 | 74 | BOOST_PP_ASSERT_MSG(0, \
 | 
|---|
 | 75 |     "ERROR: Description is mandatory for Actions, here for REACTION " \
 | 
|---|
 | 76 | )
 | 
|---|
 | 77 | #endif
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 | // check if paramdefaults is given, otherwise fill list with NODEFAULT
 | 
|---|
 | 80 | // this does not work: paramdefaults has to be completely defined before
 | 
|---|
 | 81 | // being used within option_print (used as an array there and not as
 | 
|---|
 | 82 | // some function call still to be expanded)
 | 
|---|
 | 83 | //#define paramdefaults (NODEFAULT)
 | 
|---|
 | 84 | //#define tempvalue(z,n,value)
 | 
|---|
 | 85 | //  BOOST_PP_CAT(value,(NODEFAULT))
 | 
|---|
 | 86 | //BOOST_PP_REPEAT(tempvalue, MAXPARAMTYPES, paramdefaults)
 | 
|---|
 | 87 | //#undef tempvalue
 | 
|---|
 | 88 | //#else
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 | // if present, check if correct number of arguments
 | 
|---|
 | 91 | #ifdef paramdefaults
 | 
|---|
 | 92 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramdefaults)),\
 | 
|---|
 | 93 |   ERROR: There are not the same number of "paramtokens" and "paramdefaults" in: __FILE__ \
 | 
|---|
 | 94 | )
 | 
|---|
 | 95 | #endif
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 | // print a list of type ref followed by a separator, i.e. "int i;"
 | 
|---|
 | 98 | #define type_print(z,n,TYPELIST, VARLIST, separator) \
 | 
|---|
 | 99 |   BOOST_PP_SEQ_ELEM(n, TYPELIST) \
 | 
|---|
 | 100 |   BOOST_PP_SEQ_ELEM(n, VARLIST)\
 | 
|---|
 | 101 |   separator
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 | // print a list of type ref followed, i.e. "int i, double position"
 | 
|---|
 | 104 | #define type_list(z,n,TYPELIST, VARLIST) \
 | 
|---|
 | 105 |   BOOST_PP_COMMA_IF(n)\
 | 
|---|
 | 106 |   BOOST_PP_SEQ_ELEM(n, TYPELIST) \
 | 
|---|
 | 107 |   BOOST_PP_SEQ_ELEM(n, VARLIST)
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 | // prints Options.insert
 | 
|---|
 | 110 | #ifdef paramdefaults
 | 
|---|
 | 111 | #define option_print(z,n,unused, unused2) \
 | 
|---|
 | 112 |   tester = Options. insert (\
 | 
|---|
 | 113 |       std::pair< std::string, OptionTrait *> ( \
 | 
|---|
 | 114 |       BOOST_PP_SEQ_ELEM(n, paramtokens), \
 | 
|---|
 | 115 |       new OptionTrait(\
 | 
|---|
 | 116 |           BOOST_PP_SEQ_ELEM(n, paramtokens), \
 | 
|---|
 | 117 |           &typeid( BOOST_PP_SEQ_ELEM(n, paramtypes) ), \
 | 
|---|
 | 118 |           BOOST_PP_SEQ_ELEM(n, paramdescriptions), \
 | 
|---|
 | 119 |           std::string( BOOST_PP_SEQ_ELEM(n, paramdefaults) ) )\
 | 
|---|
 | 120 |       )\
 | 
|---|
 | 121 |   ); \
 | 
|---|
 | 122 |   ASSERT(tester.second, "ActionTrait<REACTION>::ActionTrait<REACTION>() option token present twice!");
 | 
|---|
 | 123 | #else
 | 
|---|
 | 124 | #define option_print(z,n,unused, unused2) \
 | 
|---|
 | 125 |   tester = Options. insert (\
 | 
|---|
 | 126 |       std::pair< std::string, OptionTrait *> ( \
 | 
|---|
 | 127 |       BOOST_PP_SEQ_ELEM(n, paramtokens), \
 | 
|---|
 | 128 |       new OptionTrait(\
 | 
|---|
 | 129 |           BOOST_PP_SEQ_ELEM(n, paramtokens), \
 | 
|---|
 | 130 |           &typeid( BOOST_PP_SEQ_ELEM(n, paramtypes) ), \
 | 
|---|
 | 131 |           BOOST_PP_SEQ_ELEM(n, paramdescriptions), \
 | 
|---|
 | 132 |           NODEFAULT )\
 | 
|---|
 | 133 |       )\
 | 
|---|
 | 134 |   ); \
 | 
|---|
 | 135 |   ASSERT(tester.second, "ActionTrait<REACTION>::ActionTrait<REACTION>() option token present twice!");
 | 
|---|
 | 136 | #endif
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 | namespace MoleCuilder {
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 | #ifndef returntype
 | 
|---|
 | 141 | BOOST_PP_ASSERT_MSG(0,\
 | 
|---|
 | 142 |   ERROR: No returntype is defined in __FILE__ \
 | 
|---|
 | 143 | )
 | 
|---|
 | 144 | #endif
 | 
|---|
 | 145 | #define RETURNTYPE returntype
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 | // set the return type
 | 
|---|
 | 148 | RETURNTYPE 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 REACTION;
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | template <>
 | 
|---|
 | 159 | class ActionTraits<REACTION> : 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(REACTION) << ">::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(REACTION) << ">::~ActionTraits() on instance " << this << " with name " << getName() << " called." << std::endl;
 | 
|---|
 | 193 |   }
 | 
|---|
 | 194 | };
 | 
|---|
 | 195 | 
 | 
|---|
 | 196 | class REACTION : public Reaction< RETURNTYPE > {
 | 
|---|
 | 197 |   friend RETURNTYPE COMMAND(
 | 
|---|
 | 198 |   #if defined paramtypes && defined paramreferences
 | 
|---|
 | 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 |   #endif
 | 
|---|
 | 203 |   );
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 | public:
 | 
|---|
 | 206 |   REACTION();
 | 
|---|
 | 207 |   virtual ~REACTION();
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 |   virtual RETURNTYPE * doCalc();
 | 
|---|
 | 210 | 
 | 
|---|
 | 211 |   struct PARAMS : ActionParameters {
 | 
|---|
 | 212 |   #if defined paramtypes && defined paramreferences
 | 
|---|
 | 213 |   #define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, paramtypes, paramreferences, ;)
 | 
|---|
 | 214 |   #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMTYPES-1)
 | 
|---|
 | 215 |   #include BOOST_PP_LOCAL_ITERATE()
 | 
|---|
 | 216 |   #endif
 | 
|---|
 | 217 |   } params;
 | 
|---|
 | 218 | 
 | 
|---|
 | 219 | protected:
 | 
|---|
 | 220 |   virtual Dialog * fillDialog(Dialog*);
 | 
|---|
 | 221 | private:
 | 
|---|
 | 222 |   virtual void getParametersfromValueStorage();
 | 
|---|
 | 223 | };
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 | }
 | 
|---|
 | 226 | 
 | 
|---|
 | 227 | #undef paramtypes
 | 
|---|
 | 228 | #undef paramtokens
 | 
|---|
 | 229 | #undef paramreferences
 | 
|---|
 | 230 | #undef paramdescriptions
 | 
|---|
 | 231 | #undef paramdefaults
 | 
|---|
 | 232 | #undef MAXPARAMTYPES
 | 
|---|
 | 233 | 
 | 
|---|
 | 234 | #undef returntype
 | 
|---|
 | 235 | #undef RETURNTYPE
 | 
|---|
 | 236 | 
 | 
|---|
 | 237 | #undef option_print
 | 
|---|
 | 238 | #undef type_print
 | 
|---|
 | 239 | #undef type_list
 | 
|---|
 | 240 | 
 | 
|---|
 | 241 | #undef REACTION
 | 
|---|
 | 242 | #undef COMMAND
 | 
|---|
 | 243 | #undef COMMANDFULL
 | 
|---|
 | 244 | #undef PARAMS
 | 
|---|
 | 245 | 
 | 
|---|
 | 246 | #undef ACTIONNAME
 | 
|---|
 | 247 | #undef CATEGORY
 | 
|---|
 | 248 | #undef MENUNAME
 | 
|---|
 | 249 | #undef MENUPOSITION
 | 
|---|
 | 250 | #undef TOKEN
 | 
|---|
 | 251 | 
 | 
|---|
 | 252 | #undef DESCRIPTION
 | 
|---|
 | 253 | #undef SHORTFORM
 | 
|---|