* \param[in] i Running number for the subexpression.
*
* The name of the selection becomes "SubExpr N", where N is \p i;
- * Memory is allocated for the name and the name is stored both in
- * gmx::SelectionTreeElement::name and gmx::SelectionTreeElement::u::cgrp::name; the latter
- * is freed by _gmx_selelem_free().
+ * Memory is allocated for the name and the name is stored both as the
+ * name of the subexpression element and as
+ * gmx::SelectionTreeElement::u::cgrp::name; the latter is freed by
+ * _gmx_selelem_free().
*/
static void
create_subexpression_name(const SelectionTreeElementPointer &sel, int i)
{
- char *name = strdup(gmx::formatString("SubExpr %d", i).c_str());
-
- sel->name = name;
- sel->u.cgrp.name = name;
+ std::string name(gmx::formatString("SubExpr %d", i));
+ sel->setName(name);
+ sel->u.cgrp.name = strdup(name.c_str());
}
/*! \brief
{
subexpr = subexpr->next;
}
- /* The latter check excludes variable references.
- * It also excludes subexpression elements that have already been
- * processed, because they are given a name when they are first
- * encountered.
- * TODO: There should be a more robust mechanism (probably a dedicated
- * flag) for detecting parser-generated subexpressions than relying on
- * a NULL name field. Additional TODO: This mechanism doesn't seem to
- * be currently used. */
- if (child->type == SEL_SUBEXPRREF && (child->child->type != SEL_SUBEXPR
- || child->child->name == NULL))
+ /* The latter check excludes variable references. */
+ if (child->type == SEL_SUBEXPRREF && child->child->type != SEL_SUBEXPR)
{
/* Create the root element for the subexpression */
if (!root)
subexpr->next.reset(new SelectionTreeElement(SEL_ROOT));
subexpr = subexpr->next;
}
- /* Create the subexpression element and/or
+ /* Create the subexpression element and
* move the actual subexpression under the created element. */
- if (child->child->type != SEL_SUBEXPR)
- {
- subexpr->child.reset(new SelectionTreeElement(SEL_SUBEXPR));
- _gmx_selelem_set_vtype(subexpr->child, child->v.type);
- subexpr->child->child = child->child;
- child->child = subexpr->child;
- }
- else
- {
- subexpr->child = child->child;
- }
+ subexpr->child.reset(new SelectionTreeElement(SEL_SUBEXPR));
+ _gmx_selelem_set_vtype(subexpr->child, child->v.type);
+ subexpr->child->child = child->child;
+ child->child = subexpr->child;
create_subexpression_name(subexpr->child, ++*subexprn);
/* Set the flags for the created elements */
subexpr->flags |= (child->flags & SEL_VALFLAGMASK);
subexpr->child->flags |= (child->flags & SEL_VALFLAGMASK);
}
+ if (child->type == SEL_SUBEXPRREF)
+ {
+ child->setName(child->child->name());
+ }
child = child->next;
}
break;
case SEL_SUBEXPRREF:
- sel->name = sel->child->name;
sel->evaluate = ((sel->cdata->flags & SEL_CDATA_SIMPLESUBEXPR)
? &_gmx_sel_evaluate_subexprref_simple
: &_gmx_sel_evaluate_subexprref);
if (subexpr.use_count() == 2)
{
release_subexpr_memory(subexpr);
- subexpr->name = NULL;
// Free children.
subexpr->child.reset();
subexpr->freeValues();
/* Free the expression data as it is no longer needed */
sel->freeExpressionData();
/* Make the item static */
- sel->name = NULL;
sel->type = SEL_CONST;
sel->evaluate = NULL;
sel->cdata->evaluate = NULL;
{
SelectionTreeElementPointer child(new SelectionTreeElement(SEL_CONST));
_gmx_selelem_set_vtype(child, STR_VALUE);
- child->name = param->name;
+ child->setName(param->name);
child->flags &= ~SEL_ALLOCVAL;
child->flags |= SEL_FLAGSSET | SEL_VARNUMVAL | SEL_ALLOCDATA;
child->v.nr = param->val.nr;
SelectionTreeElementPointer p
= _gmx_sel_init_position(s, NULL, scanner);
if (!p) YYERROR;
- set((yyval.sel), _gmx_sel_init_selection(s->name, p, scanner));
+ set((yyval.sel), _gmx_sel_init_selection(s->name().c_str(), p, scanner));
END_ACTION;
}
break;
SelectionTreeElementPointer p
= _gmx_sel_init_position(s, NULL, scanner);
if (!p) YYERROR;
- set((yyval.sel), _gmx_sel_init_selection(s->name, p, scanner));
+ set((yyval.sel), _gmx_sel_init_selection(s->name().c_str(), p, scanner));
END_ACTION;
}
break;
SelectionTreeElementPointer p
= _gmx_sel_init_position(s, NULL, scanner);
if (!p) YYERROR;
- set($$, _gmx_sel_init_selection(s->name, p, scanner));
+ set($$, _gmx_sel_init_selection(s->name().c_str(), p, scanner));
END_ACTION;
}
| string
SelectionTreeElementPointer p
= _gmx_sel_init_position(s, NULL, scanner);
if (!p) YYERROR;
- set($$, _gmx_sel_init_selection(s->name, p, scanner));
+ set($$, _gmx_sel_init_selection(s->name().c_str(), p, scanner));
END_ACTION;
}
| selection
yyscan_t scanner)
{
_gmx_selelem_set_vtype(sel, method->type);
- sel->name = method->name;
+ sel->setName(method->name);
snew(sel->u.expr.method, 1);
memcpy(sel->u.expr.method, method, sizeof(gmx_ana_selmethod_t));
_gmx_selelem_init_method_params(sel, scanner);
char buf[2];
buf[0] = op;
buf[1] = 0;
+ sel->setName(buf);
sel->u.arith.opstr = strdup(buf);
- sel->name = sel->u.arith.opstr;
sel->child = left;
sel->child->next = right;
return sel;
{
SelectionTreeElementPointer sel(new SelectionTreeElement(SEL_GROUPREF));
_gmx_selelem_set_vtype(sel, GROUP_VALUE);
+ sel->setName(name);
sel->u.gref.name = strdup(name);
sel->u.gref.id = -1;
- sel->name = sel->u.gref.name;
return sel;
}
if (!grps)
_gmx_selparser_error(scanner, "Cannot match 'group %s'", name);
return SelectionTreeElementPointer();
}
- sel->name = sel->u.cgrp.name;
+ sel->setName(sel->u.cgrp.name);
return sel;
}
_gmx_selparser_error(scanner, "Cannot match 'group %d'", id);
return SelectionTreeElementPointer();
}
- sel->name = sel->u.cgrp.name;
+ sel->setName(sel->u.cgrp.name);
return sel;
}
{
ref.reset(new SelectionTreeElement(SEL_SUBEXPRREF));
_gmx_selelem_set_vtype(ref, sel->v.type);
- ref->name = sel->name;
+ ref->setName(sel->name());
ref->child = sel;
}
return ref;
root->child = sel;
if (name)
{
- root->name = root->u.cgrp.name = strdup(name);
+ root->setName(name);
+ root->u.cgrp.name = strdup(name);
}
/* Update the flags */
_gmx_selelem_update_flags(root, scanner);
/* If there is no name provided by the user, check whether the actual
* selection given was from an external group, and if so, use the name
* of the external group. */
- if (!root->name)
+ if (root->name().empty())
{
SelectionTreeElementPointer child = root->child;
while (child->type == SEL_MODIFIER)
&& child->child->child->type == SEL_CONST
&& child->child->child->v.type == GROUP_VALUE)
{
- root->name = root->u.cgrp.name =
- strdup(child->child->child->u.cgrp.name);
+ const char *grpName = child->child->child->u.cgrp.name;
+ root->setName(grpName);
+ root->u.cgrp.name = strdup(grpName);
}
}
/* If there still is no name, use the selection string */
- if (!root->name)
+ if (root->name().empty())
{
- root->name = root->u.cgrp.name
- = strdup(_gmx_sel_lexer_pselstr(scanner));
+ const char *selStr = _gmx_sel_lexer_pselstr(scanner);
+ root->setName(selStr);
+ root->u.cgrp.name = strdup(selStr);
}
/* Print out some information if the parser is interactive */
}
/* Create the root element */
root.reset(new SelectionTreeElement(SEL_ROOT));
+ root->setName(name);
+ root->u.cgrp.name = strdup(name);
/* Create the subexpression element */
root->child.reset(new SelectionTreeElement(SEL_SUBEXPR));
+ root->child->setName(name);
_gmx_selelem_set_vtype(root->child, expr->v.type);
- {
- char *newName = strdup(name);
- root->name = newName;
- root->u.cgrp.name = newName;
- root->child->name = newName;
- }
root->child->child = expr;
/* Update flags */
_gmx_selelem_update_flags(root, scanner);
SelectionData::SelectionData(SelectionTreeElement *elem,
const char *selstr)
- : name_(elem->name), selectionText_(selstr),
+ : name_(elem->name()), selectionText_(selstr),
rootElement_(*elem), coveredFractionType_(CFRAC_NONE),
coveredFraction_(1.0), averageCoveredFraction_(1.0),
bDynamic_(false), bDynamicCoveredFraction_(false)
if (bOk)
{
root->type = SEL_CONST;
- root->name = root->u.cgrp.name;
+ root->setName(root->u.cgrp.name);
}
}
SelectionTreeElement::SelectionTreeElement(e_selelem_t type)
{
- this->name = NULL;
this->type = type;
this->flags = (type != SEL_ROOT) ? SEL_ALLOCVAL : 0;
if (type == SEL_BOOLEAN)
fprintf(fp, "%*c %s %s", level*2+1, '*',
_gmx_selelem_type_str(sel), _gmx_sel_value_type_str(&sel.v));
- if (sel.name)
+ if (!sel.name().empty())
{
- fprintf(fp, " \"%s\"", sel.name);
+ fprintf(fp, " \"%s\"", sel.name().c_str());
}
fprintf(fp, " flg=");
if (sel.flags & SEL_FLAGSSET)
#ifndef GMX_SELECTION_SELELEM_H
#define GMX_SELECTION_SELELEM_H
+#include <string>
+
#include <boost/shared_ptr.hpp>
#include "gromacs/legacyheaders/types/simple.h"
*/
void mempoolRelease();
- /*! \brief Name of the element.
+ //! Returns the name of the element.
+ const std::string &name() const { return name_; }
+ /*! \brief
+ * Sets the name of the element.
*
- * This field is only used for informative purposes.
- * It is always either NULL or a pointer to a string.
- * Memory is never allocated for it directly.
+ * \param[in] name Name to set (can be NULL).
+ * \throws std::bad_alloc if out of memory.
*/
- const char *name;
+ void setName(const char *name) { name_ = (name != NULL ? name : ""); }
+ //! \copydoc setName(const char *)
+ void setName(const std::string &name) { name_ = name; }
+
//! Type of the element.
e_selelem_t type;
/*! \brief
SelectionTreeElementPointer next;
private:
+ /*! \brief
+ * Name of the element.
+ *
+ * This field is only used for informative purposes.
+ */
+ std::string name_;
+
GMX_DISALLOW_COPY_AND_ASSIGN(SelectionTreeElement);
};