More generic way to obtain OptionInfo for options.
[alexxy/gromacs.git] / src / gromacs / selection / selectionoption.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Implements classes in selectionoption.h and selectionoptionstorage.h.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_selection
37  */
38 #include "selectionfileoption.h"
39 #include "selectionfileoptioninfo.h"
40 #include "selectionoption.h"
41 #include "selectionoptioninfo.h"
42
43 #include <string>
44
45 #include "gromacs/options/optionsvisitor.h"
46 #include "gromacs/selection/selection.h"
47 #include "gromacs/selection/selectionoptionmanager.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/gmxassert.h"
50 #include "gromacs/utility/messagestringcollector.h"
51
52 #include "selectionfileoptionstorage.h"
53 #include "selectionoptionstorage.h"
54
55 namespace gmx
56 {
57
58 /********************************************************************
59  * SelectionOptionStorage
60  */
61
62 SelectionOptionStorage::SelectionOptionStorage(const SelectionOption &settings)
63     : MyBase(settings, OptionFlags() | efOption_NoDefaultValue
64                            | efOption_DontCheckMinimumCount),
65       info_(this), manager_(NULL), selectionFlags_(settings.selectionFlags_)
66 {
67     GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
68                        "allowMultiple() is not supported for selection options");
69 }
70
71
72 void SelectionOptionStorage::setManager(SelectionOptionManager *manager)
73 {
74     GMX_RELEASE_ASSERT(manager_ == NULL || manager_ == manager,
75                        "Manager cannot be changed once set");
76     if (manager_ == NULL)
77     {
78         manager->registerOption(this);
79         manager_ = manager;
80     }
81 }
82
83
84 std::string SelectionOptionStorage::formatSingleValue(const Selection &value) const
85 {
86     return value.selectionText();
87 }
88
89
90 void SelectionOptionStorage::addSelections(
91         const SelectionList &selections,
92         bool bFullValue)
93 {
94     if (bFullValue && selections.size() < static_cast<size_t>(minValueCount()))
95     {
96         GMX_THROW(InvalidInputError("Too few selections provided"));
97     }
98     if (bFullValue)
99     {
100         clearSet();
101     }
102     SelectionList::const_iterator i;
103     for (i = selections.begin(); i != selections.end(); ++i)
104     {
105         // TODO: Having this check in the parser would make interactive input
106         // behave better.
107         if (selectionFlags_.test(efSelection_OnlyStatic) && i->isDynamic())
108         {
109             GMX_THROW(InvalidInputError("Dynamic selections not supported"));
110         }
111         addValue(*i);
112     }
113     if (bFullValue)
114     {
115         commitValues();
116         markAsSet();
117     }
118 }
119
120
121 void SelectionOptionStorage::convertValue(const std::string &value)
122 {
123     GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
124
125     manager_->convertOptionValue(this, value);
126 }
127
128 void SelectionOptionStorage::processSetValues(ValueList *values)
129 {
130     GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
131
132     if (values->size() == 0)
133     {
134         manager_->requestOptionDelayedParsing(this);
135     }
136     else if (values->size() < static_cast<size_t>(minValueCount()))
137     {
138         GMX_THROW(InvalidInputError("Too few (valid) values provided"));
139     }
140     ValueList::iterator i;
141     for (i = values->begin(); i != values->end(); ++i)
142     {
143         i->data().setFlags(selectionFlags_);
144     }
145 }
146
147 void SelectionOptionStorage::processAll()
148 {
149     if (isRequired() && !isSet())
150     {
151         GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
152
153         manager_->requestOptionDelayedParsing(this);
154         markAsSet();
155     }
156 }
157
158 void SelectionOptionStorage::setAllowedValueCount(int count)
159 {
160     // TODO: It should be possible to have strong exception safety here.
161     MessageStringCollector errors;
162     errors.startContext("In option '" + name() + "'");
163     if (count >= 0)
164     {
165         // Should not throw because efOption_DontCheckMinimumCount is set.
166         setMinValueCount(count);
167         if (valueCount() > 0 && valueCount() < count)
168         {
169             errors.append("Too few (valid) values provided");
170         }
171     }
172     try
173     {
174         setMaxValueCount(count);
175     }
176     catch (const UserInputError &ex)
177     {
178         errors.append(ex.what());
179     }
180     errors.finishContext();
181     if (!errors.isEmpty())
182     {
183         GMX_THROW(InvalidInputError(errors.toString()));
184     }
185 }
186
187 void SelectionOptionStorage::setSelectionFlag(SelectionFlag flag, bool bSet)
188 {
189     ValueList::iterator i;
190     for (i = values().begin(); i != values().end(); ++i)
191     {
192         if (flag == efSelection_OnlyStatic && bSet && i->isDynamic())
193         {
194             MessageStringCollector errors;
195             errors.startContext("In option '" + name() + "'");
196             errors.append("Dynamic selections not supported");
197             errors.finishContext();
198             GMX_THROW(InvalidInputError(errors.toString()));
199         }
200     }
201     selectionFlags_.set(flag, bSet);
202     for (i = values().begin(); i != values().end(); ++i)
203     {
204         i->data().setFlags(selectionFlags_);
205     }
206 }
207
208
209 /********************************************************************
210  * SelectionOptionInfo
211  */
212
213 SelectionOptionInfo::SelectionOptionInfo(SelectionOptionStorage *option)
214     : OptionInfo(option)
215 {
216 }
217
218 SelectionOptionStorage &SelectionOptionInfo::option()
219 {
220     return static_cast<SelectionOptionStorage &>(OptionInfo::option());
221 }
222
223 const SelectionOptionStorage &SelectionOptionInfo::option() const
224 {
225     return static_cast<const SelectionOptionStorage &>(OptionInfo::option());
226 }
227
228 void SelectionOptionInfo::setManager(SelectionOptionManager *manager)
229 {
230     option().setManager(manager);
231 }
232
233 void SelectionOptionInfo::setValueCount(int count)
234 {
235     option().setAllowedValueCount(count);
236 }
237
238 void SelectionOptionInfo::setEvaluateVelocities(bool bEnabled)
239 {
240     option().setSelectionFlag(efSelection_EvaluateVelocities, bEnabled);
241 }
242
243 void SelectionOptionInfo::setEvaluateForces(bool bEnabled)
244 {
245     option().setSelectionFlag(efSelection_EvaluateForces, bEnabled);
246 }
247
248 void SelectionOptionInfo::setOnlyAtoms(bool bEnabled)
249 {
250     option().setSelectionFlag(efSelection_OnlyAtoms, bEnabled);
251 }
252
253 void SelectionOptionInfo::setOnlyStatic(bool bEnabled)
254 {
255     option().setSelectionFlag(efSelection_OnlyStatic, bEnabled);
256 }
257
258 void SelectionOptionInfo::setDynamicMask(bool bEnabled)
259 {
260     option().setSelectionFlag(efSelection_DynamicMask, bEnabled);
261 }
262
263 void SelectionOptionInfo::setDynamicOnlyWhole(bool bEnabled)
264 {
265     option().setSelectionFlag(efSelection_DynamicOnlyWhole, bEnabled);
266 }
267
268
269 /********************************************************************
270  * SelectionOption
271  */
272
273 AbstractOptionStoragePointer SelectionOption::createStorage() const
274 {
275     return AbstractOptionStoragePointer(new SelectionOptionStorage(*this));
276 }
277
278
279 /********************************************************************
280  * SelectionFileOptionStorage
281  */
282
283 SelectionFileOptionStorage::SelectionFileOptionStorage(const SelectionFileOption &settings)
284     : AbstractOptionStorage(settings, OptionFlags() | efOption_MultipleTimes
285                                           | efOption_DontCheckMinimumCount),
286       info_(this), manager_(NULL), bValueParsed_(false)
287 {
288 }
289
290 void SelectionFileOptionStorage::clearSet()
291 {
292     bValueParsed_ = false;
293 }
294
295 void SelectionFileOptionStorage::convertValue(const std::string &value)
296 {
297     GMX_RELEASE_ASSERT(manager_ != NULL, "Manager is not set");
298
299     if (bValueParsed_)
300     {
301         GMX_THROW(InvalidInputError("More than one file name provided"));
302     }
303     bValueParsed_ = true;
304     // TODO: Should we throw an InvalidInputError if the file does not exist?
305     manager_->parseRequestedFromFile(value);
306 }
307
308 void SelectionFileOptionStorage::processSet()
309 {
310     if (!bValueParsed_)
311     {
312         GMX_THROW(InvalidInputError("No file name provided"));
313     }
314 }
315
316
317 /********************************************************************
318  * SelectionFileOptionInfo
319  */
320
321 SelectionFileOptionInfo::SelectionFileOptionInfo(SelectionFileOptionStorage *option)
322     : OptionInfo(option)
323 {
324 }
325
326 SelectionFileOptionStorage &SelectionFileOptionInfo::option()
327 {
328     return static_cast<SelectionFileOptionStorage &>(OptionInfo::option());
329 }
330
331 const SelectionFileOptionStorage &SelectionFileOptionInfo::option() const
332 {
333     return static_cast<const SelectionFileOptionStorage &>(OptionInfo::option());
334 }
335
336 void SelectionFileOptionInfo::setManager(SelectionOptionManager *manager)
337 {
338     option().setManager(manager);
339 }
340
341
342 /********************************************************************
343  * SelectionFileOption
344  */
345
346 SelectionFileOption::SelectionFileOption(const char *name)
347     : AbstractOption(name)
348 {
349     setDescription("Provide selections from files");
350 }
351
352 AbstractOptionStoragePointer SelectionFileOption::createStorage() const
353 {
354     return AbstractOptionStoragePointer(new SelectionFileOptionStorage(*this));
355 }
356
357
358 /********************************************************************
359  * Global functions
360  */
361
362 namespace
363 {
364
365 /*! \internal \brief
366  * Visitor that sets the manager for each selection option.
367  *
368  * \ingroup module_selection
369  */
370 class SelectionOptionManagerSetter : public OptionsModifyingVisitor
371 {
372     public:
373         //! Construct a visitor that sets given manager.
374         explicit SelectionOptionManagerSetter(SelectionOptionManager *manager)
375             : manager_(manager)
376         {
377         }
378
379         void visitSubSection(Options *section)
380         {
381             OptionsModifyingIterator iterator(section);
382             iterator.acceptSubSections(this);
383             iterator.acceptOptions(this);
384         }
385
386         void visitOption(OptionInfo *option)
387         {
388             SelectionOptionInfo *selOption
389                 = option->toType<SelectionOptionInfo>();
390             if (selOption != NULL)
391             {
392                 selOption->setManager(manager_);
393             }
394             SelectionFileOptionInfo *selFileOption
395                 = option->toType<SelectionFileOptionInfo>();
396             if (selFileOption != NULL)
397             {
398                 selFileOption->setManager(manager_);
399             }
400         }
401
402     private:
403         SelectionOptionManager *manager_;
404 };
405
406 } // namespace
407
408 void setManagerForSelectionOptions(Options *options,
409                                    SelectionOptionManager *manager)
410 {
411     SelectionOptionManagerSetter(manager).visitSubSection(options);
412 }
413
414 } // namespace gmx