Reformat existing LGPL copyright notices.
[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, 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/exceptions.h"
47 #include "gromacs/utility/gmxassert.h"
48 #include "gromacs/utility/messagestringcollector.h"
49 #include "gromacs/utility/stringutil.h"
50
51 #include "options-impl.h"
52
53 namespace gmx
54 {
55
56 /********************************************************************
57  * Options::Impl
58  */
59
60 Options::Impl::Impl(const char *name, const char *title)
61     : name_(name != NULL ? name : ""), title_(title != NULL ? title : ""),
62       parent_(NULL)
63 {
64 }
65
66 Options::Impl::~Impl()
67 {
68 }
69
70 Options *Options::Impl::findSubSection(const char *name) const
71 {
72     SubSectionList::const_iterator i;
73     for (i = subSections_.begin(); i != subSections_.end(); ++i)
74     {
75         if ((*i)->name() == name)
76         {
77             return *i;
78         }
79     }
80     return NULL;
81 }
82
83 AbstractOptionStorage *Options::Impl::findOption(const char *name) const
84 {
85     OptionList::const_iterator i;
86     for (i = options_.begin(); i != options_.end(); ++i)
87     {
88         if ((*i)->name() == name)
89         {
90             return i->get();
91         }
92     }
93     return NULL;
94 }
95
96 void Options::Impl::startSource()
97 {
98     OptionList::const_iterator i;
99     for (i = options_.begin(); i != options_.end(); ++i)
100     {
101         AbstractOptionStorage &option = **i;
102         option.startSource();
103     }
104     SubSectionList::const_iterator j;
105     for (j = subSections_.begin(); j != subSections_.end(); ++j)
106     {
107         Options &section = **j;
108         section.impl_->startSource();
109     }
110 }
111
112 /********************************************************************
113  * Options
114  */
115
116 Options::Options(const char *name, const char *title)
117     : impl_(new Impl(name, title))
118 {
119 }
120
121 Options::~Options()
122 {
123 }
124
125 const std::string &Options::name() const
126 {
127     return impl_->name_;
128 }
129
130 const std::string &Options::title() const
131 {
132     return impl_->title_;
133 }
134
135 const std::string &Options::description() const
136 {
137     return impl_->description_;
138 }
139
140 void Options::setDescription(const std::string &desc)
141 {
142     impl_->description_ = desc;
143 }
144
145 void Options::addSubSection(Options *section)
146 {
147     // Make sure that section is not already inserted somewhere.
148     GMX_RELEASE_ASSERT(section->impl_->parent_ == NULL,
149                        "Cannot add as subsection twice");
150     // Make sure that there are no duplicate sections.
151     GMX_RELEASE_ASSERT(impl_->findSubSection(section->name().c_str()) == NULL,
152                        "Duplicate subsection name");
153     impl_->subSections_.push_back(section);
154     section->impl_->parent_ = this;
155 }
156
157 OptionInfo *Options::addOption(const AbstractOption &settings)
158 {
159     AbstractOptionStoragePointer option(settings.createStorage());
160     if (impl_->findOption(option->name().c_str()) != NULL)
161     {
162         GMX_THROW(APIError("Duplicate option: " + option->name()));
163     }
164     impl_->options_.push_back(move(option));
165     return &impl_->options_.back()->optionInfo();
166 }
167
168 bool Options::isSet(const char *name) const
169 {
170     AbstractOptionStorage *option = impl_->findOption(name);
171     return (option != NULL ? option->isSet() : false);
172 }
173
174 void Options::finish()
175 {
176     // TODO: Consider how to customize these error messages based on context.
177     ExceptionInitializer             errors("Invalid input values");
178     Impl::OptionList::const_iterator i;
179     for (i = impl_->options_.begin(); i != impl_->options_.end(); ++i)
180     {
181         AbstractOptionStorage &option = **i;
182         try
183         {
184             option.finish();
185         }
186         catch (UserInputError &ex)
187         {
188             ex.prependContext("In option " + option.name());
189             errors.addCurrentExceptionAsNested();
190         }
191     }
192     Impl::SubSectionList::const_iterator j;
193     for (j = impl_->subSections_.begin(); j != impl_->subSections_.end(); ++j)
194     {
195         Options &section = **j;
196         try
197         {
198             section.finish();
199         }
200         catch (const UserInputError &)
201         {
202             errors.addCurrentExceptionAsNested();
203         }
204     }
205     if (errors.hasNestedExceptions())
206     {
207         // TODO: This exception type may not always be appropriate.
208         GMX_THROW(InvalidInputError(errors));
209     }
210 }
211
212 } // namespace gmx