Merge branch 'release-4-6' into master
[alexxy/gromacs.git] / src / gromacs / selection / parser_internal.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief Helper functions for the selection parser.
37  *
38  * This header is includes only from parser.cpp (generated from parser.y), and
39  * it includes functions and macros used internally by the parser.
40  * They are in a separate file to make then easier to edit (no need to
41  * regenerate the parser), and to keep parser.y as simple as possible.
42  *
43  * \author Teemu Murtola <teemu.murtola@gmail.com>
44  * \ingroup module_selection
45  */
46 #ifndef GMX_SELECTION_PARSER_INTERNAL_H
47 #define GMX_SELECTION_PARSER_INTERNAL_H
48
49 #include <exception>
50
51 #include <boost/scoped_ptr.hpp>
52
53 #include "gromacs/utility/gmxassert.h"
54
55 #include "parsetree.h"
56 #include "selelem.h"
57
58 #include "scanner.h"
59
60 //! Error handler needed by Bison.
61 static void
62 yyerror(yyscan_t scanner, char const *s)
63 {
64     _gmx_selparser_error(scanner, "%s", s);
65 }
66
67 /*! \name Exception handling macros for actions
68  *
69  * These macros should be used at the beginning and end of each semantic action
70  * that may throw an exception. For robustness, it's best to wrap all actions
71  * that call functions declared outside parser.y should be wrapped.
72  * These macros take care to catch any exceptions, store the exception (or
73  * handle it and allow the parser to continue), and terminate the parser
74  * cleanly if necessary.
75  * The code calling the parser should use
76  * _gmx_sel_lexer_rethrow_exception_if_occurred() to rethrow any exceptions.
77  * \{
78  */
79 //! Starts an action that may throw exceptions.
80 #define BEGIN_ACTION \
81     try {
82 //! Finishes an action that may throw exceptions.
83 #define END_ACTION \
84     } \
85     catch (const std::exception &ex) \
86     { \
87         if (_gmx_selparser_handle_exception(scanner, ex)) { \
88             YYERROR; } \
89         else{ \
90             YYABORT; } \
91     }
92 //!\}
93
94 #ifndef GMX_CXX11
95 //! No-op to enable use of same get()/set() implementation as with C++11.
96 static gmx::SelectionParserValue &move(gmx::SelectionParserValue &src)
97 {
98     return src;
99 }
100 //! No-op to enable use of same get()/set() implementation as with C++11.
101 static gmx::SelectionParserParameter &move(gmx::SelectionParserParameter &src)
102 {
103     return src;
104 }
105 #endif
106
107 /*! \brief
108  * Retrieves a semantic value.
109  *
110  * \param[in] src  Semantic value to get the value from.
111  * \returns   Retrieved value.
112  * \throws    unspecified  Any exception thrown by the move constructor of
113  *      ValueType (copy constructor if GMX_CXX11 is not set).
114  *
115  * There should be no statements that may throw exceptions in actions before
116  * this function has been called for all semantic values that have a C++ object
117  * stored.  Together with set(), this function abstracts away exception
118  * safety issues that arise from the use of a plain pointer for storing the
119  * semantic values.
120  *
121  * Does not throw for smart pointer types.  If used with types that may throw,
122  * the order of operations should be such that it is exception-safe.
123  */
124 template <typename ValueType> static
125 ValueType get(ValueType *src)
126 {
127     GMX_RELEASE_ASSERT(src != NULL, "Semantic value pointers should be non-NULL");
128     boost::scoped_ptr<ValueType> srcGuard(src);
129     return ValueType(move(*src));
130 }
131 /*! \brief
132  * Sets a semantic value.
133  *
134  * \tparam     ValueType Type of value to set.
135  * \param[out] dest  Semantic value to set (typically $$).
136  * \param[in]  value Value to put into the semantic value.
137  * \throws     std::bad_alloc if out of memory.
138  * \throws     unspecified  Any exception thrown by the move constructor of
139  *      ValueType (copy constructor if GMX_CXX11 is not set).
140  *
141  * This should be the last statement before ::END_ACTION, except for a
142  * possible ::CHECK_SEL.
143  */
144 template <typename ValueType> static
145 void set(ValueType * &dest, ValueType value)
146 {
147     dest = new ValueType(move(value));
148 }
149 /*! \brief
150  * Sets an empty semantic value.
151  *
152  * \tparam     ValueType Type of value to set (must be default constructible).
153  * \param[out] dest  Semantic value to set (typically $$).
154  * \throws     std::bad_alloc if out of memory.
155  * \throws     unspecified  Any exception thrown by the default constructor of
156  *      ValueType.
157  *
158  * This should be the last statement before ::END_ACTION, except for a
159  * possible ::CHECK_SEL.
160  */
161 template <typename ValueType> static
162 void set_empty(ValueType * &dest)
163 {
164     dest = new ValueType;
165 }
166 /*! \brief
167  * Checks that a valid tree was set.
168  *
169  * Should be called after set() if it was used to set a value where NULL
170  * pointer indicates an error.
171  *
172  * \todo
173  * Get rid of this macro.  It should now be possible to handle all errors using
174  * exceptions.
175  */
176 #define CHECK_SEL(sel) \
177     if (!*(sel)) { \
178         delete sel; \
179         YYERROR; \
180     }
181
182 #endif