Merge "Merge remote-tracking branch 'gerrit/release-4-6'"
[alexxy/gromacs.git] / src / gromacs / options / abstractoption.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 abstractoption.h and abstractoptionstorage.h.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_options
37  */
38 #include "gromacs/options/abstractoption.h"
39
40 #include "gromacs/options/abstractoptionstorage.h"
41 #include "gromacs/options/optionflags.h"
42 #include "gromacs/options/optioninfo.h"
43 #include "gromacs/utility/exceptions.h"
44 #include "gromacs/utility/gmxassert.h"
45
46 #include "basicoptionstorage.h"
47
48 namespace gmx
49 {
50
51 /********************************************************************
52  * AbstractOptionStorage
53  */
54
55 AbstractOptionStorage::AbstractOptionStorage(const AbstractOption &settings,
56                                              OptionFlags staticFlags)
57     : flags_(settings.flags_ | staticFlags),
58       minValueCount_(settings.minValueCount_),
59       maxValueCount_(settings.maxValueCount_),
60       inSet_(false)
61 {
62     // Check that user has not provided incorrect values for vectors.
63     if (hasFlag(efOption_Vector) && (minValueCount_ > 1 || maxValueCount_ < 1))
64     {
65         GMX_THROW(APIError("Inconsistent value counts for vector values"));
66     }
67
68     if (settings.name_ != NULL)
69     {
70         name_  = settings.name_;
71     }
72     descr_ = settings.createDescription();
73     setFlag(efOption_ClearOnNextSet);
74 }
75
76 AbstractOptionStorage::~AbstractOptionStorage()
77 {
78 }
79
80 bool AbstractOptionStorage::isBoolean() const
81 {
82     return dynamic_cast<const BooleanOptionStorage *>(this) != NULL;
83 }
84
85 void AbstractOptionStorage::startSource()
86 {
87     setFlag(efOption_ClearOnNextSet);
88 }
89
90 void AbstractOptionStorage::startSet()
91 {
92     GMX_RELEASE_ASSERT(!inSet_, "finishSet() not called");
93     // The last condition takes care of the situation where multiple
94     // sources are used, and a later source should be able to reassign
95     // the value even though the option is already set.
96     if (isSet() && !hasFlag(efOption_MultipleTimes)
97         && !hasFlag(efOption_ClearOnNextSet))
98     {
99         GMX_THROW(InvalidInputError("Option specified multiple times"));
100     }
101     clearSet();
102     inSet_ = true;
103 }
104
105 void AbstractOptionStorage::appendValue(const std::string &value)
106 {
107     GMX_RELEASE_ASSERT(inSet_, "startSet() not called");
108     convertValue(value);
109 }
110
111 void AbstractOptionStorage::finishSet()
112 {
113     GMX_RELEASE_ASSERT(inSet_, "startSet() not called");
114     inSet_ = false;
115     // TODO: Should this be set or not when processSet() throws?
116     setFlag(efOption_Set);
117     // TODO: Correct handling of the efOption_ClearOnNextSet requires
118     // processSet() and/or convertValue() to check it internally.
119     // OptionStorageTemplate takes care of it, but it's error-prone if
120     // a custom option is implemented that doesn't use it.
121     processSet();
122     clearFlag(efOption_ClearOnNextSet);
123 }
124
125 void AbstractOptionStorage::finish()
126 {
127     GMX_RELEASE_ASSERT(!inSet_, "finishSet() not called");
128     processAll();
129     if (isRequired() && !isSet())
130     {
131         GMX_THROW(InvalidInputError("Option is required, but not set"));
132     }
133 }
134
135 void AbstractOptionStorage::setMinValueCount(int count)
136 {
137     GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
138                        "setMinValueCount() not supported with efOption_MultipleTimes");
139     GMX_RELEASE_ASSERT(count >= 0, "Invalid value count");
140     minValueCount_ = count;
141     if (isSet() && !hasFlag(efOption_DontCheckMinimumCount)
142         && valueCount() < minValueCount_)
143     {
144         GMX_THROW(InvalidInputError("Too few values"));
145     }
146 }
147
148 void AbstractOptionStorage::setMaxValueCount(int count)
149 {
150     GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
151                        "setMaxValueCount() not supported with efOption_MultipleTimes");
152     GMX_RELEASE_ASSERT(count >= -1, "Invalid value count");
153     maxValueCount_ = count;
154     if (isSet() && maxValueCount_ >= 0 && valueCount() > maxValueCount_)
155     {
156         GMX_THROW(InvalidInputError("Too many values"));
157     }
158 }
159
160 /********************************************************************
161  * OptionInfo
162  */
163
164 /*! \cond libapi */
165 OptionInfo::OptionInfo(AbstractOptionStorage *option)
166     : option_(*option)
167 {
168 }
169 //! \endcond
170
171 OptionInfo::~OptionInfo()
172 {
173 }
174
175 bool OptionInfo::isSet() const
176 {
177     return option().isSet();
178 }
179
180 bool OptionInfo::isHidden() const
181 {
182     return option().isHidden();
183 }
184
185 bool OptionInfo::isRequired() const
186 {
187     return option().isRequired();
188 }
189
190 const std::string &OptionInfo::name() const
191 {
192     return option().name();
193 }
194
195 const std::string &OptionInfo::description() const
196 {
197     return option().description();
198 }
199
200 const char *OptionInfo::type() const
201 {
202     return option().typeString();
203 }
204
205 int OptionInfo::valueCount() const
206 {
207     return option().valueCount();
208 }
209
210 std::string OptionInfo::formatValue(int i) const
211 {
212     return option().formatValue(i);
213 }
214
215 std::string OptionInfo::formatDefaultValueIfSet() const
216 {
217     return option().formatDefaultValueIfSet();
218 }
219
220 } // namespace gmx