diff --git a/include/Tree/tree.hpp b/include/Tree/tree.hpp index a97a221..9f8bfb5 100644 --- a/include/Tree/tree.hpp +++ b/include/Tree/tree.hpp @@ -168,7 +168,21 @@ namespace Tree * * @param c - a shared_ptr to a BaseNode */ - void addChild(const std::shared_ptr &c) {c->setParent(shared_from_this()); children.push_back(c); } + void addChild(const std::shared_ptr &c) { + if (c!=nullptr) + { + c->setParent(shared_from_this()); + children.push_back(c); + } + } + + + /** + * @brief prepends a child to the node which is already a shared_ptr + * + * @param c - a shared_ptr to a BaseNode + */ + void prependChild(const std::shared_ptr &c) {c->setParent(shared_from_this()); children.push_front(c); } /** * @brief adds a container of nodes to the list of children @@ -180,6 +194,15 @@ namespace Tree void addChildren(Iter begin, Iter end) { std::copy( begin, end, std::back_inserter( children ) );} + /** + * @brief prepends a container of nodes to the list of children + * + * @param begin - iterator to the beginning of the container of objects of type std::shared_ptr + * @param end - iterator to the end of the container of objects of type std::shared_ptr + */ + template + void prependChildren(Iter begin, Iter end) { std::copy( begin, end, std::front_inserter( children ) );} + /** * @brief finds the next node in the tree of a given type * @@ -202,12 +225,25 @@ namespace Tree /** * @brief finds all nodes of a given base * - * @param base - the base type of the node as a string + * @param b - the base type of the node as a string * @param c - a container for holding all found nodes, has to be holding objects of type std::shared_ptr */ - template - void findAllBase(const std::string &base, Container &c); + template + void findAllBase(const std::string &b, Container &c) + { + std::list>::iterator it; + if (base() == b) + { + c.push_back(shared_from_this()); + } + + for(it = children.begin(); it != children.end(); ++it) + { + (*it)->findAllBase(b,c); + } + + } /** * @brief checks if the given node is a direct child of this node