Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class node :
- public std::enable_shared_from_this<node>
- {
- public:
- using ptr = std::shared_ptr<node>;
- using cref = const ptr &;
- void add_child(cref n)
- {
- if(std::find(childs.begin(), childs.end(), n) != childs.end()) return;
- auto par = n->parent.lock();
- if(par)
- par->remove_child(n);
- n->parent = shared_from_this();
- childs.push_back(n);
- }
- void remove_child(cref n)
- {
- if(n)
- {
- childs.erase(std::remove(childs.begin(), childs.end(), n), childs.end());
- n->parent.reset();
- }
- }
- ptr get_child(size_t n)
- {
- return childs.at(n);
- }
- void set_parent(cref n)
- {
- auto par = parent.lock();
- if (par == n || !n)
- return;
- n->add_child(shared_from_this());
- }
- private:
- std::weak_ptr<node> parent;
- std::vector<ptr> childs;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement