diff --git a/include/Tree/tree.hpp b/include/Tree/tree.hpp index 4d0fbd4..7f8ffcb 100644 --- a/include/Tree/tree.hpp +++ b/include/Tree/tree.hpp @@ -46,8 +46,21 @@ namespace Tree /// all child nodes of a node std::list> children; + + virtual std::shared_ptr clone_() const = 0; + + public: + /** + * @brief clear the node of parent and children, but do not delete the children, required for cloning + * + */ + void clear() { + children.clear(); + parent=nullptr; + }; + /** * @brief get the root of the tree * @@ -63,6 +76,25 @@ namespace Tree } + /** + * @brief method to deep clone this node + * + * @return pointer to the cloned object + */ + std::shared_ptr clone() const + { + std::shared_ptr node = clone_(); + node->clear(); + + std::list>::const_iterator it; + for(it = children.begin(); it != children.end(); ++it) + { + node->addChild((*it)->clone()); + } + + return node; + } + /** * @brief print the tree starting with the current node to stdout * @@ -353,6 +385,9 @@ namespace Tree */ class TreeRoot : public Tree::BaseNode { + private: + virtual std::shared_ptr clone_() const {return std::make_shared();}; + public: /** * @brief returns the type of this node as a string @@ -390,6 +425,8 @@ namespace Tree */ class TempNode : public Tree::BaseNode { + private: + virtual std::shared_ptr clone_() const {return std::make_shared();}; public: /** * @brief returns the type of this node as a string