Fillwave  10.0.0
TreePtr.h
1 #pragma once
2 
3 /*
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2018 Filip Wasil and Fillwave community members
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <flw/flf/models/base/ITreeNode.h>
25 #include <flw/cmn/Containers.h>
26 #include <algorithm>
27 #include <utility>
28 
29 namespace flw {
30 namespace flf {
31 
40 template <class T, class C = ITreeNode>
41 class TreePtr : public C {
42 public:
43  TreePtr()
44  : mFlagAttachedDetached(true) {
45  // nothing
46  }
47 
48  ~TreePtr() override = default;
49 
50  TreePtr &operator=(const TreePtr &) = delete;
51 
52  TreePtr(const TreePtr &) = delete;
53 
54  TreePtr &operator=(TreePtr &&) = default;
55 
56  TreePtr(TreePtr&& ) = default;
57 
58  void attach(std::unique_ptr<T>&& node) {
59  if (node.get() == this) {
60  /* User just tried to attach tree node to itself */
61  abort();
62  }
63  node->onAttached(this);
64  mChildren.push_back(std::move(node));
65  mFlagAttachedDetached = true;
66  }
67 
68  template <typename TNode, typename ...TArguments>
69  void attachNew(TArguments ...args) {
70  attach(std::make_unique<TNode>(args...));
71  }
72 
73  void detach(T* node) {
74  auto _compare_function = [node](std::unique_ptr<T> const& e) -> bool {
75  bool found = (e.get() == node);
76  if (found) {
77  node->onDetached();
78  }
79  return found;
80  };
81  auto it = std::remove_if(mChildren.begin(), mChildren.end(), _compare_function);
82  if (it != mChildren.end()) {
83  mFlagAttachedDetached = true;
84  mChildren.erase(it, mChildren.end());
85  }
86  }
87 
88  virtual void onAttached(ITreeNode *) {
89  // nothing
90  }
91 
92  virtual void onDetached() {
93  // nothing
94  }
95 
96  void detachChildren() {
97  std::for_each(mChildren.begin(), mChildren.end(), [](std::unique_ptr<T> &e) {
98  e->onDetached();
99  });
100  mChildren.clear();
101  }
102 
103  bool isAttachedDetached() {
104  bool result = mFlagAttachedDetached;
105  mFlagAttachedDetached = false;
106  for (auto &it : mChildren) {
107  result = it->isAttachedDetached() ? true : result;
108  }
109  return result;
110  }
111 
112  bool mFlagAttachedDetached;
113 
114 protected:
115  vec<std::unique_ptr<T>> mChildren;
116 };
117 
118 } /* flf */
119 } /* flw */
Definition: Aliases.h:30
Basic tree ITreeNode Interface.
Definition: ITreeNode.h:31
Basic tree template class. Enables attaching and detaching nodes.
Definition: TreePtr.h:41