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