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