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