2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013 by the GROMACS development team.
5 * Copyright (c) 2014,2015,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.
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.
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.
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.
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.
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.
37 * \brief Handling of selection parser symbol table.
39 * This is an implementation header: there should be no need to use it outside
42 * \author Teemu Murtola <teemu.murtola@gmail.com>
43 * \ingroup module_selection
45 #ifndef GMX_SELECTION_SYMREC_H
46 #define GMX_SELECTION_SYMREC_H
52 #include <boost/stl_interfaces/iterator_interface.hpp>
56 struct gmx_ana_selmethod_t;
61 class SelectionParserSymbolTable;
65 * Single symbol for the selection parser.
67 * Public methods in this class do not throw.
69 * \ingroup module_selection
71 class SelectionParserSymbol
74 //! Defines the type of the symbol.
77 ReservedSymbol, //!< The symbol is a reserved keyword.
78 VariableSymbol, //!< The symbol is a variable.
79 MethodSymbol, //!< The symbol is a selection method.
80 PositionSymbol //!< The symbol is a position keyword.
83 ~SelectionParserSymbol();
85 //! Returns the name of the symbol.
86 const std::string& name() const;
87 //! Returns the type of the symbol.
88 SymbolType type() const;
91 * Returns the method associated with a \ref MethodSymbol symbol.
93 * \returns The method associated with the symbol.
95 * Must only be called if type() returns \ref MethodSymbol.
97 gmx_ana_selmethod_t* methodValue() const;
99 * Returns the selection tree associated with a \ref VariableSymbol symbol.
101 * \returns The variable expression associated with the symbol.
103 * Must only be called if type() returns \ref VariableSymbol.
105 const SelectionTreeElementPointer& variableValue() const;
111 * Initializes a new symbol with the given data.
113 * \param impl Implementation data.
114 * \throws std::bad_alloc if out of memory.
116 * Only the parent symbol table creates symbol objects.
118 explicit SelectionParserSymbol(Impl* impl);
120 std::unique_ptr<Impl> impl_;
123 * Needed to call the constructor and for other initialization.
125 friend class SelectionParserSymbolTable;
130 * Forward iterator for iterating symbols of a given type.
132 * Behaves as standard C++ forward iterator. To get an iterator, call
133 * SelectionParserSymbolTable::beginIterator(). Each time the iterator is
134 * incremented, it moves to the next symbol of the type given when the iterator
135 * was created. When there are no more symbols, the iterator will equal
136 * SelectionParserSymbolTable::endIterator(). It is not allowed to dereference
137 * or increment an iterator that has reached the end.
139 * Construction and assignment may throw std::bad_alloc if out of memory.
140 * Other methods do not throw.
142 * \see SelectionParserSymbolTable::beginIterator()
144 * \ingroup module_selection
146 class SelectionParserSymbolIterator :
147 public boost::stl_interfaces::iterator_interface<SelectionParserSymbolIterator, std::forward_iterator_tag, const SelectionParserSymbol>
150 boost::stl_interfaces::iterator_interface<SelectionParserSymbolIterator, std::forward_iterator_tag, const SelectionParserSymbol>;
153 //! Creates an independent copy of an iterator.
154 SelectionParserSymbolIterator(const SelectionParserSymbolIterator& other);
155 ~SelectionParserSymbolIterator();
157 //! Creates an independent copy of an iterator.
158 SelectionParserSymbolIterator& operator=(const SelectionParserSymbolIterator& other);
160 //! Equality comparison for iterators.
161 bool operator==(const SelectionParserSymbolIterator& other) const;
162 //! Dereferences the iterator.
163 reference operator*() const;
164 //! Moves the iterator to the next symbol.
165 SelectionParserSymbolIterator& operator++();
166 using Base:: operator++;
172 * Initializes a new iterator with the given data.
174 * \param impl Implementation data.
176 * Only the parent symbol table can create non-default-constructed
179 explicit SelectionParserSymbolIterator(Impl* impl);
181 std::unique_ptr<Impl> impl_;
184 * Needed to access the constructor.
186 friend class SelectionParserSymbolTable;
190 * Symbol table for the selection parser.
192 * \ingroup module_selection
194 class SelectionParserSymbolTable
198 * Creates a new symbol table.
200 * \throws std::bad_alloc if out of memory.
202 * The created table is initialized with reserved and position symbols.
204 SelectionParserSymbolTable();
205 ~SelectionParserSymbolTable();
208 * Finds a symbol by name.
210 * \param[in] name Symbol name to find.
211 * \returns Pointer to the symbol with name \p name, or
216 const SelectionParserSymbol* findSymbol(const std::string& name) const;
219 * Returns the start iterator for iterating symbols of a given type.
221 * \param[in] type Type of symbols to iterate over.
222 * \returns Iterator that points to the first symbol of type \p type.
223 * \throws std::bad_alloc if out of memory.
225 * \see SelectionParserSymbolIterator
227 SelectionParserSymbolIterator beginIterator(SelectionParserSymbol::SymbolType type) const;
229 * Returns the end iterator for symbol iteration.
231 * \throws std::bad_alloc if out of memory.
233 * Currently, the end value is the same for all symbol types.
235 * \see SelectionParserSymbolIterator
237 SelectionParserSymbolIterator endIterator() const;
240 * Adds a new variable symbol.
242 * \param[in] name Name of the new symbol.
243 * \param[in] sel Value of the variable.
244 * \throws std::bad_alloc if out of memory.
245 * \throws InvalidInputError if there was a symbol with the same
248 void addVariable(const char* name, const SelectionTreeElementPointer& sel);
250 * Adds a new method symbol.
252 * \param[in] name Name of the new symbol.
253 * \param[in] method Method that this symbol represents.
254 * \throws std::bad_alloc if out of memory.
255 * \throws APIError if there was a symbol with the same name.
257 void addMethod(const char* name, gmx_ana_selmethod_t* method);
262 std::unique_ptr<Impl> impl_;
265 * Needed to access implementation types.
267 friend class SelectionParserSymbolIterator;