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 <boost/stl_interfaces/iterator_interface.hpp>
53 #include "gromacs/utility/classhelpers.h"
57 struct gmx_ana_selmethod_t;
62 class SelectionParserSymbolTable;
66 * Single symbol for the selection parser.
68 * Public methods in this class do not throw.
70 * \ingroup module_selection
72 class SelectionParserSymbol
75 //! Defines the type of the symbol.
78 ReservedSymbol, //!< The symbol is a reserved keyword.
79 VariableSymbol, //!< The symbol is a variable.
80 MethodSymbol, //!< The symbol is a selection method.
81 PositionSymbol //!< The symbol is a position keyword.
84 ~SelectionParserSymbol();
86 //! Returns the name of the symbol.
87 const std::string& name() const;
88 //! Returns the type of the symbol.
89 SymbolType type() const;
92 * Returns the method associated with a \ref MethodSymbol symbol.
94 * \returns The method associated with the symbol.
96 * Must only be called if type() returns \ref MethodSymbol.
98 gmx_ana_selmethod_t* methodValue() const;
100 * Returns the selection tree associated with a \ref VariableSymbol symbol.
102 * \returns The variable expression associated with the symbol.
104 * Must only be called if type() returns \ref VariableSymbol.
106 const SelectionTreeElementPointer& variableValue() const;
112 * Initializes a new symbol with the given data.
114 * \param impl Implementation data.
115 * \throws std::bad_alloc if out of memory.
117 * Only the parent symbol table creates symbol objects.
119 explicit SelectionParserSymbol(Impl* impl);
121 PrivateImplPointer<Impl> impl_;
124 * Needed to call the constructor and for other initialization.
126 friend class SelectionParserSymbolTable;
131 * Forward iterator for iterating symbols of a given type.
133 * Behaves as standard C++ forward iterator. To get an iterator, call
134 * SelectionParserSymbolTable::beginIterator(). Each time the iterator is
135 * incremented, it moves to the next symbol of the type given when the iterator
136 * was created. When there are no more symbols, the iterator will equal
137 * SelectionParserSymbolTable::endIterator(). It is not allowed to dereference
138 * or increment an iterator that has reached the end.
140 * Construction and assignment may throw std::bad_alloc if out of memory.
141 * Other methods do not throw.
143 * \see SelectionParserSymbolTable::beginIterator()
145 * \ingroup module_selection
147 class SelectionParserSymbolIterator :
148 public boost::stl_interfaces::iterator_interface<SelectionParserSymbolIterator, std::forward_iterator_tag, const SelectionParserSymbol>
151 boost::stl_interfaces::iterator_interface<SelectionParserSymbolIterator, std::forward_iterator_tag, const SelectionParserSymbol>;
154 //! Creates an independent copy of an iterator.
155 SelectionParserSymbolIterator(const SelectionParserSymbolIterator& other);
156 ~SelectionParserSymbolIterator();
158 //! Creates an independent copy of an iterator.
159 SelectionParserSymbolIterator& operator=(const SelectionParserSymbolIterator& other);
161 //! Equality comparison for iterators.
162 bool operator==(const SelectionParserSymbolIterator& other) const;
163 //! Dereferences the iterator.
164 reference operator*() const;
165 //! Moves the iterator to the next symbol.
166 SelectionParserSymbolIterator& operator++();
167 using Base:: operator++;
173 * Initializes a new iterator with the given data.
175 * \param impl Implementation data.
177 * Only the parent symbol table can create non-default-constructed
180 explicit SelectionParserSymbolIterator(Impl* impl);
182 PrivateImplPointer<Impl> impl_;
185 * Needed to access the constructor.
187 friend class SelectionParserSymbolTable;
191 * Symbol table for the selection parser.
193 * \ingroup module_selection
195 class SelectionParserSymbolTable
199 * Creates a new symbol table.
201 * \throws std::bad_alloc if out of memory.
203 * The created table is initialized with reserved and position symbols.
205 SelectionParserSymbolTable();
206 ~SelectionParserSymbolTable();
209 * Finds a symbol by name.
211 * \param[in] name Symbol name to find.
212 * \returns Pointer to the symbol with name \p name, or
217 const SelectionParserSymbol* findSymbol(const std::string& name) const;
220 * Returns the start iterator for iterating symbols of a given type.
222 * \param[in] type Type of symbols to iterate over.
223 * \returns Iterator that points to the first symbol of type \p type.
224 * \throws std::bad_alloc if out of memory.
226 * \see SelectionParserSymbolIterator
228 SelectionParserSymbolIterator beginIterator(SelectionParserSymbol::SymbolType type) const;
230 * Returns the end iterator for symbol iteration.
232 * \throws std::bad_alloc if out of memory.
234 * Currently, the end value is the same for all symbol types.
236 * \see SelectionParserSymbolIterator
238 SelectionParserSymbolIterator endIterator() const;
241 * Adds a new variable symbol.
243 * \param[in] name Name of the new symbol.
244 * \param[in] sel Value of the variable.
245 * \throws std::bad_alloc if out of memory.
246 * \throws InvalidInputError if there was a symbol with the same
249 void addVariable(const char* name, const SelectionTreeElementPointer& sel);
251 * Adds a new method symbol.
253 * \param[in] name Name of the new symbol.
254 * \param[in] method Method that this symbol represents.
255 * \throws std::bad_alloc if out of memory.
256 * \throws APIError if there was a symbol with the same name.
258 void addMethod(const char* name, gmx_ana_selmethod_t* method);
263 PrivateImplPointer<Impl> impl_;
266 * Needed to access implementation types.
268 friend class SelectionParserSymbolIterator;