3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements gmx::SelectionOptionManager.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
38 #include "selectionoptionmanager.h"
42 #include "gromacs/selection/selection.h"
43 #include "gromacs/selection/selectioncollection.h"
44 #include "gromacs/utility/exceptions.h"
45 #include "gromacs/utility/format.h"
47 #include "selectionoptionstorage.h"
52 /********************************************************************
53 * SelectionOptionManager::Impl
57 * Private implemention class for SelectionOptionManager.
59 * \ingroup module_selection
61 class SelectionOptionManager::Impl
65 * Request for postponed parsing of selections.
67 struct SelectionRequest
69 //! Initializes a request for the given option.
70 explicit SelectionRequest(SelectionOptionStorage *storage)
75 //! Returns name of the requested selection optin.
76 const std::string &name() const
78 return storage_->name();
80 //! Returns description for the requested selection option.
81 const std::string &description() const
83 return storage_->description();
86 * Returns the number of selections requested in this request.
88 * -1 indicates no upper limit.
92 return storage_->maxValueCount();
95 //! Storage object to which the selections will be added.
96 SelectionOptionStorage *storage_;
99 //! Collection for a list of selection requests.
100 typedef std::vector<SelectionRequest> RequestList;
103 * Helper class that clears a request list on scope exit.
105 * Methods in this class do not throw.
107 class RequestsClearer
110 //! Constructs an object that clears given list on scope exit.
111 explicit RequestsClearer(RequestList *requests)
112 : requests_(requests)
115 //! Clears the request list given to the constructor.
122 RequestList *requests_;
126 * Creates a new selection collection.
128 * \throws std::bad_alloc if out of memory.
130 explicit Impl(SelectionCollection *collection);
133 * Assign selections from a list to pending requests.
135 * \param[in] selections List of selections to assign.
136 * \throws std::bad_alloc if out of memory.
137 * \throws InvalidInputError if the assignment cannot be done
138 * (see parseRequestedFromFile() for documented conditions).
140 * Loops through \p selections and the pending requests lists in order,
141 * and for each requests, assigns the first yet unassigned selections
144 void placeSelectionsInRequests(const SelectionList &selections);
146 //! Selection collection to which selections are stored.
147 SelectionCollection &collection_;
148 //! List of selections requested for later parsing.
149 RequestList requests_;
152 SelectionOptionManager::Impl::Impl(SelectionCollection *collection)
153 : collection_(*collection)
157 void SelectionOptionManager::Impl::placeSelectionsInRequests(
158 const SelectionList &selections)
160 RequestsClearer clearRequestsOnExit(&requests_);
162 SelectionList::const_iterator first = selections.begin();
163 SelectionList::const_iterator last = first;
164 RequestList::const_iterator i;
165 // TODO: Improve error messages.
166 for (i = requests_.begin(); i != requests_.end(); ++i)
168 const SelectionRequest &request = *i;
169 if (request.count() > 0)
171 if (selections.end() - first < request.count())
173 GMX_THROW(InvalidInputError("Too few selections provided"));
175 last = first + request.count();
179 if (i != requests_.end() - 1)
181 GMX_THROW(InvalidInputError(
182 formatString("Request for selection '%s' must "
183 "not be followed by others",
184 request.name().c_str())));
186 last = selections.end();
188 SelectionList curr(first, last);
189 request.storage_->addSelections(curr, true);
192 if (last != selections.end())
194 GMX_THROW(InvalidInputError("Too many selections provided"));
199 /********************************************************************
200 * SelectionOptionManager
203 SelectionOptionManager::SelectionOptionManager(SelectionCollection *collection)
204 : impl_(new Impl(collection))
208 SelectionOptionManager::~SelectionOptionManager()
212 SelectionCollection &
213 SelectionOptionManager::selectionCollection()
215 return impl_->collection_;
219 SelectionOptionManager::requestDelayedParsing(SelectionOptionStorage *storage)
221 impl_->requests_.push_back(Impl::SelectionRequest(storage));
225 SelectionOptionManager::parseRequestedFromStdin(bool bInteractive)
227 Impl::RequestsClearer clearRequestsOnExit(&impl_->requests_);
229 Impl::RequestList::const_iterator i;
230 for (i = impl_->requests_.begin(); i != impl_->requests_.end(); ++i)
232 const Impl::SelectionRequest &request = *i;
235 std::fprintf(stderr, "\nSpecify ");
236 if (request.count() < 0)
238 std::fprintf(stderr, "any number of selections");
240 else if (request.count() == 1)
242 std::fprintf(stderr, "a selection");
246 std::fprintf(stderr, "%d selections", request.count());
248 std::fprintf(stderr, " for option '%s' (%s):\n",
249 request.name().c_str(), request.description().c_str());
250 std::fprintf(stderr, "(one selection per line, 'help' for help%s)\n",
251 request.count() < 0 ? ", Ctrl-D to end" : "");
253 SelectionList selections;
254 impl_->collection_.parseFromStdin(request.count(), bInteractive, &selections);
255 request.storage_->addSelections(selections, true);
260 SelectionOptionManager::parseRequestedFromFile(const std::string &filename)
262 SelectionList selections;
263 impl_->collection_.parseFromFile(filename, &selections);
264 impl_->placeSelectionsInRequests(selections);
268 SelectionOptionManager::parseRequestedFromString(const std::string &str)
270 SelectionList selections;
271 impl_->collection_.parseFromString(str, &selections);
272 impl_->placeSelectionsInRequests(selections);