Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_inherence_with_priority_object.hpp
1#pragma once
2
3#include "structure/design_pattern/spk_inherence_object.hpp"
4
5#include <algorithm>
6#include <iostream>
7#include <string>
8#include <utility>
9
10namespace spk
11{
12 template <typename TType>
28 {
29 public:
33 using Priority = std::size_t;
34
35 private:
36 Priority _priority = 0;
37
38 protected:
43 {
44 auto &childs = this->children();
45
46 std::stable_sort(childs.begin(), childs.end(), [](const TType *a, const TType *b) {
47 return a->priority() > b->priority();
48 });
49 }
50
55 {
56 if (auto *p = this->parent(); p != nullptr)
57 {
59 }
60 }
61
62 public:
67 {
68 static_assert(
69 std::is_base_of_v<InherenceWithPriorityObject<TType>, TType>,
70 "TType must inherit from InherenceWithPriorityObject<TType> to use priority() sorting.");
71 }
72
77 void addChild(TType *p_child) override
78 {
81 }
82
87 void setPriority(const Priority &p_priority)
88 {
89 if (_priority == p_priority)
90 {
91 return;
92 }
93
94 _priority = p_priority;
96 }
97
102 const Priority &priority() const
103 {
104 return _priority;
105 }
106 };
107}
Provides parent/child ownership links for tree structures.
Definition spk_inherence_object.hpp:22
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 void addChild(TType *p_child)
Adds a child to this node, handling reparenting as needed.
Definition spk_inherence_object.hpp:46
InherenceWithPriorityObject()
Builds a priority-aware inherence object.
Definition spk_inherence_with_priority_object.hpp:66
void addChild(TType *p_child) override
Adds a child and re-sorts by priority.
Definition spk_inherence_with_priority_object.hpp:77
void setPriority(const Priority &p_priority)
Sets this node's priority and refreshes parent ordering.
Definition spk_inherence_with_priority_object.hpp:87
void _sortChildByPriority()
Sorts children by descending priority.
Definition spk_inherence_with_priority_object.hpp:42
std::size_t Priority
Priority value type used for sorting.
Definition spk_inherence_with_priority_object.hpp:33
void _notifyParentPriorityChanged()
Notifies the parent that this node's priority changed.
Definition spk_inherence_with_priority_object.hpp:54
const Priority & priority() const
Returns the current priority value.
Definition spk_inherence_with_priority_object.hpp:102