1 | /*
|
---|
2 | * Action_impl.hpp
|
---|
3 | *
|
---|
4 | * Created on: Aug 25, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | /** These macros define the following functions, necessary but repetitive for
|
---|
9 | * every Action:
|
---|
10 | * -# Dialog* fillDialog()
|
---|
11 | * -# action command (e.g. AnalysisMolecularVolume() )
|
---|
12 | * -# void getParametersfromValuStorage()
|
---|
13 | * -# struct Action...Parameters
|
---|
14 | *
|
---|
15 | * For this, the user has the define the following values, each with
|
---|
16 | * parenthesis, for the values/parameters the action needs
|
---|
17 | * -# paramtypes, e.g. (int)(double)
|
---|
18 | * -# paramtokens, e.g. ("Z")("length")
|
---|
19 | * -# paramreferences, e.g. (Z)(length)
|
---|
20 | * and for additional values/parameters to save in the state
|
---|
21 | * -# statetypes, e.g. (int)(double)
|
---|
22 | * -# statereferences, e.g. (Z)(length)
|
---|
23 | * and the name and category of the action
|
---|
24 | * -# CATEGORY, e.g. Analysis
|
---|
25 | * -# ACTIONNAME, e.g. MolecularVolume
|
---|
26 | */
|
---|
27 |
|
---|
28 | // include config.h
|
---|
29 | #ifdef HAVE_CONFIG_H
|
---|
30 | #include <config.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include "CodePatterns/Chronos.hpp"
|
---|
34 |
|
---|
35 | #include <boost/preprocessor/cat.hpp>
|
---|
36 | #include <boost/preprocessor/expand.hpp>
|
---|
37 | #include <boost/preprocessor/comparison/equal.hpp>
|
---|
38 | #include <boost/preprocessor/comparison/not_equal.hpp>
|
---|
39 | #include <boost/preprocessor/control/if.hpp>
|
---|
40 | #include <boost/preprocessor/debug/assert.hpp>
|
---|
41 | #include <boost/preprocessor/iteration/local.hpp>
|
---|
42 | #include <boost/preprocessor/punctuation/comma_if.hpp>
|
---|
43 | #include <boost/preprocessor/repetition/repeat.hpp>
|
---|
44 | #include <boost/preprocessor/seq/elem.hpp>
|
---|
45 | #include <boost/preprocessor/seq/push_back.hpp>
|
---|
46 | #include <boost/preprocessor/seq/seq.hpp>
|
---|
47 | #include <boost/preprocessor/seq/size.hpp>
|
---|
48 | #include <boost/preprocessor/seq/transform.hpp>
|
---|
49 |
|
---|
50 | // some derived names: if CATEGORY is not given, we don't prefix with it
|
---|
51 | #ifdef CATEGORY
|
---|
52 | #define ACTION BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Action))
|
---|
53 | #define COMMAND BOOST_PP_CAT(CATEGORY, ACTIONNAME)
|
---|
54 | #define STATE BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, State))
|
---|
55 | #define PARAMS BOOST_PP_CAT(CATEGORY, BOOST_PP_CAT(ACTIONNAME, Parameters))
|
---|
56 | #else
|
---|
57 | #define ACTION BOOST_PP_CAT(ACTIONNAME, Action)
|
---|
58 | #define COMMAND ACTIONNAME
|
---|
59 | #define STATE BOOST_PP_CAT(ACTIONNAME, State)
|
---|
60 | #define PARAMS BOOST_PP_CAT(ACTIONNAME, Parameters)
|
---|
61 | #endif
|
---|
62 | #define INSTANCE BOOST_PP_CAT(this_, BOOST_PP_CAT(ACTIONNAME, _instance))
|
---|
63 |
|
---|
64 | // check if no lists given
|
---|
65 | #ifndef paramtypes
|
---|
66 | #define MAXPARAMTYPES 0
|
---|
67 | #else
|
---|
68 | #define MAXPARAMTYPES BOOST_PP_SEQ_SIZE(paramtypes)
|
---|
69 | #endif
|
---|
70 | #ifndef statetypes
|
---|
71 | #define MAXSTATETYPES 0
|
---|
72 | #else
|
---|
73 | #define MAXSTATETYPES BOOST_PP_SEQ_SIZE(statetypes)
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | // check user has given name and category
|
---|
77 | #ifndef ACTIONNAME
|
---|
78 | ERROR: No "ACTIONNAME" defined in: __FILE__
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | // calculate numbers and check whether all have same size
|
---|
82 | #ifdef paramtokens
|
---|
83 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramtokens)),\
|
---|
84 | ERROR: There are not the same number of "paramtokens" and "paramtypes" in: __FILE__ \
|
---|
85 | )
|
---|
86 | #endif
|
---|
87 | #ifdef paramreferences
|
---|
88 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXPARAMTYPES, BOOST_PP_SEQ_SIZE(paramreferences)),\
|
---|
89 | ERROR: There are not the same number of "paramtokens" and "paramreferences" in: __FILE__ \
|
---|
90 | )
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | #ifdef statetypes
|
---|
94 | BOOST_PP_ASSERT_MSG(BOOST_PP_EQUAL(MAXSTATETYPES, BOOST_PP_SEQ_SIZE(statereferences)),\
|
---|
95 | ERROR: There are not the same number of "statetypes" and "statereferences" in: __FILE__ \
|
---|
96 | )
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | // print a list of type ref followed by a separator, i.e. "int i;"
|
---|
100 | #define initialiser_print(z,n,initialiserlist) \
|
---|
101 | BOOST_PP_SEQ_ELEM(n, initialiserlist) \
|
---|
102 | (BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(n, initialiserlist))),
|
---|
103 |
|
---|
104 | // print a list of ref(_ref) followed by a separator, i.e. "id(_id),"
|
---|
105 | #define type_print(z,n,TYPELIST, VARLIST, separator) \
|
---|
106 | BOOST_PP_SEQ_ELEM(n, TYPELIST) \
|
---|
107 | BOOST_PP_SEQ_ELEM(n, VARLIST)\
|
---|
108 | separator
|
---|
109 |
|
---|
110 | // print a list of type ref followed, i.e. "int i, double position"
|
---|
111 | #define type_list(z,n,TYPELIST,VARLIST) \
|
---|
112 | BOOST_PP_COMMA_IF(n)\
|
---|
113 | BOOST_PP_SEQ_ELEM(n, TYPELIST) \
|
---|
114 | BOOST_PP_SEQ_ELEM(n, VARLIST)
|
---|
115 |
|
---|
116 | // prints dialog->query calls for paramtypes with tokens
|
---|
117 | #define dialog_print(z,n,unused) \
|
---|
118 | dialog->query<\
|
---|
119 | BOOST_PP_SEQ_ELEM(n, paramtypes)\
|
---|
120 | >(\
|
---|
121 | BOOST_PP_SEQ_ELEM(n, paramtokens)\
|
---|
122 | , Traits.getDescription()\
|
---|
123 | );
|
---|
124 |
|
---|
125 | // prints set/queryCurrentValue (command) for paramreferences and paramtokens
|
---|
126 | #define value_print(z,n,command, prefix) \
|
---|
127 | ValueStorage::getInstance(). command (\
|
---|
128 | BOOST_PP_SEQ_ELEM(n, paramtokens)\
|
---|
129 | , \
|
---|
130 | prefix\
|
---|
131 | BOOST_PP_SEQ_ELEM(n, paramreferences)\
|
---|
132 | );
|
---|
133 |
|
---|
134 | #include "Actions/ActionRegistry.hpp"
|
---|
135 | #include "UIElements/Dialog.hpp"
|
---|
136 |
|
---|
137 | #ifdef paramtokens
|
---|
138 | #define statenecessary 1
|
---|
139 | #endif
|
---|
140 | #ifndef statetokens
|
---|
141 | #define statenecessary 1
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | namespace MoleCuilder {
|
---|
145 |
|
---|
146 | // =========== memento to remember the state when undoing ===========
|
---|
147 | #ifdef statenecessary
|
---|
148 | class STATE : public ActionState {
|
---|
149 | public:
|
---|
150 | STATE(
|
---|
151 | #if defined statetypes && defined statereferences // if we have parameters, we have to add "_" before each reference and add the params as the last one
|
---|
152 | #define OP(s,data,elem) BOOST_PP_CAT(data, elem) // OP to add "_"
|
---|
153 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, BOOST_PP_SEQ_PUSH_BACK(statetypes, const ACTION::PARAMS &), BOOST_PP_SEQ_TRANSFORM(OP, _, BOOST_PP_SEQ_PUSH_BACK(statereferences, params)))
|
---|
154 | #else /// if not, params is only list
|
---|
155 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, (const ACTION::PARAMS &), (_params))
|
---|
156 | #endif
|
---|
157 | #define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES)
|
---|
158 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
159 | ) :
|
---|
160 | #if defined statetypes && defined statereferences // do we have parameters at all?
|
---|
161 | BOOST_PP_REPEAT(MAXSTATETYPES, initialiser_print, statereferences)
|
---|
162 | #endif
|
---|
163 | params(_params)
|
---|
164 | {}
|
---|
165 |
|
---|
166 | #if defined statetypes && defined statereferences // do we have parameters at all?
|
---|
167 | #define BOOST_PP_LOCAL_MACRO(n) type_print(~, n, statetypes, statereferences, ;)
|
---|
168 | #define BOOST_PP_LOCAL_LIMITS (0, MAXSTATETYPES-1)
|
---|
169 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
170 | #endif
|
---|
171 | ACTION::PARAMS params;
|
---|
172 | };
|
---|
173 | #endif /* statenecessary */
|
---|
174 |
|
---|
175 | // (const) prototype to be placed into the ActionRegistry (must be deleted by registry itself)
|
---|
176 | const ACTION *INSTANCE = new ACTION();
|
---|
177 |
|
---|
178 | // =========== constructor ===========
|
---|
179 | ACTION::ACTION () :
|
---|
180 | Action(ActionTrait<ACTION>())
|
---|
181 | {}
|
---|
182 |
|
---|
183 | // =========== destructor ===========
|
---|
184 | ACTION::~ACTION ()
|
---|
185 | {
|
---|
186 | //std::cout << "Action ACTION is being destroyed." << std::endl;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // =========== fill a dialog ===========
|
---|
190 | Dialog* ACTION::fillDialog(Dialog *dialog) {
|
---|
191 | ASSERT(dialog,"No Dialog given when filling actionname's dialog");
|
---|
192 | #if BOOST_PP_EQUAL(MAXPARAMTYPES,0)
|
---|
193 | dialog->queryEmpty(TOKEN, Traits.getDescription());
|
---|
194 | #else
|
---|
195 | #define BOOST_PP_LOCAL_MACRO(n) dialog_print(~, n, ~)
|
---|
196 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
197 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
198 | #endif
|
---|
199 | return dialog;
|
---|
200 | };
|
---|
201 |
|
---|
202 | // =========== time the action ===========
|
---|
203 | // we need this here to have the correct function name
|
---|
204 | void ACTION::startTimer() const { Chronos::getInstance().startTiming( std::string( TOKEN ) ); }
|
---|
205 | void ACTION::endTimer() const { Chronos::getInstance().endTiming( std::string( TOKEN ) ); }
|
---|
206 |
|
---|
207 | // =========== command for calling action directly ===========
|
---|
208 | #if defined paramtypes && defined paramreferences
|
---|
209 | void COMMAND(
|
---|
210 | #define BOOST_PP_LOCAL_MACRO(n) type_list(~, n, paramtypes, paramreferences)
|
---|
211 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
212 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
213 | )
|
---|
214 | #else
|
---|
215 | void COMMAND()
|
---|
216 | #endif
|
---|
217 | {
|
---|
218 | ACTION::PARAMS params;
|
---|
219 | Action *ToCall = ActionRegistry::getInstance().getActionByName( TOKEN ); //->clone(params);
|
---|
220 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
221 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, setCurrentValue, )
|
---|
222 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
223 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
224 | #endif
|
---|
225 | ToCall->call(Action::NonInteractive);
|
---|
226 | };
|
---|
227 |
|
---|
228 | // =========== obtain parameters from Storage, used by performCall() ===========
|
---|
229 | void ACTION::getParametersfromValueStorage() {
|
---|
230 | #if BOOST_PP_NOT_EQUAL(MAXPARAMTYPES,0)
|
---|
231 | #define BOOST_PP_LOCAL_MACRO(n) value_print(~, n, queryCurrentValue, params.)
|
---|
232 | #define BOOST_PP_LOCAL_LIMITS (0, MAXPARAMTYPES-1)
|
---|
233 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
234 | #endif
|
---|
235 | };
|
---|
236 |
|
---|
237 | }
|
---|
238 |
|
---|
239 | // free up defines
|
---|
240 | #undef paramtypes
|
---|
241 | #undef paramtokens
|
---|
242 | #undef paramreferences
|
---|
243 | #undef MAXPARAMTYPES
|
---|
244 | #undef statetypes
|
---|
245 | #undef statereferences
|
---|
246 | #undef MAXSTATETYPES
|
---|
247 |
|
---|
248 | #undef ACTION
|
---|
249 | #undef COMMAND
|
---|
250 | #undef PARAMS
|
---|
251 | #undef STATE
|
---|
252 | #undef INSTANCE
|
---|
253 |
|
---|
254 | #undef ACTIONNAME
|
---|
255 | #undef CATEGORY
|
---|
256 | #undef TOKEN
|
---|