Add a version of XML_Node::getChildren with a simpler interface

This commit is contained in:
Ray Speth 2014-10-22 21:48:04 +00:00
parent 424a04bffc
commit a208c54d1c
10 changed files with 44 additions and 44 deletions

View file

@ -566,9 +566,19 @@ public:
*
* @param children output vector of pointers to XML_Node children
* with the matching name
* @deprecated To be removed after Cantera 2.2. Use the version that returns
* the vector of child nodes
*/
void getChildren(const std::string& name, std::vector<XML_Node*>& children) const;
//! Get a vector of pointers to XML_Node containing all of the children
//! of the current node which matche the given name
/*!
* @param name Name of the XML_Node children to search for
* @return vector of pointers to child XML_Nodes with the matching name
*/
std::vector<XML_Node*> getChildren(const std::string& name) const;
//! Return a changeable reference to a child of the current node, named by the argument
/*!
* Note the underlying data allows for more than one XML element with the same name.

View file

@ -183,8 +183,7 @@ void getNamedStringValue(const Cantera::XML_Node& node, const std::string& nameS
void getIntegers(const Cantera::XML_Node& node,
std::map<std::string, int>& v)
{
std::vector<XML_Node*> f;
node.getChildren("integer",f);
std::vector<XML_Node*> f = node.getChildren("integer");
for (size_t i = 0; i < f.size(); i++) {
const XML_Node& fi = *f[i];
if (fi["min"] != "" && fi["max"] != "") {
@ -329,16 +328,14 @@ size_t getFloatArray(const Cantera::XML_Node& node, std::vector<doublereal> & v,
{
const Cantera::XML_Node* readNode = &node;
if (node.name() != nodeName) {
vector<Cantera::XML_Node*> ll;
node.getChildren(nodeName, ll);
vector<Cantera::XML_Node*> ll = node.getChildren(nodeName);
if (ll.size() == 0) {
throw CanteraError("getFloatArray",
"wrong xml element type/name: was expecting "
+ nodeName + "but accessed " + node.name());
} else {
readNode = ll[0];
ll.clear();
readNode->getChildren("floatArray", ll);
ll = readNode->getChildren("floatArray");
if (ll.size() > 0) {
readNode = ll[0];
}

View file

@ -897,6 +897,8 @@ void XML_Node::unlock()
void XML_Node::getChildren(const std::string& nm,
std::vector<XML_Node*>& children_) const
{
warn_deprecated("XML_Node::getChildren", "To be removed after Cantera 2.2."
"Use overload that returns the vector of XML_Node pointers.");
for (size_t i = 0; i < nChildren(); i++) {
if (child(i).name() == nm) {
children_.push_back(&child(i));
@ -904,6 +906,17 @@ void XML_Node::getChildren(const std::string& nm,
}
}
std::vector<XML_Node*> XML_Node::getChildren(const std::string& nm) const
{
std::vector<XML_Node*> children_;
for (size_t i = 0; i < nChildren(); i++) {
if (child(i).name() == nm) {
children_.push_back(&child(i));
}
}
return children_;
}
XML_Node& XML_Node::child(const std::string& aloc) const
{
string::size_type iloc;

View file

@ -755,8 +755,7 @@ void importFromXML(string infile, string id)
if (x.name() != "multiphase")
throw CanteraError("MultiPhase::importFromXML",
"Current XML_Node is not a multiphase element.");
vector<XML_Node*> phases;
x.getChildren("phase",phases);
vector<XML_Node*> phases = x.getChildren("phase");
int np = phases.size();
int n;
ThermoPhase* p;

View file

@ -206,8 +206,7 @@ bool getReagents(const XML_Node& rxn, Kinetics& kin, int rp,
*/
if (rp == 1 && rxn.hasChild("order")) {
std::vector<XML_Node*> ord;
rxn.getChildren("order", ord);
std::vector<XML_Node*> ord = rxn.getChildren("order");
doublereal forder;
for (size_t nn = 0; nn < ord.size(); nn++) {
const XML_Node& oo = *ord[nn];
@ -306,8 +305,7 @@ bool getOrders(const XML_Node& rxnNode, Kinetics& kin,
* Check to see if reaction orders have been specified.
*/
if (rxnNode.hasChild("order")) {
std::vector<XML_Node*> ord;
rxnNode.getChildren("order", ord);
std::vector<XML_Node*> ord = rxnNode.getChildren("order");
doublereal forder;
for (size_t nn = 0; nn < ord.size(); nn++) {
const XML_Node& oo = *ord[nn];
@ -327,8 +325,7 @@ bool getOrders(const XML_Node& rxnNode, Kinetics& kin,
}
if (rxnNode.hasChild("orders")) {
std::vector<XML_Node*> orders;
rxnNode.getChildren("orders", orders);
std::vector<XML_Node*> orders = rxnNode.getChildren("orders");
//
// Doesn't really make sense to have more than one of these blocks
//
@ -628,8 +625,7 @@ static void getStick(const XML_Node& node, Kinetics& kin,
static void getCoverageDependence(const XML_Node& node,
thermo_t& surfphase, ReactionData& rdata)
{
vector<XML_Node*> cov;
node.getChildren("coverage", cov);
vector<XML_Node*> cov = node.getChildren("coverage");
size_t k, nc = cov.size();
doublereal e;
string spname;
@ -1131,8 +1127,6 @@ bool installReactionArrays(const XML_Node& p, Kinetics& kin,
std::string default_phase, bool check_for_duplicates)
{
rxninfo _rxns;
vector<XML_Node*> rarrays;
int itot = 0;
/*
* Search the children of the phase element for the
@ -1142,7 +1136,7 @@ bool installReactionArrays(const XML_Node& p, Kinetics& kin,
* Each one will be processed sequentially, with the
* end result being purely additive.
*/
p.getChildren("reactionArray",rarrays);
vector<XML_Node*> rarrays = p.getChildren("reactionArray");
if (rarrays.empty()) {
kin.finalize();
return false;
@ -1190,11 +1184,8 @@ bool installReactionArrays(const XML_Node& p, Kinetics& kin,
* a reaction if it's tagged by one of the include fields.
* Or, we include all reactions if there are no include fields.
*/
vector<XML_Node*> incl;
rxns.getChildren("include",incl);
vector<XML_Node*> allrxns;
rdata->getChildren("reaction",allrxns);
vector<XML_Node*> incl = rxns.getChildren("include");
vector<XML_Node*> allrxns = rdata->getChildren("reaction");
// if no 'include' directive, then include all reactions
if (incl.empty()) {
for (size_t i = 0; i < allrxns.size(); i++) {

View file

@ -109,8 +109,7 @@ XML_Node& Domain1D::save(XML_Node& o, const doublereal* const sol)
void Domain1D::restore(const XML_Node& dom, doublereal* soln, int loglevel)
{
vector_fp values;
vector<XML_Node*> nodes;
dom.getChildren("floatArray", nodes);
vector<XML_Node*> nodes = dom.getChildren("floatArray");
for (size_t i = 0; i < nodes.size(); i++) {
string title = nodes[i]->attrib("title");
getFloatArray(*nodes[i], values, false);

View file

@ -127,8 +127,7 @@ void Sim1D::restore(const std::string& fname, const std::string& id,
throw CanteraError("Sim1D::restore","No solution with id = "+id);
}
vector<XML_Node*> xd;
f->getChildren("domain", xd);
vector<XML_Node*> xd = f->getChildren("domain");
if (xd.size() != m_nd) {
throw CanteraError("Sim1D::restore", "Solution does not contain the "
" correct number of domains. Found " +

View file

@ -587,8 +587,7 @@ void StFlow::restore(const XML_Node& dom, doublereal* soln, int loglevel)
size_t nsp = m_thermo->nSpecies();
vector_int did_species(nsp, 0);
vector<XML_Node*> str;
dom.getChildren("string",str);
vector<XML_Node*> str = dom.getChildren("string");
for (size_t istr = 0; istr < str.size(); istr++) {
const XML_Node& nd = *str[istr];
writelog(nd["title"]+": "+nd.value()+"\n");
@ -598,8 +597,7 @@ void StFlow::restore(const XML_Node& dom, doublereal* soln, int loglevel)
pp = getFloat(dom, "pressure", "pressure");
setPressure(pp);
vector<XML_Node*> d;
dom.child("grid_data").getChildren("floatArray",d);
vector<XML_Node*> d = dom.child("grid_data").getChildren("floatArray");
size_t nd = d.size();
vector_fp x;

View file

@ -342,8 +342,7 @@ void LatticeSolidPhase::installSlavePhases(Cantera::XML_Node* phaseNode)
XML_Node& eosdata = phaseNode->child("thermo");
XML_Node& la = eosdata.child("LatticeArray");
std::vector<XML_Node*> lattices;
la.getChildren("phase",lattices);
std::vector<XML_Node*> lattices = la.getChildren("phase");
for (size_t n = 0; n < m_nlattice; n++) {
LatticePhase* lp = m_lattice[n];
size_t nsp = lp->nSpecies();
@ -471,8 +470,7 @@ void LatticeSolidPhase::setParametersFromXML(const XML_Node& eosdata)
{
eosdata._require("model","LatticeSolid");
XML_Node& la = eosdata.child("LatticeArray");
std::vector<XML_Node*> lattices;
la.getChildren("phase",lattices);
std::vector<XML_Node*> lattices = la.getChildren("phase");
size_t nl = lattices.size();
m_nlattice = nl;
for (size_t n = 0; n < nl; n++) {

View file

@ -243,8 +243,7 @@ static void formSpeciesXMLNodeList(std::vector<XML_Node*> &spDataNodeList,
// spArray_names field, then add all species
// defined in the corresponding database to the phase
if (nsp == 1 && spnames[0] == "all") {
std::vector<XML_Node*> allsp;
db->getChildren("species", allsp);
std::vector<XML_Node*> allsp = db->getChildren("species");
nsp = allsp.size();
spnames.resize(nsp);
for (size_t nn = 0; nn < nsp; nn++) {
@ -266,8 +265,7 @@ static void formSpeciesXMLNodeList(std::vector<XML_Node*> &spDataNodeList,
}
}
} else if (nsp == 1 && spnames[0] == "unique") {
std::vector<XML_Node*> allsp;
db->getChildren("species", allsp);
std::vector<XML_Node*> allsp = db->getChildren("species");
nsp = allsp.size();
spnames.resize(nsp);
for (size_t nn = 0; nn < nsp; nn++) {
@ -388,8 +386,7 @@ bool importPhase(XML_Node& phase, ThermoPhase* th,
* sources. For each one, a speciesArray element must be
* present.
***************************************************************/
vector<XML_Node*> sparrays;
phase.getChildren("speciesArray", sparrays);
vector<XML_Node*> sparrays = phase.getChildren("speciesArray");
if (ssConvention != cSS_CONVENTION_SLAVE) {
if (sparrays.empty()) {
throw CanteraError("importPhase",
@ -641,8 +638,7 @@ const XML_Node* speciesXML_Node(const std::string& kname,
throw CanteraError("speciesXML_Node()",
"Unexpected phaseSpeciesData name: " + jname);
}
vector<XML_Node*> xspecies;
phaseSpeciesData->getChildren("species", xspecies);
vector<XML_Node*> xspecies = phaseSpeciesData->getChildren("species");
for (size_t j = 0; j < xspecies.size(); j++) {
const XML_Node& sp = *xspecies[j];
jname = sp["name"];