]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/props.cxx
Patch from Cameron Moore:
[simgear.git] / simgear / misc / props.cxx
index bb13712414c91db125634f1e204a369a20129a15..0af19b435799e79f54772a974c9ae617276d069c 100644 (file)
@@ -19,6 +19,7 @@ using std::cerr;
 using std::endl;
 using std::find;
 using std::sort;
+using std::vector;
 
 #else
 
@@ -26,6 +27,15 @@ using std::sort;
 #include <simgear/debug/logstream.hxx>
 
 SG_USING_STD(sort);
+SG_USING_STD(find);
+SG_USING_STD(vector);
+
+#ifdef _MSC_VER
+// MSVC is buggy, and needs something strange here
+SG_USING_STD(vector<SGPropertyNode_ptr>);
+SG_USING_STD(vector<SGPropertyChangeListener *>);
+SG_USING_STD(vector<SGPropertyNode *>);
+#endif
 
 #endif
 
@@ -623,8 +633,10 @@ const int SGPropertyNode::LAST_USED_ATTRIBUTE = TRACE_WRITE;
  */
 SGPropertyNode::SGPropertyNode ()
   : _name(copy_string("")),
+    _display_name(0),
     _index(0),
     _parent(0),
+    _path(0),
     _path_cache(0),
     _type(NONE),
     _tied(false),
@@ -640,8 +652,10 @@ SGPropertyNode::SGPropertyNode ()
  * Copy constructor.
  */
 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
-  : _index(node._index),
+  : _display_name(0),
+    _index(node._index),
     _parent(0),                        // don't copy the parent
+    _path(0),
     _path_cache(0),
     _type(node._type),
     _tied(node._tied),
@@ -723,8 +737,10 @@ SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
 SGPropertyNode::SGPropertyNode (const char * name,
                                int index,
                                SGPropertyNode * parent)
-  : _index(index),
+  : _display_name(0),
+    _index(index),
     _parent(parent),
+    _path(0),
     _path_cache(0),
     _type(NONE),
     _tied(false),
@@ -743,6 +759,8 @@ SGPropertyNode::SGPropertyNode (const char * name,
 SGPropertyNode::~SGPropertyNode ()
 {
   delete [] _name;
+  delete [] _display_name;
+  delete [] _path;
   delete _path_cache;
   clear_value();
   delete _listeners;
@@ -844,7 +862,7 @@ SGPropertyNode::getChild (const char * name, int index, bool create)
     SGPropertyNode_ptr node;
     pos = find_child(name, index, _removedChildren);
     if (pos >= 0) {
-      std::vector<SGPropertyNode_ptr>::iterator it = _removedChildren.begin();
+      vector<SGPropertyNode_ptr>::iterator it = _removedChildren.begin();
       it += pos;
       node = _removedChildren[pos];
       _removedChildren.erase(it);
@@ -902,7 +920,7 @@ SGPropertyNode::removeChild (const char * name, int index, bool keep)
   SGPropertyNode_ptr ret;
   int pos = find_child(name, index, _children);
   if (pos >= 0) {
-    std::vector<SGPropertyNode_ptr>::iterator it = _children.begin();
+    vector<SGPropertyNode_ptr>::iterator it = _children.begin();
     it += pos;
     SGPropertyNode_ptr node = _children[pos];
     _children.erase(it);
@@ -918,20 +936,36 @@ SGPropertyNode::removeChild (const char * name, int index, bool keep)
 
 
 const char *
-SGPropertyNode::getPath (bool simplify) const
+SGPropertyNode::getDisplayName (bool simplify) const
 {
-  if (_parent == 0)
-    return "";
-
-  string path = _parent->getPath(simplify);
-  path += '/';
-  path += _name;
+  string display = _name;
   if (_index != 0 || !simplify) {
-    char buffer[128];
+    char buffer[64];
     sprintf(buffer, "[%d]", _index);
-    path += buffer;
+    display += buffer;
   }
-  return path.c_str();
+  _display_name = copy_string(display.c_str());
+  return _display_name;
+}
+
+
+const char *
+SGPropertyNode::getPath (bool simplify) const
+{
+                               // Calculate the complete path only once.
+  if (_path == 0) {
+    string path;
+    if (_parent == 0) {
+      path = "";
+    } else {
+      path = _parent->getPath(simplify);
+      path += '/';
+      path += getDisplayName(simplify);
+    }
+    _path = copy_string(path.c_str());
+  }
+
+  return _path;
 }
 
 SGPropertyNode::Type
@@ -2029,7 +2063,7 @@ void
 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
 {
   if (_listeners != 0) {
-    for (int i = 0; i < _listeners->size(); i++) {
+    for (unsigned int i = 0; i < _listeners->size(); i++) {
       (*_listeners)[i]->valueChanged(node);
     }
   }
@@ -2042,7 +2076,7 @@ SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
                                SGPropertyNode * child)
 {
   if (_listeners != 0) {
-    for (int i = 0; i < _listeners->size(); i++) {
+    for (unsigned int i = 0; i < _listeners->size(); i++) {
       (*_listeners)[i]->childAdded(parent, child);
     }
   }
@@ -2055,7 +2089,7 @@ SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
                                  SGPropertyNode * child)
 {
   if (_listeners != 0) {
-    for (int i = 0; i < _listeners->size(); i++) {
+    for (unsigned int i = 0; i < _listeners->size(); i++) {
       (*_listeners)[i]->childRemoved(parent, child);
     }
   }