Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / selection / selectionoptionmanager.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2018 by the GROMACS development team.
5  * Copyright (c) 2019,2020, 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::SelectionOptionManager.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \inpublicapi
42  * \ingroup module_selection
43  */
44 #ifndef GMX_SELECTION_SELECTIONOPTIONMANAGER_H
45 #define GMX_SELECTION_SELECTIONOPTIONMANAGER_H
46
47 #include <string>
48
49 #include "gromacs/options/options.h"
50 #include "gromacs/utility/classhelpers.h"
51
52 namespace gmx
53 {
54
55 class SelectionCollection;
56 class SelectionOptionStorage;
57
58 /*! \brief
59  * Handles interaction of selection options with other options and user input.
60  *
61  * This class implements interaction of SelectionOption with
62  * SelectionCollection, and also implements features of SelectionOption that
63  * require actions outside options parsing.
64  * It also implements the coupling between SelectionOption and
65  * SelectionFileOption.
66  * It needs to be added using Options::addManager() before SelectionOption or
67  * SelectionFileOption options can be added to an Options collection.
68  *
69  * The main features of this class are:
70  *  - convertOptionValue(), which is used to convert string values into
71  *    selections for options.
72  *  - requestOptionDelayedParsing(), which is called by the internal
73  *    implementation of selection options when an option is provided on the
74  *    command line without a value.  Such calls are remembered, and the value
75  *    for all requested options can be later provided by calling one of
76  *    parseRequestedFromStdin(), parseRequestedFromFile() or
77  *    parseRequstedFromString().
78  *
79  * \inpublicapi
80  * \ingroup module_selection
81  */
82 class SelectionOptionManager : public IOptionManager
83 {
84 public:
85     /*! \brief
86      * Creates a manager for selection options.
87      *
88      * \throws  std::bad_alloc if out of memory.
89      */
90     explicit SelectionOptionManager(SelectionCollection* selections);
91     ~SelectionOptionManager() override;
92
93     /*! \brief
94      * Adds a selection option to be managed.
95      *
96      * \param     storage  Storage object for the option to register.
97      * \throws    std::bad_alloc if out of memory.
98      *
99      * This is only for internal use by the selection module.
100      * It is not possible to obtain a SelectionOptionStorage pointer
101      * through any public or library API.
102      *
103      * Strong exception safety.
104      */
105     void registerOption(SelectionOptionStorage* storage);
106     /*! \brief
107      * Converts a string value to selections for an option.
108      *
109      * \param     storage  Storage object to receive the selections.
110      * \param[in] value    Value to convert.
111      * \param[in] bFullValue  If true, the provided selections are the full
112      *      value of the option, and additional checks are performed.
113      * \throws    std::bad_alloc if out of memory.
114      * \throws    InvalidInputError if the selection string is not valid,
115      *      or uses a feature not supported by the option.
116      *
117      * This is only for internal use by the selection module.
118      * It is not possible to obtain a SelectionOptionStorage pointer
119      * through any public or library API.
120      */
121     void convertOptionValue(SelectionOptionStorage* storage, const std::string& value, bool bFullValue);
122     /*! \brief
123      * Adds a selection option for delayed user input.
124      *
125      * \param     storage  Storage object for the option to request.
126      * \throws    std::bad_alloc if out of memory.
127      *
128      * This is only for internal use by the selection module.
129      * It is not possible to obtain a SelectionOptionStorage pointer
130      * through any public or library API.
131      *
132      * Strong exception safety.
133      */
134     void requestOptionDelayedParsing(SelectionOptionStorage* storage);
135
136     /*! \brief
137      * Returns whether there are requested selections that need input from
138      * parseRequestedFrom*().
139      */
140     bool hasRequestedSelections() const;
141
142     /*! \brief
143      * Initializes options for setting global selection properties.
144      *
145      * \param[in,out] options Options object to initialize.
146      * \throws        std::bad_alloc if out of memory.
147      *
148      * \see SelectionCollection::initOptions()
149      */
150     void initOptions(IOptionsContainer* options);
151
152     /*! \brief
153      * Parses selection(s) from standard input for options not yet
154      * provided.
155      *
156      * \param[in]  bInteractive Whether the parser should behave
157      *      interactively.
158      * \throws     unspecified  Can throw any exception thrown by
159      *      SelectionCollection::parseFromStdin().
160      * \throws     std::bad_alloc if out of memory.
161      *
162      * This method cooperates with SelectionOption to allow interactive
163      * input of requested selections after all options have been processed.
164      * It should be called after the Options::finish() method has been
165      * called on all options that add selections to this collection.
166      * For each required selection option that has not been given, as well
167      * as for optional selection options that have been specified without
168      * values, it will prompt the user to input the necessary selections.
169      */
170     void parseRequestedFromStdin(bool bInteractive);
171     /*! \brief
172      * Parses selection(s) from a file for options not yet provided.
173      *
174      * \param[in]  filename Name of the file to parse selections from.
175      * \throws     unspecified  Can throw any exception thrown by
176      *      SelectionCollection::parseFromFile().
177      * \throws     std::bad_alloc if out of memory.
178      * \throws     InvalidInputError if
179      *      - the number of selections in \p filename doesn't match the
180      *        number requested.
181      *      - any selection uses a feature that is not allowed for the
182      *        corresponding option.
183      *      - if there is a request for any number of selections that is
184      *        not the last (in which case it is not possible to determine
185      *        which selections belong to which request).
186      *
187      * This method behaves as parseRequestedFromStdin(), with two
188      * exceptions:
189      *  -# It reads the selections from a file instead of standard input.
190      *  -# If no requests are pending, assigns values to all required
191      *     options that have not yet been set.
192      *
193      * This method used to implement SelectionFileOption.
194      *
195      * \see parseRequestedFromStdin()
196      */
197     void parseRequestedFromFile(const std::string& filename);
198     /*! \brief
199      * Parses selection(s) from a string for options not yet provided.
200      *
201      * \param[in]  str     String to parse.
202      * \throws     unspecified  Can throw any exception thrown by
203      *      SelectionCollection::parseFromString().
204      * \throws     std::bad_alloc if out of memory.
205      * \throws     InvalidInputError in same conditions as
206      *      parseRequestedFromFile().
207      *
208      * This method behaves as parseRequestedFromFile(), but reads the
209      * selections from a string instead of a file.
210      * This method is mainly used for testing.
211      *
212      * \see parseRequestedFromFile()
213      */
214     void parseRequestedFromString(const std::string& str);
215
216 private:
217     class Impl;
218
219     PrivateImplPointer<Impl> impl_;
220
221     /*! \brief
222      * Needed for handling delayed selection parsing requests.
223      */
224     friend class SelectionOptionStorage;
225 };
226
227 } // namespace gmx
228
229 #endif