/*
+ * This file is part of the GROMACS molecular simulation package.
*
- * This source code is part of
+ * Copyright (c) 2012,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
*
- * G R O M A C S
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
*
- * GROningen MAchine for Chemical Simulations
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
*
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
*
* To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
*/
/*! \internal \file
* \brief Helper functions for the selection parser.
* They are in a separate file to make then easier to edit (no need to
* regenerate the parser), and to keep parser.y as simple as possible.
*
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
* \ingroup module_selection
*/
#ifndef GMX_SELECTION_PARSER_INTERNAL_H
#define GMX_SELECTION_PARSER_INTERNAL_H
-#include <exception>
-
-#include "parsetree.h"
-#include "selelem.h"
-
-#include "scanner.h"
-
-//! Helper method to reorder a list of parameter values and to count the values.
-static t_selexpr_value *
-process_value_list(t_selexpr_value *values, int *nr)
-{
- t_selexpr_value *val, *pval, *nval;
-
- /* Count values (if needed) and reverse list */
- if (nr)
- {
- *nr = 0;
- }
- pval = NULL;
- val = values;
- while (val)
- {
- if (nr)
- {
- ++*nr;
- }
- nval = val->next;
- val->next = pval;
- pval = val;
- val = nval;
- }
- values = pval;
+#include "config.h"
- return values;
-}
+#include <exception>
-//! Helper method to reorder a list of parameters.
-static t_selexpr_param *
-process_param_list(t_selexpr_param *params)
-{
- t_selexpr_param *par, *ppar, *npar;
+#include <boost/scoped_ptr.hpp>
- /* Reverse list */
- ppar = NULL;
- par = params;
- while (par)
- {
- npar = par->next;
- par->next = ppar;
- ppar = par;
- par = npar;
- }
- params = ppar;
+#include "gromacs/utility/gmxassert.h"
- return params;
-}
+#include "parsetree.h"
+#include "scanner.h"
+#include "selelem.h"
//! Error handler needed by Bison.
static void
//! Finishes an action that may throw exceptions.
#define END_ACTION \
} \
- catch(const std::exception &ex) \
+ catch (const std::exception &ex) \
{ \
- if (_gmx_selparser_handle_exception(scanner, ex)) \
- YYERROR; \
- else \
- YYABORT; \
+ if (_gmx_selparser_handle_exception(scanner, ex)) { \
+ YYERROR; } \
+ else{ \
+ YYABORT; } \
}
//!\}
+#ifndef GMX_CXX11
+//! No-op to enable use of same get()/set() implementation as with C++11.
+static gmx::SelectionParserValue &move(gmx::SelectionParserValue &src)
+{
+ return src;
+}
+//! No-op to enable use of same get()/set() implementation as with C++11.
+static gmx::SelectionParserParameter &move(gmx::SelectionParserParameter &src)
+{
+ return src;
+}
+#endif
+
/*! \brief
- * Retrieves a selection tree pointer from a semantic value.
+ * Retrieves a semantic value.
*
- * \param[in] src Semantic value to get the tree from.
- * \returns Pointer to the selection tree.
+ * \param[in] src Semantic value to get the value from.
+ * \returns Retrieved value.
+ * \throws unspecified Any exception thrown by the move constructor of
+ * ValueType (copy constructor if GMX_CXX11 is not set).
*
* There should be no statements that may throw exceptions in actions before
- * this function has been called for all semantic values that have a tree
- * argument. Together with set(), this function abstracts away exception
+ * this function has been called for all semantic values that have a C++ object
+ * stored. Together with set(), this function abstracts away exception
* safety issues that arise from the use of a plain pointer for storing the
- * selection tree semantic values.
+ * semantic values.
*
- * Does not throw.
+ * Does not throw for smart pointer types. If used with types that may throw,
+ * the order of operations should be such that it is exception-safe.
*/
-static gmx::SelectionTreeElementPointer
-get(gmx::SelectionTreeElementPointer *src)
+template <typename ValueType> static
+ValueType get(ValueType *src)
{
- gmx::SelectionTreeElementPointer result;
- if (src != NULL)
- {
- result.swap(*src);
- delete src;
- }
- return result;
+ GMX_RELEASE_ASSERT(src != NULL, "Semantic value pointers should be non-NULL");
+ boost::scoped_ptr<ValueType> srcGuard(src);
+ return ValueType(move(*src));
}
/*! \brief
- * Sets a selection tree pointer to a semantic value.
+ * Sets a semantic value.
*
- * \param[out] dest Semantic value to set the tree to.
- * \param[in] value Pointer to the selection tree to set.
+ * \tparam ValueType Type of value to set.
+ * \param[out] dest Semantic value to set (typically $$).
+ * \param[in] value Value to put into the semantic value.
* \throws std::bad_alloc if out of memory.
+ * \throws unspecified Any exception thrown by the move constructor of
+ * ValueType (copy constructor if GMX_CXX11 is not set).
*
* This should be the last statement before ::END_ACTION, except for a
* possible ::CHECK_SEL.
*/
-static void
-set(gmx::SelectionTreeElementPointer *&dest,
- const gmx::SelectionTreeElementPointer &value)
+template <typename ValueType> static
+void set(ValueType * &dest, ValueType value)
+{
+ dest = new ValueType(move(value));
+}
+/*! \brief
+ * Sets an empty semantic value.
+ *
+ * \tparam ValueType Type of value to set (must be default constructible).
+ * \param[out] dest Semantic value to set (typically $$).
+ * \throws std::bad_alloc if out of memory.
+ * \throws unspecified Any exception thrown by the default constructor of
+ * ValueType.
+ *
+ * This should be the last statement before ::END_ACTION, except for a
+ * possible ::CHECK_SEL.
+ */
+template <typename ValueType> static
+void set_empty(ValueType * &dest)
{
- dest = new gmx::SelectionTreeElementPointer(value);
+ dest = new ValueType;
}
/*! \brief
* Checks that a valid tree was set.