9ca066ec2404980427b25efaa6c62944cd90084d
[alexxy/gromacs.git] / src / gromacs / options / options.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2014,2015,2016,2018,2019, 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 /*! \file
36  * \brief
37  * Declares gmx::Options.
38  *
39  * Together with basicoptions.h, this header forms the part of the public
40  * API that most classes will use to provide options.
41  *
42  * \author Teemu Murtola <teemu.murtola@gmail.com>
43  * \inpublicapi
44  * \ingroup module_options
45  */
46 #ifndef GMX_OPTIONS_OPTIONS_H
47 #define GMX_OPTIONS_OPTIONS_H
48
49 #include <string>
50
51 #include "gromacs/options/ioptionscontainerwithsections.h"
52 #include "gromacs/utility/classhelpers.h"
53
54 namespace gmx
55 {
56
57 class AbstractOption;
58 class OptionSection;
59 class OptionSectionInfo;
60 class OptionsAssigner;
61
62 namespace internal
63 {
64 class OptionsImpl;
65 }
66
67 /*! \brief
68  * Base class for option managers.
69  *
70  * This class is used as a marker for all classes that are used with
71  * Options::addManager().  It doesn't provide any methods, but only supports
72  * transporting these classes through the Options collection into the
73  * individual option implementation classes.
74  *
75  * The virtual destructor is present to make this class polymorphic, such that
76  * `dynamic_cast` can be used when retrieving a manager of a certain type for
77  * the individual options.
78  *
79  * \inlibraryapi
80  * \ingroup module_options
81  */
82 class IOptionManager
83 {
84 protected:
85     virtual ~IOptionManager();
86 };
87
88 /*! \brief
89  * Collection of options.
90  *
91  * See \ref module_options for an overview of how the options work.
92  * The IOptionsContainerWithSections interface documents how to add options.
93  *
94  * In order to keep the public interface of this class simple, functionality
95  * to assign values to options is provided by a separate OptionsAssigner class.
96  * Similarly, functionality for looping over all options (e.g., for writing out
97  * help) is provided by OptionsIterator.
98  *
99  * \inpublicapi
100  * \ingroup module_options
101  */
102 class Options : public IOptionsContainerWithSections
103 {
104 public:
105     //! Initializes an empty options root container.
106     Options();
107     ~Options() override;
108
109     /*! \brief
110      * Adds an option manager.
111      *
112      * \param    manager Manager to add.
113      * \throws   std::bad_alloc if out of memory.
114      *
115      * Option managers are used by some types of options that require
116      * interaction between different option instances (e.g., selection
117      * options), or need to support globally set properties (e.g., a global
118      * default file prefix).  Option objects can retrieve the pointer to
119      * their manager when they are created, and the caller can alter the
120      * behavior of the options through the manager.
121      * See the individual managers for details.
122      *
123      * Caller is responsible for memory management of \p manager.
124      * The Options object (and its contained options) only stores a
125      * reference to the object.
126      *
127      * This method cannot be called after adding options or sections.
128      */
129     void addManager(IOptionManager* manager);
130
131     // From IOptionsContainer
132     IOptionsContainer& addGroup() override;
133
134     //! Returns a handle to the root section.
135     OptionSectionInfo& rootSection();
136     //! Returns a handle to the root section.
137     const OptionSectionInfo& rootSection() const;
138
139     /*! \brief
140      * Notifies the collection that all option values are assigned.
141      *
142      * \throws InvalidInputError if invalid user input is detected.
143      *
144      * This function should be called after no more option values are
145      * to be assigned.  Values in storage variables are guaranteed to be
146      * available only after this call, although in most cases, they are
147      * available already during assignment.
148      *
149      * If invalid option values, e.g., missing required option, is detected
150      * at this point, this function throws.  The thrown exception contains
151      * information on all errors detected during the call.
152      */
153     void finish();
154
155 private:
156     // From IOptionsContainerWithSections
157     internal::OptionSectionImpl* addSectionImpl(const AbstractOptionSection& section) override;
158     // From IOptionsContainer
159     OptionInfo* addOptionImpl(const AbstractOption& settings) override;
160
161     PrivateImplPointer<internal::OptionsImpl> impl_;
162
163     //! Needed to be able to extend the interface of this object.
164     friend class OptionsAssigner;
165 };
166
167 } // namespace gmx
168
169 #endif