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, 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
51 #include "gromacs/utility/classhelpers.h"
55 struct gmx_ana_selmethod_t;
60 class SelectionParserSymbolTable;
64 * Single symbol for the selection parser.
66 * Public methods in this class do not throw.
68 * \ingroup module_selection
70 class SelectionParserSymbol
73 //! Defines the type of the symbol.
76 ReservedSymbol, //!< The symbol is a reserved keyword.
77 VariableSymbol, //!< The symbol is a variable.
78 MethodSymbol, //!< The symbol is a selection method.
79 PositionSymbol //!< The symbol is a position keyword.
82 ~SelectionParserSymbol();
84 //! Returns the name of the symbol.
85 const std::string& name() const;
86 //! Returns the type of the symbol.
87 SymbolType type() const;
90 * Returns the method associated with a \ref MethodSymbol symbol.
92 * \returns The method associated with the symbol.
94 * Must only be called if type() returns \ref MethodSymbol.
96 gmx_ana_selmethod_t* methodValue() const;
98 * Returns the selection tree associated with a \ref VariableSymbol symbol.
100 * \returns The variable expression associated with the symbol.
102 * Must only be called if type() returns \ref VariableSymbol.
104 const SelectionTreeElementPointer& variableValue() const;
110 * Initializes a new symbol with the given data.
112 * \param impl Implementation data.
113 * \throws std::bad_alloc if out of memory.
115 * Only the parent symbol table creates symbol objects.
117 explicit SelectionParserSymbol(Impl* impl);
119 PrivateImplPointer<Impl> impl_;
122 * Needed to call the constructor and for other initialization.
124 friend class SelectionParserSymbolTable;
129 * Input iterator for iterating symbols of a given type.
131 * Behaves as standard C++ input iterator. To get an iterator, call
132 * SelectionParserSymbolTable::beginIterator(). Each time the iterator is
133 * incremented, it moves to the next symbol of the type given when the iterator
134 * was created. When there are no more symbols, the iterator will equal
135 * SelectionParserSymbolTable::endIterator(). It is not allowed to dereference
136 * or increment an iterator that has reached the end.
138 * Construction and assignment may throw std::bad_alloc if out of memory.
139 * Other methods do not throw.
141 * \see SelectionParserSymbolTable::beginIterator()
143 * \ingroup module_selection
145 class SelectionParserSymbolIterator
148 /*! \name Iterator type traits
149 * Satisfies the requirements for STL input iterator.
152 using iterator_category = std::input_iterator_tag;
153 using value_type = const SelectionParserSymbol;
154 using difference_type = std::ptrdiff_t;
155 using pointer = const SelectionParserSymbol*;
156 using reference = const SelectionParserSymbol&;
159 //! Creates an independent copy of an iterator.
160 SelectionParserSymbolIterator(const SelectionParserSymbolIterator& other);
161 ~SelectionParserSymbolIterator();
163 //! Creates an independent copy of an iterator.
164 SelectionParserSymbolIterator& operator=(const SelectionParserSymbolIterator& other);
166 //! Equality comparison for iterators.
167 bool operator==(const SelectionParserSymbolIterator& other) const;
168 //! Inequality comparison for iterators.
169 bool operator!=(const SelectionParserSymbolIterator& other) const { return !operator==(other); }
170 //! Dereferences the iterator.
171 reference operator*() const;
172 //! Dereferences the iterator.
173 pointer operator->() const { return &operator*(); }
174 //! Moves the iterator to the next symbol.
175 SelectionParserSymbolIterator& operator++();
176 //! Moves the iterator to the next symbol.
177 SelectionParserSymbolIterator operator++(int)
179 SelectionParserSymbolIterator tmp(*this);
188 * Initializes a new iterator with the given data.
190 * \param impl Implementation data.
192 * Only the parent symbol table can create non-default-constructed
195 explicit SelectionParserSymbolIterator(Impl* impl);
197 PrivateImplPointer<Impl> impl_;
200 * Needed to access the constructor.
202 friend class SelectionParserSymbolTable;
206 * Symbol table for the selection parser.
208 * \ingroup module_selection
210 class SelectionParserSymbolTable
214 * Creates a new symbol table.
216 * \throws std::bad_alloc if out of memory.
218 * The created table is initialized with reserved and position symbols.
220 SelectionParserSymbolTable();
221 ~SelectionParserSymbolTable();
224 * Finds a symbol by name.
226 * \param[in] name Symbol name to find.
227 * \returns Pointer to the symbol with name \p name, or
232 const SelectionParserSymbol* findSymbol(const std::string& name) const;
235 * Returns the start iterator for iterating symbols of a given type.
237 * \param[in] type Type of symbols to iterate over.
238 * \returns Iterator that points to the first symbol of type \p type.
239 * \throws std::bad_alloc if out of memory.
241 * \see SelectionParserSymbolIterator
243 SelectionParserSymbolIterator beginIterator(SelectionParserSymbol::SymbolType type) const;
245 * Returns the end iterator for symbol iteration.
247 * \throws std::bad_alloc if out of memory.
249 * Currently, the end value is the same for all symbol types.
251 * \see SelectionParserSymbolIterator
253 SelectionParserSymbolIterator endIterator() const;
256 * Adds a new variable symbol.
258 * \param[in] name Name of the new symbol.
259 * \param[in] sel Value of the variable.
260 * \throws std::bad_alloc if out of memory.
261 * \throws InvalidInputError if there was a symbol with the same
264 void addVariable(const char* name, const SelectionTreeElementPointer& sel);
266 * Adds a new method symbol.
268 * \param[in] name Name of the new symbol.
269 * \param[in] method Method that this symbol represents.
270 * \throws std::bad_alloc if out of memory.
271 * \throws APIError if there was a symbol with the same name.
273 void addMethod(const char* name, gmx_ana_selmethod_t* method);
278 PrivateImplPointer<Impl> impl_;
281 * Needed to access implementation types.
283 friend class SelectionParserSymbolIterator;