SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / options / abstractoption.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010-2017, The GROMACS development team.
5  * Copyright (c) 2019, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS 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 GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements classes in abstractoption.h and abstractoptionstorage.h.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_options
42  */
43 #include "gmxpre.h"
44
45 #include "abstractoption.h"
46
47 #include "gromacs/options/abstractoptionstorage.h"
48 #include "gromacs/utility/any.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/gmxassert.h"
51
52 #include "basicoptionstorage.h"
53 #include "optionflags.h"
54
55 namespace gmx
56 {
57
58 /********************************************************************
59  * AbstractOptionStorage
60  */
61
62 AbstractOptionStorage::AbstractOptionStorage(const AbstractOption& settings, OptionFlags staticFlags) :
63     flags_(settings.flags_ | staticFlags),
64     storeIsSet_(settings.storeIsSet_),
65     minValueCount_(settings.minValueCount_),
66     maxValueCount_(settings.maxValueCount_),
67     bInSet_(false),
68     bSetValuesHadErrors_(false)
69 {
70     // Check that user has not provided incorrect values for vectors.
71     if (hasFlag(efOption_Vector) && (minValueCount_ > 1 || maxValueCount_ < 1))
72     {
73         GMX_THROW(APIError("Inconsistent value counts for vector values"));
74     }
75
76     if (settings.name_ != nullptr)
77     {
78         name_ = settings.name_;
79     }
80     if (settings.descr_ != nullptr)
81     {
82         descr_ = settings.descr_;
83     }
84     if (storeIsSet_ != nullptr)
85     {
86         *storeIsSet_ = false;
87     }
88     setFlag(efOption_ClearOnNextSet);
89 }
90
91 AbstractOptionStorage::~AbstractOptionStorage() {}
92
93 bool AbstractOptionStorage::isBoolean() const
94 {
95     return dynamic_cast<const BooleanOptionStorage*>(this) != nullptr;
96 }
97
98 void AbstractOptionStorage::startSource()
99 {
100     setFlag(efOption_ClearOnNextSet);
101 }
102
103 void AbstractOptionStorage::startSet()
104 {
105     GMX_RELEASE_ASSERT(!bInSet_, "finishSet() not called");
106     // The last condition takes care of the situation where multiple
107     // sources are used, and a later source should be able to reassign
108     // the value even though the option is already set.
109     if (isSet() && !hasFlag(efOption_MultipleTimes) && !hasFlag(efOption_ClearOnNextSet))
110     {
111         GMX_THROW(InvalidInputError("Option specified multiple times"));
112     }
113     clearSet();
114     bInSet_              = true;
115     bSetValuesHadErrors_ = false;
116 }
117
118 void AbstractOptionStorage::appendValue(const Any& value)
119 {
120     GMX_RELEASE_ASSERT(bInSet_, "startSet() not called");
121     try
122     {
123         convertValue(value);
124     }
125     catch (const std::exception&)
126     {
127         bSetValuesHadErrors_ = true;
128         throw;
129     }
130 }
131
132 void AbstractOptionStorage::markAsSet()
133 {
134     setFlag(efOption_Set);
135     if (storeIsSet_ != nullptr)
136     {
137         *storeIsSet_ = true;
138     }
139 }
140
141 void AbstractOptionStorage::finishSet()
142 {
143     GMX_RELEASE_ASSERT(bInSet_, "startSet() not called");
144     bInSet_ = false;
145     // We mark the option as set even when there are errors to avoid additional
146     // errors from required options not set.
147     // TODO: There could be a separate flag for this purpose.
148     markAsSet();
149     if (!bSetValuesHadErrors_)
150     {
151         // TODO: Correct handling of the efOption_ClearOnNextSet requires
152         // processSet() and/or convertValue() to check it internally.
153         // OptionStorageTemplate takes care of it, but it's error-prone if
154         // a custom option is implemented that doesn't use it.
155         processSet();
156     }
157     bSetValuesHadErrors_ = false;
158     clearFlag(efOption_ClearOnNextSet);
159     clearSet();
160 }
161
162 void AbstractOptionStorage::finish()
163 {
164     GMX_RELEASE_ASSERT(!bInSet_, "finishSet() not called");
165     processAll();
166     if (isRequired() && !(isSet() || hasFlag(efOption_ExplicitDefaultValue)))
167     {
168         GMX_THROW(InvalidInputError("Option is required, but not set"));
169     }
170 }
171
172 void AbstractOptionStorage::setMinValueCount(int count)
173 {
174     GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
175                        "setMinValueCount() not supported with efOption_MultipleTimes");
176     GMX_RELEASE_ASSERT(count >= 0, "Invalid value count");
177     minValueCount_ = count;
178     if (isSet() && !hasFlag(efOption_DontCheckMinimumCount) && valueCount() < minValueCount_)
179     {
180         GMX_THROW(InvalidInputError("Too few values"));
181     }
182 }
183
184 void AbstractOptionStorage::setMaxValueCount(int count)
185 {
186     GMX_RELEASE_ASSERT(!hasFlag(efOption_MultipleTimes),
187                        "setMaxValueCount() not supported with efOption_MultipleTimes");
188     GMX_RELEASE_ASSERT(count >= -1, "Invalid value count");
189     maxValueCount_ = count;
190     if (isSet() && maxValueCount_ >= 0 && valueCount() > maxValueCount_)
191     {
192         GMX_THROW(InvalidInputError("Too many values"));
193     }
194 }
195
196 /********************************************************************
197  * OptionInfo
198  */
199
200 /*! \cond libapi */
201 OptionInfo::OptionInfo(AbstractOptionStorage* option) : option_(*option) {}
202 //! \endcond
203
204 OptionInfo::~OptionInfo() {}
205
206 bool OptionInfo::isSet() const
207 {
208     return option().isSet();
209 }
210
211 bool OptionInfo::isHidden() const
212 {
213     return option().isHidden();
214 }
215
216 bool OptionInfo::isRequired() const
217 {
218     return option().isRequired();
219 }
220
221 int OptionInfo::minValueCount() const
222 {
223     if (option().defaultValueIfSetExists())
224     {
225         return 0;
226     }
227     return option().minValueCount();
228 }
229
230 int OptionInfo::maxValueCount() const
231 {
232     return option().maxValueCount();
233 }
234
235 const std::string& OptionInfo::name() const
236 {
237     return option().name();
238 }
239
240 std::string OptionInfo::type() const
241 {
242     return option().typeString();
243 }
244
245 std::string OptionInfo::formatDescription() const
246 {
247     std::string description(option().description());
248     std::string extraDescription(option().formatExtraDescription());
249     if (!extraDescription.empty())
250     {
251         description.append(extraDescription);
252     }
253     return description;
254 }
255
256 std::vector<Any> OptionInfo::defaultValues() const
257 {
258     return option().defaultValues();
259 }
260
261 std::vector<std::string> OptionInfo::defaultValuesAsStrings() const
262 {
263     return option().defaultValuesAsStrings();
264 }
265
266 std::vector<Any> OptionInfo::normalizeValues(const std::vector<Any>& values) const
267 {
268     return option().normalizeValues(values);
269 }
270
271 } // namespace gmx