/*
+ * This file is part of the GROMACS molecular simulation package.
*
- * This source code is part of
+ * Copyright (c) 2009,2010,2011,2012,2013,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
* Implements functions in selparam.h.
*
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
* \ingroup module_selection
*/
+#include "gmxpre.h"
+
#include <algorithm>
#include <string>
-#include "gromacs/legacyheaders/smalloc.h"
-#include "gromacs/legacyheaders/string2.h"
-#include "gromacs/legacyheaders/vec.h"
-
+#include "gromacs/math/vec.h"
#include "gromacs/selection/position.h"
-#include "gromacs/selection/selmethod.h"
-#include "gromacs/selection/selparam.h"
-#include "gromacs/utility/errorcodes.h"
+#include "gromacs/utility/cstringutil.h"
#include "gromacs/utility/exceptions.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/messagestringcollector.h"
+#include "gromacs/utility/smalloc.h"
#include "gromacs/utility/stringutil.h"
#include "parsetree.h"
-#include "position.h"
#include "scanner.h"
#include "selelem.h"
+#include "selmethod.h"
+#include "selparam.h"
using gmx::SelectionParserValue;
using gmx::SelectionParserValueList;
{
return (i == 0) ? NULL : ¶m[i-1];
}
- for ( ; i < nparam; ++i)
+ for (; i < nparam; ++i)
{
if (!strcmp(param[i].name, name))
{
static void
place_child(const SelectionTreeElementPointer &root,
const SelectionTreeElementPointer &child,
- gmx_ana_selparam_t *param)
+ gmx_ana_selparam_t *param)
{
gmx_ana_selparam_t *ps;
int n;
/*! \brief
* Comparison function for sorting integer ranges.
- *
+ *
* \param[in] a Pointer to the first range.
* \param[in] b Pointer to the second range.
* \returns -1, 0, or 1 depending on the relative order of \p a and \p b.
/*! \brief
* Parses the values for a parameter that takes integer or real ranges.
- *
+ *
* \param[in] values List of values.
* \param param Parameter to parse.
* \param[in] scanner Scanner data structure.
int i, j, n;
param->flags &= ~SPAR_DYNAMIC;
- if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range parameter type");
- return false;
- }
+ GMX_RELEASE_ASSERT(param->val.type == INT_VALUE || param->val.type == REAL_VALUE,
+ "Invalid range parameter type");
idata = NULL;
rdata = NULL;
if (param->val.type == INT_VALUE)
_gmx_selparser_error(scanner, "expressions not supported within range parameters");
return false;
}
- if (value->type != param->val.type)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid range value type");
- return false;
- }
+ GMX_RELEASE_ASSERT(value->type == param->val.type,
+ "Invalid range value type (should have been caught earlier)");
if (param->val.type == INT_VALUE)
{
int i1 = std::min(value->u.i.i1, value->u.i.i2);
{
idata[j] = idata[i];
idata[j+1] = idata[i+1];
- j += 2;
+ j += 2;
}
}
}
{
rdata[j] = rdata[i];
rdata[j+1] = rdata[i+1];
- j += 2;
+ j += 2;
}
}
}
/*! \brief
* Parses the values for a parameter that takes a variable number of values.
- *
+ *
* \param[in] values List of values.
* \param param Parameter to parse.
* \param root Selection element to which child expressions are added.
* is stored, each as a separate value.
*/
static bool
-parse_values_varnum(const SelectionParserValueList &values,
- gmx_ana_selparam_t *param,
+parse_values_varnum(const SelectionParserValueList &values,
+ gmx_ana_selparam_t *param,
const SelectionTreeElementPointer &root,
- void *scanner)
+ void *scanner)
{
int i, j;
if (param->val.type != INT_VALUE && param->val.type != REAL_VALUE
&& param->val.type != STR_VALUE && param->val.type != POS_VALUE)
{
- GMX_ERROR_NORET(gmx::eeInternalError,
- "Variable-count value type not implemented");
- return false;
+ GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
}
/* Reserve appropriate amount of memory */
if (param->val.type == POS_VALUE)
{
gmx_ana_pos_reserve(param->val.u.p, valueCount, 0);
- gmx_ana_pos_set_nr(param->val.u.p, valueCount);
gmx_ana_indexmap_init(¶m->val.u.p->m, NULL, NULL, INDEX_UNKNOWN);
+ gmx_ana_pos_set_nr(param->val.u.p, valueCount);
}
else
{
_gmx_selparser_error(scanner, "expressions not supported within value lists");
return false;
}
- if (value->type != param->val.type)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
- return false;
- }
+ GMX_RELEASE_ASSERT(value->type == param->val.type,
+ "Invalid value type (should have been caught earlier)");
switch (param->val.type)
{
case INT_VALUE:
param->val.u.r[i++] = value->u.r.r1;
break;
case STR_VALUE:
- param->val.u.s[i++] = strdup(value->stringValue().c_str());
+ param->val.u.s[i++] = gmx_strdup(value->stringValue().c_str());
break;
case POS_VALUE: copy_rvec(value->u.x, param->val.u.p->x[i++]); break;
default: /* Should not be reached */
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
- return false;
+ GMX_THROW(gmx::InternalError("Variable-count value type not implemented"));
}
}
param->val.nr = i;
child->setName(param->name);
child->flags &= ~SEL_ALLOCVAL;
child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
- child->v.nr = param->val.nr;
+ child->v.nr = param->val.nr;
_gmx_selvalue_setstore(&child->v, param->val.u.s);
/* Because the child is not group-valued, the u union is not used
* for anything, so we can abuse it by storing the parameter value
child->child = expr;
}
/* Setup the child element */
- child->flags &= ~SEL_ALLOCVAL;
+ child->flags &= ~SEL_ALLOCVAL;
child->u.param = param;
if (child->v.type != param->val.type)
{
// FIXME: Use exceptions.
return SelectionTreeElementPointer();
}
- _gmx_selelem_update_flags(child, scanner);
+ _gmx_selelem_update_flags(child);
if ((child->flags & SEL_DYNAMIC) && !(param->flags & SPAR_DYNAMIC))
{
_gmx_selparser_error(scanner, "dynamic values not supported");
/*! \brief
* Parses an expression value for a parameter that takes a variable number of values.
- *
+ *
* \param[in] values List of values.
* \param param Parameter to parse.
* \param root Selection element to which child expressions are added.
* \returns true if the values were parsed successfully, false otherwise.
*/
static bool
-parse_values_varnum_expr(const SelectionParserValueList &values,
- gmx_ana_selparam_t *param,
+parse_values_varnum_expr(const SelectionParserValueList &values,
+ gmx_ana_selparam_t *param,
const SelectionTreeElementPointer &root,
- void *scanner)
+ void *scanner)
{
- if (values.size() != 1 || !values.front().hasExpressionValue())
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid expression value");
- return false;
- }
+ GMX_RELEASE_ASSERT(values.size() == 1 && values.front().hasExpressionValue(),
+ "Called with an invalid type of value");
SelectionTreeElementPointer child
= add_child(root, param, values.front().expr, scanner);
case POS_VALUE: sel->v.u.p = ¶m->val.u.p[i]; break;
case GROUP_VALUE: sel->v.u.g = ¶m->val.u.g[i]; break;
default: /* Error */
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid value type");
- return false;
+ GMX_THROW(gmx::InternalError("Invalid value type"));
}
- sel->v.nr = 1;
+ sel->v.nr = 1;
sel->v.nalloc = -1;
return true;
}
param->flags &= ~SPAR_DYNAMIC;
}
- i = 0;
+ i = 0;
bDynamic = false;
SelectionParserValueList::const_iterator value;
for (value = values.begin(); value != values.end() && i < param->val.nr; ++value)
param->val.u.r[i] = value->u.r.r1;
break;
case STR_VALUE:
- param->val.u.s[i] = strdup(value->stringValue().c_str());
+ param->val.u.s[i] = gmx_strdup(value->stringValue().c_str());
break;
case POS_VALUE:
gmx_ana_pos_init_const(¶m->val.u.p[i], value->u.x);
break;
case NO_VALUE:
case GROUP_VALUE:
- GMX_ERROR_NORET(gmx::eeInternalError,
- "Invalid non-expression value");
- return false;
+ GMX_THROW(gmx::InternalError("Invalid non-expression value type"));
}
}
++i;
const SelectionParserValueList &values,
gmx_ana_selparam_t *param, void *scanner)
{
- bool bSetNo;
-
- if (param->val.type != NO_VALUE)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid boolean parameter");
- return false;
- }
+ GMX_ASSERT(param->val.type == NO_VALUE,
+ "Boolean parser called for non-boolean parameter");
if (values.size() > 1 || (!values.empty() && values.front().type != INT_VALUE))
{
_gmx_selparser_error(scanner, "parameter takes only a yes/no/on/off/0/1 value");
return false;
}
- bSetNo = false;
+ bool bSetNo = false;
/* Check if the parameter name is given with a 'no' prefix */
if (name.length() > 2 && name[0] == 'n' && name[1] == 'o'
&& name.compare(2, name.length() - 2, param->name) == 0)
*/
static bool
parse_values_enum(const SelectionParserValueList &values,
- gmx_ana_selparam_t *param,
- void *scanner)
+ gmx_ana_selparam_t *param,
+ void *scanner)
{
+ GMX_ASSERT(param->val.type == STR_VALUE,
+ "Enum parser called for non-string parameter");
if (values.size() != 1)
{
_gmx_selparser_error(scanner, "a single value is required");
return false;
}
const SelectionParserValue &value = values.front();
- if (value.type != STR_VALUE || param->val.type != STR_VALUE)
- {
- GMX_ERROR_NORET(gmx::eeInternalError, "Invalid enum parameter");
- return false;
- }
+ GMX_RELEASE_ASSERT(value.type == param->val.type,
+ "Invalid value type (should have been caught earlier)");
if (value.hasExpressionValue())
{
_gmx_selparser_error(scanner, "expression value for enumerated parameter not supported");
}
const std::string &svalue = value.stringValue();
- int i = 1;
- int match = 0;
+ int i = 1;
+ int match = 0;
while (param->val.u.s[i] != NULL)
{
if (gmx::startsWith(param->val.u.s[i], svalue))
*value = SelectionParserValue::createPosition(expr->v.u.p->x[0]);
break;
default:
- GMX_ERROR_NORET(gmx::eeInternalError,
- "Unsupported value type");
- break;
+ GMX_THROW(gmx::InternalError(
+ "Unsupported constant expression value type"));
}
}
}
* have been processed, no matter is there was an error or not.
*/
bool
-_gmx_sel_parse_params(const SelectionParserParameterList &pparams,
+_gmx_sel_parse_params(const gmx::SelectionParserParameterList &pparams,
int nparam, gmx_ana_selparam_t *params,
- const SelectionTreeElementPointer &root, void *scanner)
+ const gmx::SelectionTreeElementPointer &root,
+ void *scanner)
{
gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
- gmx_ana_selparam_t *oparam;
- bool bOk, rc;
- int i;
+ gmx_ana_selparam_t *oparam;
+ bool bOk, rc;
+ int i;
/* Check that the value pointers of SPAR_VARNUM parameters are NULL and
* that they are not NULL for other parameters */
bOk = true;
for (i = 0; i < nparam; ++i)
{
- std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
+ std::string contextStr = gmx::formatString("In parameter '%s'", params[i].name);
gmx::MessageStringContext context(errors, contextStr);
if (params[i].val.type != POS_VALUE && (params[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
{
if (!pparam->name().empty())
{
contextStr = gmx::formatString("In parameter '%s'", pparam->name().c_str());
- i = -1;
- oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
+ i = -1;
+ oparam = gmx_ana_selparam_find(pparam->name().c_str(), nparam, params);
}
else if (i >= 0)
{
contextStr = gmx::formatString("In value %d", i + 1);
- oparam = ¶ms[i];
+ oparam = ¶ms[i];
if (oparam->name != NULL)
{
oparam = NULL;