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