From 3c7f9586aec6d1e26966c4b836fb2ee9505e0146 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Wed, 7 Mar 2012 02:07:35 +0000 Subject: [PATCH] implement XMLNode::operator=() as a deep operation with the same semantics as the XMLNode copy constructor. attempt to share as much code as possible between them and the destructor git-svn-id: svn://localhost/ardour2/branches/3.0@11612 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/pbd/pbd/xml++.h | 4 +++ libs/pbd/xml++.cc | 60 +++++++++++++++++++++++++++++++------------- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/libs/pbd/pbd/xml++.h b/libs/pbd/pbd/xml++.h index 6a5097780e..6b6ba34f6d 100644 --- a/libs/pbd/pbd/xml++.h +++ b/libs/pbd/pbd/xml++.h @@ -75,6 +75,8 @@ public: XMLNode(const XMLNode& other); ~XMLNode(); + XMLNode& operator= (const XMLNode& other); + const std::string& name() const { return _name; } bool is_content() const { return _is_content; } @@ -121,6 +123,8 @@ private: XMLPropertyList _proplist; XMLPropertyMap _propmap; mutable XMLNodeList _selected_children; + + void clear_lists (); }; class XMLProperty { diff --git a/libs/pbd/xml++.cc b/libs/pbd/xml++.cc index 58a0c4e747..b97310eea5 100644 --- a/libs/pbd/xml++.cc +++ b/libs/pbd/xml++.cc @@ -206,37 +206,63 @@ XMLNode::XMLNode(const string& n, const string& c) XMLNode::XMLNode(const XMLNode& from) { - XMLPropertyList props; - XMLPropertyIterator curprop; - XMLNodeList nodes; - XMLNodeIterator curnode; - - _name = from.name(); - set_content(from.content()); - - props = from.properties(); - for (curprop = props.begin(); curprop != props.end(); ++curprop) { - add_property((*curprop)->name().c_str(), (*curprop)->value()); - } - - nodes = from.children(); - for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) { - add_child_copy(**curnode); - } + *this = from; } XMLNode::~XMLNode() +{ + clear_lists (); +} + +void +XMLNode::clear_lists () { XMLNodeIterator curchild; XMLPropertyIterator curprop; + _selected_children.clear (); + _propmap.clear (); + for (curchild = _children.begin(); curchild != _children.end(); ++curchild) { delete *curchild; } + _children.clear (); + for (curprop = _proplist.begin(); curprop != _proplist.end(); ++curprop) { delete *curprop; } + + _proplist.clear (); +} + +XMLNode& +XMLNode::operator= (const XMLNode& from) +{ + if (&from != this) { + + XMLPropertyList props; + XMLPropertyIterator curprop; + XMLNodeList nodes; + XMLNodeIterator curnode; + + clear_lists (); + + _name = from.name(); + set_content(from.content()); + + props = from.properties(); + for (curprop = props.begin(); curprop != props.end(); ++curprop) { + add_property((*curprop)->name().c_str(), (*curprop)->value()); + } + + nodes = from.children(); + for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) { + add_child_copy(**curnode); + } + } + + return *this; } const string&