* \param[in,out] scanner Scanner data structure.
* \param[in] maxnr Maximum number of selections to parse
* (if -1, parse as many as provided by the user).
- * \param[out] output Vector to which parsed selections are
- * appended.
+ * \returns Vector of parsed selections.
* \throws std::bad_alloc if out of memory.
* \throws InvalidInputError if there is a parsing error.
*
- * Parsed selections are appended to \p output without clearing it
- * first. If parsing fails, \p output is not modified.
- *
* Used internally to implement parseFromStdin(), parseFromFile() and
* parseFromString().
*/
- void runParser(void *scanner, int maxnr,
- SelectionList *output);
+ SelectionList runParser(void *scanner, int maxnr);
/*! \brief
* Replace group references by group contents.
*
}
-void
-SelectionCollection::Impl::runParser(yyscan_t scanner, int maxnr,
- SelectionList *output)
+SelectionList
+SelectionCollection::Impl::runParser(yyscan_t scanner, int maxnr)
{
gmx_ana_selcollection_t *sc = &_sc;
GMX_ASSERT(sc == _gmx_sel_lexer_selcollection(scanner),
errors.append("Too few selections provided");
}
- if (bOk)
- {
- SelectionDataList::const_iterator i;
- output->reserve(output->size() + nr);
- for (i = _sc.sel.begin() + oldCount; i != _sc.sel.end(); ++i)
- {
- output->push_back(Selection(i->get()));
- }
- }
// TODO: Remove added selections from the collection if parsing failed?
-
if (!bOk || !errors.isEmpty())
{
GMX_ASSERT(!bOk && !errors.isEmpty(), "Inconsistent error reporting");
GMX_THROW(InvalidInputError(errors.toString()));
}
+
+ SelectionList result;
+ SelectionDataList::const_iterator i;
+ result.reserve(nr);
+ for (i = _sc.sel.begin() + oldCount; i != _sc.sel.end(); ++i)
+ {
+ result.push_back(Selection(i->get()));
+ }
+ return result;
}
}
-void
-SelectionCollection::parseFromStdin(int nr, bool bInteractive,
- SelectionList *output)
+SelectionList
+SelectionCollection::parseFromStdin(int nr, bool bInteractive)
{
yyscan_t scanner;
_impl->_grps);
/* We don't set the lexer input here, which causes it to use a special
* internal implementation for reading from stdin. */
- _impl->runParser(scanner, nr, output);
+ return _impl->runParser(scanner, nr);
}
-void
-SelectionCollection::parseFromFile(const std::string &filename,
- SelectionList *output)
+SelectionList
+SelectionCollection::parseFromFile(const std::string &filename)
{
yyscan_t scanner;
_impl->_bExternalGroupsSet,
_impl->_grps);
_gmx_sel_set_lex_input_file(scanner, file.handle());
- _impl->runParser(scanner, -1, output);
- file.close();
+ return _impl->runParser(scanner, -1);
}
-void
-SelectionCollection::parseFromString(const std::string &str,
- SelectionList *output)
+SelectionList
+SelectionCollection::parseFromString(const std::string &str)
{
yyscan_t scanner;
_impl->_bExternalGroupsSet,
_impl->_grps);
_gmx_sel_set_lex_input_str(scanner, str.c_str());
- _impl->runParser(scanner, -1, output);
+ return _impl->runParser(scanner, -1);
}
* (if -1, parse as many as provided by the user).
* \param[in] bInteractive Whether the parser should behave
* interactively.
- * \param[out] output Vector to which parsed selections are appended.
+ * \returns Vector of parsed selections.
* \throws std::bad_alloc if out of memory.
* \throws InvalidInputError if there is a parsing error
* (an interactive parser only throws this if too few selections
* are provided and the user forced the end of input).
*
- * Parsed selections are appended to \p output without clearing it
- * first. If parsing fails, \p output is not modified.
- *
- * The objects returned in \p output remain valid for the lifetime of
+ * The returned objects remain valid for the lifetime of
* the selection collection.
* Some information about the selections only becomes available once
* compile() has been called; see \ref Selection.
*/
- void parseFromStdin(int count, bool bInteractive,
- SelectionList *output);
+ SelectionList parseFromStdin(int count, bool bInteractive);
/*! \brief
* Parses selection(s) from a file.
*
* \param[in] filename Name of the file to parse selections from.
- * \param[out] output Vector to which parsed selections are appended.
+ * \returns Vector of parsed selections.
* \throws std::bad_alloc if out of memory.
* \throws InvalidInputError if there is a parsing error.
*
- * Parsed selections are appended to \p output without clearing it
- * first. If parsing fails, \p output is not modified.
- *
- * The objects returned in \p output remain valid for the lifetime of
+ * The returned objects remain valid for the lifetime of
* the selection collection.
* Some information about the selections only becomes available once
* compile() has been called; see \ref Selection.
*/
- void parseFromFile(const std::string &filename,
- SelectionList *output);
+ SelectionList parseFromFile(const std::string &filename);
/*! \brief
* Parses selection(s) from a string.
*
* \param[in] str String to parse selections from.
- * \param[out] output Vector to which parsed selections are appended.
+ * \returns Vector of parsed selections.
* \throws std::bad_alloc if out of memory.
* \throws InvalidInputError if there is a parsing error.
*
- * Parsed selections are appended to \p output without clearing it
- * first. If parsing fails, \p output is not modified.
- *
- * The objects returned in \p output remain valid for the lifetime of
+ * The returned objects remain valid for the lifetime of
* the selection collection.
* Some information about the selections only becomes available once
* compile() has been called; see \ref Selection.
*/
- void parseFromString(const std::string &str,
- SelectionList *output);
+ SelectionList parseFromString(const std::string &str);
/*! \brief
* Prepares the selections for evaluation and performs optimizations.
*
SelectionOptionManager::convertOptionValue(SelectionOptionStorage *storage,
const std::string &value)
{
- SelectionList selections;
- impl_->collection_.parseFromString(value, &selections);
+ SelectionList selections = impl_->collection_.parseFromString(value);
storage->addSelections(selections, false);
}
std::fprintf(stderr, "(one selection per line, 'help' for help%s)\n",
request.count() < 0 ? ", Ctrl-D to end" : "");
}
- SelectionList selections;
- impl_->collection_.parseFromStdin(request.count(), bInteractive, &selections);
+ SelectionList selections
+ = impl_->collection_.parseFromStdin(request.count(), bInteractive);
request.storage_->addSelections(selections, true);
}
}
void
SelectionOptionManager::parseRequestedFromFile(const std::string &filename)
{
- SelectionList selections;
- impl_->collection_.parseFromFile(filename, &selections);
+ SelectionList selections = impl_->collection_.parseFromFile(filename);
impl_->placeSelectionsInRequests(selections);
}
void
SelectionOptionManager::parseRequestedFromString(const std::string &str)
{
- SelectionList selections;
- impl_->collection_.parseFromString(str, &selections);
+ SelectionList selections = impl_->collection_.parseFromString(str);
impl_->placeSelectionsInRequests(selections);
}
{
SCOPED_TRACE(std::string("Parsing selection \"")
+ selections[i] + "\"");
- ASSERT_NO_THROW(_sc.parseFromString(selections[i], &_sel));
+ gmx::SelectionList result;
+ ASSERT_NO_THROW(result = _sc.parseFromString(selections[i]));
+ _sel.insert(_sel.end(), result.begin(), result.end());
if (_sel.size() == _count)
{
std::string id = gmx::formatString("Variable%d", static_cast<int>(varcount + 1));
TEST_F(SelectionCollectionTest, ParsesSelectionsFromFile)
{
- ASSERT_NO_THROW(_sc.parseFromFile(gmx::test::getTestFilePath("selfile.dat"),
- &_sel));
+ ASSERT_NO_THROW(_sel = _sc.parseFromFile(gmx::test::getTestFilePath("selfile.dat")));
// These should match the contents of selfile.dat
ASSERT_EQ(2U, _sel.size());
EXPECT_STREQ("resname RA RB", _sel[0].selectionText());
TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue)
{
- EXPECT_THROW(_sc.parseFromString("mindist from atomnr 1 cutoff", &_sel),
+ EXPECT_THROW(_sc.parseFromString("mindist from atomnr 1 cutoff"),
gmx::InvalidInputError);
}
TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue2)
{
- EXPECT_THROW(_sc.parseFromString("within 1 of", &_sel),
+ EXPECT_THROW(_sc.parseFromString("within 1 of"),
gmx::InvalidInputError);
}
TEST_F(SelectionCollectionTest, HandlesMissingMethodParamValue3)
{
- EXPECT_THROW(_sc.parseFromString("within of atomnr 1", &_sel),
+ EXPECT_THROW(_sc.parseFromString("within of atomnr 1"),
gmx::InvalidInputError);
}
TEST_F(SelectionCollectionTest, RecoversFromUnknownGroupReference)
{
- ASSERT_NO_THROW(_sc.parseFromString("group \"foo\"", &_sel));
+ ASSERT_NO_THROW(_sc.parseFromString("group \"foo\""));
ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
EXPECT_THROW(_sc.setIndexGroups(NULL), gmx::InvalidInputError);
EXPECT_THROW(_sc.compile(), gmx::APIError);
TEST_F(SelectionCollectionTest, RecoversFromMissingMoleculeInfo)
{
- ASSERT_NO_THROW(_sc.parseFromString("molindex 1 to 5", &_sel));
+ ASSERT_NO_THROW(_sc.parseFromString("molindex 1 to 5"));
ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
EXPECT_THROW(_sc.compile(), gmx::InconsistentInputError);
}
TEST_F(SelectionCollectionTest, RecoversFromMissingAtomTypes)
{
- ASSERT_NO_THROW(_sc.parseFromString("type CA", &_sel));
+ ASSERT_NO_THROW(_sc.parseFromString("type CA"));
ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
EXPECT_THROW(_sc.compile(), gmx::InconsistentInputError);
}
TEST_F(SelectionCollectionTest, RecoversFromMissingPDBInfo)
{
- ASSERT_NO_THROW(_sc.parseFromString("altloc A", &_sel));
+ ASSERT_NO_THROW(_sc.parseFromString("altloc A"));
ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
EXPECT_THROW(_sc.compile(), gmx::InconsistentInputError);
}
TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation)
{
- ASSERT_NO_THROW(_sc.parseFromString("all permute 1 1", &_sel));
+ ASSERT_NO_THROW(_sc.parseFromString("all permute 1 1"));
ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
EXPECT_THROW(_sc.compile(), gmx::InvalidInputError);
}
TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation2)
{
- ASSERT_NO_THROW(_sc.parseFromString("all permute 3 2 1", &_sel));
+ ASSERT_NO_THROW(_sc.parseFromString("all permute 3 2 1"));
ASSERT_NO_FATAL_FAILURE(setAtomCount(10));
EXPECT_THROW(_sc.compile(), gmx::InconsistentInputError);
}
TEST_F(SelectionCollectionTest, RecoversFromInvalidPermutation3)
{
- ASSERT_NO_THROW(_sc.parseFromString("x < 1.5 permute 3 2 1", &_sel));
+ ASSERT_NO_THROW(_sc.parseFromString("x < 1.5 permute 3 2 1"));
ASSERT_NO_FATAL_FAILURE(loadTopology("simple.gro"));
ASSERT_NO_THROW(_sc.compile());
EXPECT_THROW(_sc.evaluate(_frame, NULL), gmx::InconsistentInputError);