Horizon Official Technical Documentation
Element.hpp
Go to the documentation of this file.
1
2
3#ifndef HORIZON_CORE_STRUCTURES_LINKEDLIST_ELEMENT_HPP
4#define HORIZON_CORE_STRUCTURES_LINKEDLIST_ELEMENT_HPP
5
6namespace Horizon
7{
8namespace Structures
9{
10namespace LinkedList
11{
18{
19private:
20 friend class Head;
21
24
25public:
26 Element() : _next(nullptr), _prev(nullptr) { }
27
29 bool has_next() const { return (_next && _next->_next != nullptr); }
31 bool has_prev() const { return (_prev && _prev->_prev != nullptr); }
33 bool is_in_list() const { return (_next != nullptr && _prev != nullptr); }
34
36 Element *next() { return has_next() ? _next : nullptr; }
37 Element const *next() const { return has_next() ? _next : nullptr; }
39 Element *prev() { return has_prev() ? _prev : nullptr; }
40 Element const *prev() const { return has_prev() ? _prev : nullptr; }
41
43 Element *nocheck_next() { return _next; }
44 Element const *nocheck_next() const { return _next; }
46 Element *nocheck_prev() { return _prev; }
47 Element const *nocheck_prev() const { return _prev; }
48
50 void delink()
51 {
52 if (!is_in_list())
53 return;
54
55 _next->_prev = _prev;
56 _prev->_next = _next;
57 _next = nullptr;
58 _prev = nullptr;
59 }
60
64 void push_before(Element* pElem)
65 {
66 pElem->_next = this;
67 pElem->_prev = _prev;
68 _prev->_next = pElem;
69 _prev = pElem;
70 }
71
75 void push_after(Element* pElem)
76 {
77 pElem->_prev = this;
78 pElem->_next = _next;
79 _next->_prev = pElem;
80 _next = pElem;
81 }
82
83private:
84 // disable copy and assignment because we don't want to copy Elements and we don't want to assign Elements to each other.
85 // This is a linked list, not a copyable list because it is used to manage a list of objects that are not copyable.
86 // It should be noted that copying the objects could result in unexpected behavior.
87 Element(Element const&) = delete;
88 Element& operator=(Element const&) = delete;
89
90protected:
92 {
93 delink();
94 }
95};
96}
97}
98}
99
100#endif /* HORIZON_CORE_STRUCTURES_HPP */
This class is used to manage a linked list of Elements. It is not intended to be used directly....
Definition: Element.hpp:18
void push_after(Element *pElem)
Pushes this Element after the given Element.
Definition: Element.hpp:75
Element const * next() const
Definition: Element.hpp:37
Element * _next
Definition: Element.hpp:22
Element const * prev() const
Definition: Element.hpp:40
Element * next()
Returns the next Element in the list.
Definition: Element.hpp:36
~Element()
Definition: Element.hpp:91
Element const * nocheck_next() const
Definition: Element.hpp:44
void delink()
Removes this Element from the list.
Definition: Element.hpp:50
Element const * nocheck_prev() const
Definition: Element.hpp:47
void push_before(Element *pElem)
Pushes this Element before the given Element.
Definition: Element.hpp:64
bool is_in_list() const
Returns true if this Element is in a list.
Definition: Element.hpp:33
Element & operator=(Element const &)=delete
Element * nocheck_prev()
Returns the previous Element in the list without checking if it exists.
Definition: Element.hpp:46
Element * prev()
Returns the previous Element in the list.
Definition: Element.hpp:39
bool has_next() const
Returns true if this Element has a next Element in the list.
Definition: Element.hpp:29
Element * _prev
Definition: Element.hpp:23
bool has_prev() const
Returns true if this Element has a previous Element in the list.
Definition: Element.hpp:31
Element * nocheck_next()
Returns the next Element in the list without checking if it exists.
Definition: Element.hpp:43
Element()
Definition: Element.hpp:26
LinkedList Head class. This class is used to manage a linked list of Elements. It is used as a base c...
Definition: Head.hpp:26
Definition: Element.hpp:7