ionTreeNode.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <set>
5 #include <type_traits>
6 
7 
17 template <typename Implementation>
18 class ITreeNode
19 {
20 
21 public:
22 
23  Implementation const * const GetParent() const
24  {
25  return Parent;
26  }
27 
28  std::set<Implementation *> const & GetChildren() const
29  {
30  return Children;
31  }
32 
33  void AddChild(Implementation * child)
34  {
35  child->SetParent(static_cast<Implementation *>(this));
36  }
37 
38  void RemoveChild(Implementation * child)
39  {
40  if (child->Parent == this)
41  child->SetParent(0);
42  }
43 
44  void SetParent(Implementation * parent)
45  {
46  if (Parent)
47  static_cast<ITreeNode<Implementation> *>(Parent)->Children.erase(static_cast<Implementation *>(this));
48 
49  Parent = parent;
50 
51  if (Parent)
52  static_cast<ITreeNode<Implementation> *>(parent)->Children.insert(static_cast<Implementation *>(this));
53  }
54 
56  {
57  for (auto & Child : Children)
58  Child->Parent = 0;
59  Children.clear();
60  }
61 
62  void Remove()
63  {
64  delete this;
65  }
66 
67  template <typename Return>
68  void RecurseOnChildren(Return (Implementation::* Function)())
69  {
70  for (auto Child : Children)
71  (Child->*Function)();
72  }
73 
74  template <typename Return, typename P1>
75  void RecurseOnChildren(Return (Implementation::* Function)(P1), P1 p1)
76  {
77  for (auto Child : Children)
78  (Child->*Function)(p1);
79  }
80 
81  template <typename Return, typename P1, typename P2>
82  void RecurseOnChildren(Return (Implementation::* Function)(P1, P2), P1 p1, P2 p2)
83  {
84  for (auto Child : Children)
85  (Child->*Function)(p1, p2);
86  }
87 
88  virtual ~ITreeNode()
89  {
90  SetParent(0);
91  }
92 
93 protected:
94 
96  : Parent()
97  {}
98 
99  Implementation * Parent;
100  std::set<Implementation *> Children;
101 
102 };
103 
104 
109 template <typename Implementation>
111 {
112 
113 public:
114 
115  std::set<Implementation *> const & GetChildren() const
116  {
117  return Children;
118  }
119 
120  void AddChild(Implementation * Child)
121  {
122  Children.insert(static_cast<Implementation *>(Child));
123  }
124 
125  void RemoveChild(Implementation * Child)
126  {
127  Children.erase(static_cast<Implementation *>(Child));
128  }
129 
131  {
132  Children.clear();
133  }
134 
135  template <typename Return>
136  void RecurseOnChildren(Return (Implementation::* Function)())
137  {
138  for (auto Child : Children)
139  (Child->*Function)();
140  }
141 
142  template <typename Return, typename P1>
143  void RecurseOnChildren(Return (Implementation::* Function)(P1), P1 p1)
144  {
145  for (auto Child : Children)
146  (Child->*Function)(p1);
147  }
148 
149  template <typename Return, typename P1, typename P2>
150  void RecurseOnChildren(Return (Implementation::* Function)(P1, P2), P1 p1, P2 p2)
151  {
152  for (auto Child : Children)
153  (Child->*Function)(p1, p2);
154  }
155 
156 protected:
157 
159  {}
160 
161  std::set<Implementation *> Children;
162 
163 };
Interface for a Node in a generic tree structure.Note that a static_cast to TreeNode<Implementation> ...
Definition: ionTreeNode.h:18
void RecurseOnChildren(Return(Implementation::*Function)(P1), P1 p1)
Definition: ionTreeNode.h:75
std::set< Implementation * > Children
Definition: ionTreeNode.h:161
void RecurseOnChildren(Return(Implementation::*Function)(P1, P2), P1 p1, P2 p2)
Definition: ionTreeNode.h:150
Implementation * Parent
Definition: ionTreeNode.h:99
ITreeNode()
Definition: ionTreeNode.h:95
void RemoveAllChildren()
Definition: ionTreeNode.h:130
void RemoveChild(Implementation *child)
Definition: ionTreeNode.h:38
void RemoveAllChildren()
Definition: ionTreeNode.h:55
void RecurseOnChildren(Return(Implementation::*Function)())
Definition: ionTreeNode.h:68
Implementation const *const GetParent() const
Definition: ionTreeNode.h:23
virtual ~ITreeNode()
Definition: ionTreeNode.h:88
void RecurseOnChildren(Return(Implementation::*Function)())
Definition: ionTreeNode.h:136
IMultiTreeNode()
Definition: ionTreeNode.h:158
void AddChild(Implementation *Child)
Definition: ionTreeNode.h:120
void RemoveChild(Implementation *Child)
Definition: ionTreeNode.h:125
std::set< Implementation * > const & GetChildren() const
Definition: ionTreeNode.h:115
void Remove()
Definition: ionTreeNode.h:62
void SetParent(Implementation *parent)
Definition: ionTreeNode.h:44
std::set< Implementation * > const & GetChildren() const
Definition: ionTreeNode.h:28
std::set< Implementation * > Children
Definition: ionTreeNode.h:100
Variant of ITreeNode that doesn&#39;t require a single parent relationship.
Definition: ionTreeNode.h:110
void AddChild(Implementation *child)
Definition: ionTreeNode.h:33
void RecurseOnChildren(Return(Implementation::*Function)(P1), P1 p1)
Definition: ionTreeNode.h:143
void RecurseOnChildren(Return(Implementation::*Function)(P1, P2), P1 p1, P2 p2)
Definition: ionTreeNode.h:82