Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_inherence_object.hpp
1#pragma once
2
3#include <vector>
4
5namespace spk
6{
20 template <typename TType>
21 class InherenceObject
22 {
23
24 private:
25 TType *_parent = nullptr;
26 std::vector<TType *> _children;
27
28 public:
29 InherenceObject()
30 {
31 static_assert(std::is_base_of_v<InherenceObject<TType>, TType>, "TType must inherit from InherenceObject<TType>");
32 }
33
34 virtual ~InherenceObject()
35 {
36 if (_parent != nullptr)
37 {
38 _parent->removeChild(static_cast<TType *>(this));
39 }
40 }
41
46 virtual void addChild(TType *p_child)
47 {
48 if (p_child == nullptr || p_child == static_cast<TType *>(this))
49 {
50 return;
51 }
52
53 if (p_child->_parent != nullptr)
54 {
55 p_child->_parent->removeChild(p_child);
56 }
57
58 p_child->_parent = static_cast<TType *>(this);
59 _children.emplace_back(p_child);
60 }
61
66 virtual void removeChild(TType *p_child)
67 {
68 if (p_child == nullptr)
69 {
70 return;
71 }
72
73 auto it = std::find(_children.begin(), _children.end(), p_child);
74 if (it != _children.end())
75 {
76 _children.erase(it);
77 p_child->_parent = nullptr;
78 }
79 }
80
85 TType *parent() const
86 {
87 return _parent;
88 }
89
94 virtual std::vector<TType *> &children()
95 {
96 return _children;
97 }
98
103 virtual const std::vector<TType *> &children() const
104 {
105 return _children;
106 }
107
112 {
113 for (TType *child : _children)
114 {
115 child->_parent = nullptr;
116 }
117 _children.clear();
118 }
119 };
120}
TType * parent() const
Returns the parent pointer.
Definition spk_inherence_object.hpp:85
virtual std::vector< TType * > & children()
Returns the children vector.
Definition spk_inherence_object.hpp:94
virtual const std::vector< TType * > & children() const
Returns the children vector.
Definition spk_inherence_object.hpp:103
virtual void addChild(TType *p_child)
Adds a child to this node, handling reparenting as needed.
Definition spk_inherence_object.hpp:46
virtual void removeChild(TType *p_child)
Removes a child from this node.
Definition spk_inherence_object.hpp:66
void clearChildren()
Clears the children list and resets each child's parent pointer.
Definition spk_inherence_object.hpp:111