#include "vectorPropTemplates.hxx"
#include <algorithm>
+#include <limits>
#include <sstream>
#include <iomanip>
return index;
}
+/**
+ * Get first unused index for child nodes with the given name
+ */
+static int
+first_unused_index( const char * name,
+ const PropertyList& nodes,
+ int min_index )
+{
+ const char* nameEnd = name + strlen(name);
+
+ for( int index = min_index; index < std::numeric_limits<int>::max(); ++index )
+ {
+ if( find_child(name, nameEnd, index, nodes) < 0 )
+ return index;
+ }
+
+ SG_LOG(SG_GENERAL, SG_ALERT, "Too much nodes: " << name);
+ return -1;
+}
+
template<typename Itr>
inline SGPropertyNode*
SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
* create a non-const child by name after the last node with the same name.
*/
SGPropertyNode *
-SGPropertyNode::addChild (const char * name)
+SGPropertyNode::addChild(const char * name, int min_index, bool append)
{
- int pos = find_last_child(name, _children)+1;
+ int pos = append
+ ? std::max(find_last_child(name, _children) + 1, min_index)
+ : first_unused_index(name, _children, min_index);
SGPropertyNode_ptr node;
node = new SGPropertyNode(name, name + strlen(name), pos, this);
*/
static const int LAST_USED_ATTRIBUTE;
-
/**
* Default constructor.
*/
/**
* Create a child node after the last node with the same name.
- */
- SGPropertyNode * addChild (const char * name);
+ *
+ * @param min_index Minimal index for new node (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
+ */
+ SGPropertyNode * addChild ( const char* name,
+ int min_index = 0,
+ bool append = true );
+ SGPropertyNode * addChild ( const std::string& name,
+ int min_index = 0,
+ bool append = true )
+ { return addChild(name.c_str(), min_index, append); }
/**
* Get a child node by name and index.