3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
33 * Declares gmx::Options.
35 * Together with basicoptions.h, this header forms the part of the public
36 * API that most classes will use to provide options.
38 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
40 * \ingroup module_options
42 #ifndef GMX_OPTIONS_OPTIONS_H
43 #define GMX_OPTIONS_OPTIONS_H
47 #include "../utility/common.h"
48 #include "../utility/gmxassert.h"
50 #include "abstractoption.h"
56 class OptionsAssigner;
57 class OptionsIterator;
60 * Collection of options.
62 * This class provides a standard interface for implementing input options.
63 * Standard usage is to write a method that creates an Options that is owned by
64 * the object, populates it with supported options, and then returns it:
66 // <as class attributes>
68 Options options("common", "Common Options");
73 using gmx::StringOption;
74 using gmx::IntegerOption;
75 options.addOption(StringOption("arg1").store(&arg1));
76 options.addOption(IntegerOption("arg2").store(&arg2));
79 * The caller of that method can then use a parser implementation such as
80 * CommandLineParser to provide values for the options.
82 * Header basicoptions.h provides declarations of several standard
83 * option types for use with addOption(). Documentation of those classes
84 * also give more examples of how to define options.
86 * In order to keep the public interface of this class simple and to reduce
87 * build dependencies on objects that simply provide options, functionality
88 * to assign values to options is provided by a separate OptionsAssigner class.
89 * Similarly, functionality for looping over all options (e.g., for writing out
90 * help) is provided by OptionsIterator.
93 * \ingroup module_options
99 * Initializes the name and title of an option collection.
101 * \param[in] name Single-word name.
102 * \param[in] title Descriptive title.
104 * Copies the input strings.
106 Options(const char *name, const char *title);
109 //! Returns the short name of the option collection.
110 const std::string &name() const;
111 //! Returns the title of the option collection.
112 const std::string &title() const;
113 //! Returns the full description of the option collection.
114 const std::string &description() const;
117 * Sets the full description of the option collection.
119 * \param[in] desc String to set as the description.
121 * concatenateStrings() is useful for forming the input string.
123 void setDescription(const std::string &desc);
124 //int addBugs(int nbugs, const char *const *bugs);
127 * Adds an option collection as a subsection of this collection.
129 * \param[in] section Subsection to add.
131 * The name() field of \p section is used as the name of the
132 * subsection. If an attempt is made to add two different subsections
133 * with the same name, this function asserts.
135 * For certain functionality to work properly, no options should
136 * be added to the subsection after it has been added to another
139 * Only a pointer to the provided object is stored. The caller is
140 * responsible that the object exists for the lifetime of the
142 * It is not possible to add the same Options object as a subsection to
143 * several different Options.
144 * If an attempt is made, the function asserts.
146 void addSubSection(Options *section);
148 * Adds a recognized option to the collection.
150 * \param[in] settings Option description.
151 * \returns OptionInfo object for the created option (never NULL).
152 * \throws APIError if invalid option settings are provided.
154 * This method provides the internal implementation, but in most cases
155 * the templated method is called from user code.
156 * See the templated method for more details.
158 OptionInfo *addOption(const AbstractOption &settings);
160 * Adds a recognized option to the collection.
162 * \tparam OptionType Type of the options description object.
163 * \param[in] settings Option description.
164 * \returns OptionInfo object for the created option (never NULL).
165 * \throws APIError if invalid option settings are provided.
167 * The return value is a pointer for more convenient use in callers:
168 * often callers need to declare the variable that will hold the return
169 * value in wider scope than would be achieved by declaring it at the
170 * site where addOption() is called.
171 * The returned pointer must not be freed.
173 * See \link Options class documentation \endlink for example usage.
176 * \p OptionType::InfoType must specify a type that derives from
177 * OptionInfo and matches the type that is returned by
178 * AbstractOptionStorage::optionInfo() for the storage object that
179 * corresponds to \p OptionType.
181 template <class OptionType>
182 typename OptionType::InfoType *addOption(const OptionType &settings)
185 = addOption(static_cast<const AbstractOption &>(settings));
186 GMX_ASSERT(info->isType<typename OptionType::InfoType>(),
187 "Mismatching option info type declaration and implementation");
188 return info->toType<typename OptionType::InfoType>();
191 //! Returns true if option \p name is set.
192 bool isSet(const char *name) const;
194 * Notifies the collection that all option values are assigned.
196 * \throws InvalidInputError if invalid user input is detected.
198 * This function should be called after no more option values are
199 * to be assigned. Values in storage variables are guaranteed to be
200 * available only after this call, although in most cases, they are
201 * available already during assignment.
203 * If invalid option values, e.g., missing required option, is detected
204 * at this point, this function throws. The thrown exception contains
205 * information on all errors detected during the call.
212 PrivateImplPointer<Impl> impl_;
214 //! Needed to be able to extend the interface of this object.
215 friend class OptionsAssigner;
216 //! Needed to be able to extend the interface of this object.
217 friend class OptionsIterator;
218 //! Needed to be able to extend the interface of this object.
219 friend class OptionsModifyingIterator;