From: Thomas Geymayer Date: Sun, 14 Oct 2012 15:26:52 +0000 (+0200) Subject: Add method SGPropertyNode::addChildren to create multiple children at once X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e24e3c06122dcba0d15a3d01be8957bb6b96d739;p=simgear.git Add method SGPropertyNode::addChildren to create multiple children at once --- diff --git a/simgear/props/props.cxx b/simgear/props/props.cxx index d2691747..09b71fb4 100644 --- a/simgear/props/props.cxx +++ b/simgear/props/props.cxx @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -892,6 +893,52 @@ SGPropertyNode::addChild(const char * name, int min_index, bool append) return node; } +/** + * Create multiple children with unused indices + */ +simgear::PropertyList +SGPropertyNode::addChildren( const std::string& name, + size_t count, + int min_index, + bool append ) +{ + simgear::PropertyList nodes; + std::set used_indices; + + if( !append ) + { + // First grab all used indices. This saves us of testing every index if it + // is used for every element to be created + for( size_t i = 0; i < nodes.size(); i++ ) + { + const SGPropertyNode* node = nodes[i]; + + if( node->getNameString() == name && node->getIndex() >= min_index ) + used_indices.insert(node->getIndex()); + } + } + else + { + // If we don't want to fill the holes just find last node + min_index = std::max(find_last_child(name.c_str(), _children) + 1, min_index); + } + + for( int index = min_index; + index < std::numeric_limits::max() && nodes.size() < count; + ++index ) + { + if( used_indices.find(index) == used_indices.end() ) + { + SGPropertyNode_ptr node; + node = new SGPropertyNode(name, index, this); + _children.push_back(node); + fireChildAdded(node); + nodes.push_back(node); + } + } + + return nodes; +} /** * Get a non-const child by index. diff --git a/simgear/props/props.hxx b/simgear/props/props.hxx index bf0e563e..e0a1d0c6 100644 --- a/simgear/props/props.hxx +++ b/simgear/props/props.hxx @@ -851,7 +851,7 @@ public: } /** - * Create a child node after the last node with the same name. + * Create a new child node with the given name and an unused index * * @param min_index Minimal index for new node (skips lower indices) * @param append Whether to simply use the index after the last used index @@ -865,6 +865,19 @@ public: bool append = true ) { return addChild(name.c_str(), min_index, append); } + /** + * Create multiple child nodes with the given name an unused indices + * + * @param count The number of nodes create + * @param min_index Minimal index for new nodes (skips lower indices) + * @param append Whether to simply use the index after the last used index + * or use a lower, unused index if it exists + */ + simgear::PropertyList addChildren ( const std::string& name, + size_t count, + int min_index = 0, + bool append = true ); + /** * Get a child node by name and index. */