ADD: added possibility to clone a tree node

This commit is contained in:
Dominik Meyer 2019-08-28 15:00:06 +02:00
parent 64219f581d
commit ee840c6134
No known key found for this signature in database
GPG Key ID: B4C312B600606B64
1 changed files with 37 additions and 0 deletions

View File

@ -46,8 +46,21 @@ namespace Tree
/// all child nodes of a node
std::list<std::shared_ptr<Tree::BaseNode>> children;
virtual std::shared_ptr<Tree::BaseNode> 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<Tree::BaseNode> clone() const
{
std::shared_ptr<Tree::BaseNode> node = clone_();
node->clear();
std::list<std::shared_ptr<Tree::BaseNode>>::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<Tree::BaseNode> clone_() const {return std::make_shared<Tree::TreeRoot>();};
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<Tree::BaseNode> clone_() const {return std::make_shared<Tree::TempNode>();};
public:
/**
* @brief returns the type of this node as a string