Add copyPropertiesIf helper

This commit is contained in:
James Turner 2020-05-03 12:21:36 +01:00
parent ad3621e23b
commit 898559ab31
2 changed files with 55 additions and 0 deletions

View File

@ -854,4 +854,48 @@ copyPropertiesWithAttribute(const SGPropertyNode *in, SGPropertyNode *out,
return true; return true;
} }
namespace {
// for normal recursion we want to call the predicate *before* creating children
bool _inner_copyPropertiesIf(const SGPropertyNode *in, SGPropertyNode *out,
PropertyPredicate predicate)
{
bool retval = copyPropertyValue(in, out);
if (!retval) {
return false;
}
out->setAttributes( in->getAttributes() );
int nChildren = in->nChildren();
for (int i = 0; i < nChildren; i++) {
const SGPropertyNode* in_child = in->getChild(i);
// skip this child
if (!predicate(in_child)) {
continue;
}
SGPropertyNode* out_child = out->getChild(in_child->getNameString(),
in_child->getIndex(),
true);
bool ok = copyPropertiesIf(in_child, out_child, predicate);
if (!ok) {
return false;
}
} // of children iteration
return true;
}
} // of anonymous namespace
bool copyPropertiesIf(const SGPropertyNode *in, SGPropertyNode *out,
PropertyPredicate predicate)
{
// allow the *entire* copy to be a no-op
bool doCopy = predicate(in);
if (!doCopy)
return true; // doesn't count as failure
return _inner_copyPropertiesIf(in, out, predicate);
}
// end of props_io.cxx // end of props_io.cxx

View File

@ -17,6 +17,7 @@
#include <string> #include <string>
#include <iosfwd> #include <iosfwd>
#include <functional>
/** /**
* Read properties from an XML input stream. * Read properties from an XML input stream.
@ -67,6 +68,16 @@ bool copyProperties (const SGPropertyNode *in, SGPropertyNode *out);
bool copyPropertiesWithAttribute(const SGPropertyNode *in, SGPropertyNode *out, bool copyPropertiesWithAttribute(const SGPropertyNode *in, SGPropertyNode *out,
SGPropertyNode::Attribute attr); SGPropertyNode::Attribute attr);
using PropertyPredicate = std::function<bool (const SGPropertyNode* in)>;
/**
* Copy properties, if the predicate returns true for the in node.
* If a parent node returns false, descendats will <em>not</em> be
* checked
*/
bool copyPropertiesIf(const SGPropertyNode *in, SGPropertyNode *out,
PropertyPredicate predicate);
#endif // __PROPS_IO_HXX #endif // __PROPS_IO_HXX
// end of props_io.hxx // end of props_io.hxx