1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
6 | *
|
---|
7 | *
|
---|
8 | * This file is part of MoleCuilder.
|
---|
9 | *
|
---|
10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * Registry<Action>.cpp
|
---|
26 | *
|
---|
27 | * Created on: Jan 7, 2010
|
---|
28 | * Author: crueger
|
---|
29 | */
|
---|
30 |
|
---|
31 | // include config.h
|
---|
32 | #ifdef HAVE_CONFIG_H
|
---|
33 | #include <config.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include "CodePatterns/MemDebug.hpp"
|
---|
37 |
|
---|
38 | #include "CodePatterns/Assert.hpp"
|
---|
39 |
|
---|
40 | #include "Actions/ActionRegistry.hpp"
|
---|
41 | #include "Actions/OptionRegistry.hpp"
|
---|
42 | #include "Actions/OptionTrait.hpp"
|
---|
43 | #include "CodePatterns/Registry_impl.hpp"
|
---|
44 |
|
---|
45 | #include "GlobalListOfActions.hpp"
|
---|
46 |
|
---|
47 | #include "AllActionHeaders.hpp"
|
---|
48 |
|
---|
49 | #include <boost/preprocessor/iteration/local.hpp>
|
---|
50 | #include <boost/preprocessor/seq/elem.hpp>
|
---|
51 | #include <boost/preprocessor/seq/size.hpp>
|
---|
52 |
|
---|
53 | using namespace MoleCuilder;
|
---|
54 |
|
---|
55 | // static entities
|
---|
56 | bool ActionRegistry::completely_instatiated = false;
|
---|
57 |
|
---|
58 | /** Constructor for class ActionRegistry.
|
---|
59 | */
|
---|
60 | ActionRegistry::ActionRegistry()
|
---|
61 | {
|
---|
62 | //std::cout << "ActionRegistry::ActionRegistry() called, instance is " << this << "." << std::endl;
|
---|
63 | fillRegistry();
|
---|
64 | prepareAllMakroActions();
|
---|
65 | fillOptionRegistry();
|
---|
66 | completely_instatiated = true;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Destructor for class ActionRegistry.
|
---|
70 | */
|
---|
71 | ActionRegistry::~ActionRegistry()
|
---|
72 | {
|
---|
73 | // first unregister all their options
|
---|
74 | clearOptionRegistry();
|
---|
75 |
|
---|
76 | // add all MakroActions: PLEASE adhere to the alphabetical ordering
|
---|
77 | {
|
---|
78 | MakroAction & presentAction =
|
---|
79 | const_cast<MakroAction &>(
|
---|
80 | dynamic_cast<const MakroAction &>(getActionByName("molecular-dynamics")));
|
---|
81 | presentAction.unprepare(*this);
|
---|
82 | }
|
---|
83 | {
|
---|
84 | MakroAction & presentAction =
|
---|
85 | const_cast<MakroAction &>(
|
---|
86 | dynamic_cast<const MakroAction &>(getActionByName("optimize-structure")));
|
---|
87 | presentAction.unprepare(*this);
|
---|
88 | }
|
---|
89 | {
|
---|
90 | MakroAction & presentAction =
|
---|
91 | const_cast<MakroAction &>(
|
---|
92 | dynamic_cast<const MakroAction &>(getActionByName("subgraph-dissection")));
|
---|
93 | presentAction.unprepare(*this);
|
---|
94 | }
|
---|
95 | {
|
---|
96 | MakroAction & presentAction =
|
---|
97 | const_cast<MakroAction &>(
|
---|
98 | dynamic_cast<const MakroAction &>(getActionByName("translate-molecules")));
|
---|
99 | presentAction.unprepare(*this);
|
---|
100 | }
|
---|
101 |
|
---|
102 | //std::cout << "ActionRegistry::~ActionRegistry() called, instance is " << this << "." << std::endl;
|
---|
103 | cleanup();
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /** Instantiates each existing Action.
|
---|
108 | *
|
---|
109 | */
|
---|
110 | void ActionRegistry::fillRegistry()
|
---|
111 | {
|
---|
112 | #define instance_print(z,n,list) \
|
---|
113 | { \
|
---|
114 | Action * presentAction = new \
|
---|
115 | BOOST_PP_CAT(BOOST_PP_SEQ_ELEM(n, list), Action) \
|
---|
116 | (); \
|
---|
117 | registerInstance(presentAction); \
|
---|
118 | }
|
---|
119 | #define BOOST_PP_LOCAL_MACRO(n) instance_print(~, n, GLOBALLISTOFACTIONS)
|
---|
120 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFACTIONS)))
|
---|
121 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
122 | #undef instance_print
|
---|
123 | }
|
---|
124 |
|
---|
125 | void ActionRegistry::prepareAllMakroActions()
|
---|
126 | {
|
---|
127 | // now prepare each macro action that require presence of all primitive ones
|
---|
128 | {
|
---|
129 | MakroAction & presentAction =
|
---|
130 | const_cast<MakroAction &>(
|
---|
131 | dynamic_cast<const MakroAction &>(getActionByName("subgraph-dissection")));
|
---|
132 | presentAction.prepare(*this);
|
---|
133 | }
|
---|
134 | {
|
---|
135 | MakroAction & presentAction =
|
---|
136 | const_cast<MakroAction &>(
|
---|
137 | dynamic_cast<const MakroAction &>(getActionByName("molecular-dynamics")));
|
---|
138 | presentAction.prepare(*this);
|
---|
139 | }
|
---|
140 | {
|
---|
141 | MakroAction & presentAction =
|
---|
142 | const_cast<MakroAction &>(
|
---|
143 | dynamic_cast<const MakroAction &>(getActionByName("optimize-structure")));
|
---|
144 | presentAction.prepare(*this);
|
---|
145 | }
|
---|
146 | {
|
---|
147 | MakroAction & presentAction =
|
---|
148 | const_cast<MakroAction &>(
|
---|
149 | dynamic_cast<const MakroAction &>(getActionByName("translate-molecules")));
|
---|
150 | presentAction.prepare(*this);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Registers every existing Action's option with the OptionRegistry.
|
---|
155 | *
|
---|
156 | */
|
---|
157 | void ActionRegistry::fillOptionRegistry() const
|
---|
158 | {
|
---|
159 | for (const_iterator iter = getBeginIter(); iter != getEndIter(); ++iter) {
|
---|
160 | // register with OptionRegistry
|
---|
161 | Action ¤Action = *(iter->second);
|
---|
162 | for (ActionTrait::options_const_iterator optioniter = currenAction.Traits.getBeginIter();
|
---|
163 | optioniter != currenAction.Traits.getEndIter();
|
---|
164 | ++optioniter) {
|
---|
165 | // options may have been re-used by other Actions, so check beforehand whether adding is needed
|
---|
166 | if (!OptionRegistry::getInstance().isOptionPresentByName((optioniter->first))) {
|
---|
167 | OptionRegistry::getInstance().registerInstance(optioniter->second);
|
---|
168 | } else { // if present, ASSERT that types coincide
|
---|
169 | #ifndef NDEBUG
|
---|
170 | OptionTrait const * const PresentOption = OptionRegistry::getInstance().getOptionByName(optioniter->first);
|
---|
171 | #endif
|
---|
172 | ASSERT(PresentOption->getType() == optioniter->second->getType(),
|
---|
173 | ("Action::Action() - option to add "+
|
---|
174 | std::string(optioniter->first)+
|
---|
175 | " of Action "+
|
---|
176 | std::string(currenAction.getName())+
|
---|
177 | " is already present with different type: "
|
---|
178 | +toString(PresentOption->getType())+" != "+toString(optioniter->second->getType())
|
---|
179 | )
|
---|
180 | );
|
---|
181 | ASSERT(PresentOption->getDefaultValue() == optioniter->second->getDefaultValue(),
|
---|
182 | ("Action::Action() - option to add "+
|
---|
183 | std::string(optioniter->first)+
|
---|
184 | " of Action "+
|
---|
185 | std::string(currenAction.getName())+
|
---|
186 | " is already present with different default value: "
|
---|
187 | +PresentOption->getDefaultValue()+" != "+optioniter->second->getDefaultValue()
|
---|
188 | )
|
---|
189 | );
|
---|
190 | ASSERT(PresentOption->getShortForm() == optioniter->second->getShortForm(),
|
---|
191 | ("Action::Action() - option to add "+
|
---|
192 | std::string(optioniter->first)+
|
---|
193 | " of Action "+
|
---|
194 | std::string(currenAction.getName())+
|
---|
195 | " is already present with different short form: "
|
---|
196 | +PresentOption->getShortForm()+" != "+optioniter->second->getShortForm()
|
---|
197 | )
|
---|
198 | );
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | /** Unregisters every existing Action's option from OptionRegistry.
|
---|
205 | *
|
---|
206 | */
|
---|
207 | void ActionRegistry::clearOptionRegistry() const
|
---|
208 | {
|
---|
209 | for (const_iterator iter = getBeginIter(); iter != getEndIter(); ++iter) {
|
---|
210 | Action ¤Action = *(iter->second);
|
---|
211 | for (ActionTrait::options_const_iterator optioniter = currenAction.Traits.getBeginIter();
|
---|
212 | optioniter != currenAction.Traits.getEndIter();
|
---|
213 | ++optioniter) {
|
---|
214 | // unregister option if still registered
|
---|
215 | if(OptionRegistry::getInstance().isOptionPresentByName((optioniter->first)))
|
---|
216 | if (OptionRegistry::getInstance().getOptionByName((optioniter->first)) == optioniter->second) {
|
---|
217 | OptionRegistry::getInstance().unregisterInstance(optioniter->second);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Just passes on call to Registry<Action>::getByName().
|
---|
224 | * \param name name of Action
|
---|
225 | * \return const ref to Action
|
---|
226 | */
|
---|
227 | const Action& ActionRegistry::getActionByName(const std::string &name)
|
---|
228 | {
|
---|
229 | Action * const action = getByName(name);
|
---|
230 | ASSERT( action != NULL,
|
---|
231 | "ActionRegistry::getActionByName() - action "+name+" not present!");
|
---|
232 | return *action;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /** Just passes on call to Registry<Action>::isPresentByName().
|
---|
236 | * \param name name of Action
|
---|
237 | * \return true - Action instance present, false - not
|
---|
238 | */
|
---|
239 | bool ActionRegistry::isActionPresentByName(const std::string &name) const
|
---|
240 | {
|
---|
241 | return isPresentByName(name);
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | CONSTRUCT_REGISTRY(Action)
|
---|