Add method SGPropertyNode::addChildren to create multiple children at once

This commit is contained in:
Thomas Geymayer
2012-10-14 17:26:52 +02:00
parent 1dfac0a8b9
commit e24e3c0612
2 changed files with 61 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
#include <algorithm>
#include <limits>
#include <set>
#include <sstream>
#include <iomanip>
#include <iterator>
@@ -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<int> 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<int>::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.

View File

@@ -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.
*/