diff --git a/test/pitytest11/src/PityTree.hh b/test/pitytest11/src/PityTree.hh index 9478c7f..3644e7e 100644 --- a/test/pitytest11/src/PityTree.hh +++ b/test/pitytest11/src/PityTree.hh @@ -29,6 +29,10 @@ namespace pEp { explicit PityTree(T& self, const std::string& name, T& parent); explicit PityTree(const PityTree& rhs, T& owner); + // copy-assign + PityTree& operator=(const PityTree& rhs); + + // clone virtual PityTree* clone() = 0; // Append @@ -62,6 +66,7 @@ namespace pEp { void setParent(T* const parent); private: + void _cloneChildRefs(const PityTree& rhs); // Fields std::string _nodename; T& _self; diff --git a/test/pitytest11/src/PityTree.hxx b/test/pitytest11/src/PityTree.hxx index fdef8f4..7b9fcb6 100644 --- a/test/pitytest11/src/PityTree.hxx +++ b/test/pitytest11/src/PityTree.hxx @@ -44,12 +44,15 @@ namespace pEp { { _nodename = rhs._nodename; _parent = nullptr; + _cloneChildRefs(rhs); + } - for (const ChildRef& cr : rhs.getChildRefs()) { - _childobjs.push_back(ChildObj(cr.second.clone())); - T& ret = *_childobjs.back().get(); - addRef(ret); - } + template + PityTree& PityTree::operator=(const PityTree& rhs) { + _nodename = rhs._nodename; + _parent = nullptr; + _cloneChildRefs(rhs); + return *this; } template @@ -196,6 +199,17 @@ namespace pEp { return name; } + + // When you copy a treenode, you need to create a copy of all children + // and take ownership + template + void PityTree::_cloneChildRefs(const PityTree& rhs) { + for (const ChildRef& cr : rhs.getChildRefs()) { + _childobjs.push_back(ChildObj(cr.second.clone())); + T& ret = *_childobjs.back().get(); + addRef(ret); + } + } } // namespace PityTest11 } // namespace pEp