Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / options / options.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements gmx::Options.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_options
41  */
42 #include "gromacs/options/options.h"
43
44 #include "gromacs/options/abstractoption.h"
45 #include "gromacs/options/abstractoptionstorage.h"
46 #include "gromacs/utility/arrayref.h"
47 #include "gromacs/utility/exceptions.h"
48 #include "gromacs/utility/gmxassert.h"
49 #include "gromacs/utility/messagestringcollector.h"
50 #include "gromacs/utility/stringutil.h"
51
52 #include "options-impl.h"
53
54 namespace gmx
55 {
56
57 /********************************************************************
58  * Options::Impl
59  */
60
61 Options::Impl::Impl(const char *name, const char *title)
62     : name_(name != NULL ? name : ""), title_(title != NULL ? title : ""),
63       parent_(NULL)
64 {
65 }
66
67 Options::Impl::~Impl()
68 {
69 }
70
71 Options *Options::Impl::findSubSection(const char *name) const
72 {
73     SubSectionList::const_iterator i;
74     for (i = subSections_.begin(); i != subSections_.end(); ++i)
75     {
76         if ((*i)->name() == name)
77         {
78             return *i;
79         }
80     }
81     return NULL;
82 }
83
84 AbstractOptionStorage *Options::Impl::findOption(const char *name) const
85 {
86     OptionList::const_iterator i;
87     for (i = options_.begin(); i != options_.end(); ++i)
88     {
89         if ((*i)->name() == name)
90         {
91             return i->get();
92         }
93     }
94     return NULL;
95 }
96
97 void Options::Impl::startSource()
98 {
99     OptionList::const_iterator i;
100     for (i = options_.begin(); i != options_.end(); ++i)
101     {
102         AbstractOptionStorage &option = **i;
103         option.startSource();
104     }
105     SubSectionList::const_iterator j;
106     for (j = subSections_.begin(); j != subSections_.end(); ++j)
107     {
108         Options &section = **j;
109         section.impl_->startSource();
110     }
111 }
112
113 /********************************************************************
114  * Options
115  */
116
117 Options::Options(const char *name, const char *title)
118     : impl_(new Impl(name, title))
119 {
120 }
121
122 Options::~Options()
123 {
124 }
125
126 const std::string &Options::name() const
127 {
128     return impl_->name_;
129 }
130
131 const std::string &Options::title() const
132 {
133     return impl_->title_;
134 }
135
136 const std::string &Options::description() const
137 {
138     return impl_->description_;
139 }
140
141 void Options::setDescription(const std::string &desc)
142 {
143     impl_->description_ = desc;
144 }
145
146 void Options::setDescription(const ConstArrayRef<const char *> &descArray)
147 {
148     impl_->description_ = concatenateStrings(descArray.data(), descArray.size());
149 }
150
151 void Options::addSubSection(Options *section)
152 {
153     // Make sure that section is not already inserted somewhere.
154     GMX_RELEASE_ASSERT(section->impl_->parent_ == NULL,
155                        "Cannot add as subsection twice");
156     // Make sure that there are no duplicate sections.
157     GMX_RELEASE_ASSERT(impl_->findSubSection(section->name().c_str()) == NULL,
158                        "Duplicate subsection name");
159     impl_->subSections_.push_back(section);
160     section->impl_->parent_ = this;
161 }
162
163 OptionInfo *Options::addOption(const AbstractOption &settings)
164 {
165     AbstractOptionStoragePointer option(settings.createStorage());
166     if (impl_->findOption(option->name().c_str()) != NULL)
167     {
168         GMX_THROW(APIError("Duplicate option: " + option->name()));
169     }
170     impl_->options_.push_back(move(option));
171     return &impl_->options_.back()->optionInfo();
172 }
173
174 bool Options::isSet(const char *name) const
175 {
176     AbstractOptionStorage *option = impl_->findOption(name);
177     return (option != NULL ? option->isSet() : false);
178 }
179
180 void Options::finish()
181 {
182     // TODO: Consider how to customize these error messages based on context.
183     ExceptionInitializer             errors("Invalid input values");
184     Impl::OptionList::const_iterator i;
185     for (i = impl_->options_.begin(); i != impl_->options_.end(); ++i)
186     {
187         AbstractOptionStorage &option = **i;
188         try
189         {
190             option.finish();
191         }
192         catch (UserInputError &ex)
193         {
194             ex.prependContext("In option " + option.name());
195             errors.addCurrentExceptionAsNested();
196         }
197     }
198     Impl::SubSectionList::const_iterator j;
199     for (j = impl_->subSections_.begin(); j != impl_->subSections_.end(); ++j)
200     {
201         Options &section = **j;
202         try
203         {
204             section.finish();
205         }
206         catch (const UserInputError &)
207         {
208             errors.addCurrentExceptionAsNested();
209         }
210     }
211     if (errors.hasNestedExceptions())
212     {
213         // TODO: This exception type may not always be appropriate.
214         GMX_THROW(InvalidInputError(errors));
215     }
216 }
217
218 } // namespace gmx