1 | /**************************************************************************/ |
2 | /* self_list.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef SELF_LIST_H |
32 | #define SELF_LIST_H |
33 | |
34 | #include "core/error/error_macros.h" |
35 | #include "core/typedefs.h" |
36 | |
37 | template <class T> |
38 | class SelfList { |
39 | public: |
40 | class List { |
41 | SelfList<T> *_first = nullptr; |
42 | SelfList<T> *_last = nullptr; |
43 | |
44 | public: |
45 | void add(SelfList<T> *p_elem) { |
46 | ERR_FAIL_COND(p_elem->_root); |
47 | |
48 | p_elem->_root = this; |
49 | p_elem->_next = _first; |
50 | p_elem->_prev = nullptr; |
51 | |
52 | if (_first) { |
53 | _first->_prev = p_elem; |
54 | |
55 | } else { |
56 | _last = p_elem; |
57 | } |
58 | |
59 | _first = p_elem; |
60 | } |
61 | |
62 | void add_last(SelfList<T> *p_elem) { |
63 | ERR_FAIL_COND(p_elem->_root); |
64 | |
65 | p_elem->_root = this; |
66 | p_elem->_next = nullptr; |
67 | p_elem->_prev = _last; |
68 | |
69 | if (_last) { |
70 | _last->_next = p_elem; |
71 | |
72 | } else { |
73 | _first = p_elem; |
74 | } |
75 | |
76 | _last = p_elem; |
77 | } |
78 | |
79 | void remove(SelfList<T> *p_elem) { |
80 | ERR_FAIL_COND(p_elem->_root != this); |
81 | if (p_elem->_next) { |
82 | p_elem->_next->_prev = p_elem->_prev; |
83 | } |
84 | |
85 | if (p_elem->_prev) { |
86 | p_elem->_prev->_next = p_elem->_next; |
87 | } |
88 | |
89 | if (_first == p_elem) { |
90 | _first = p_elem->_next; |
91 | } |
92 | |
93 | if (_last == p_elem) { |
94 | _last = p_elem->_prev; |
95 | } |
96 | |
97 | p_elem->_next = nullptr; |
98 | p_elem->_prev = nullptr; |
99 | p_elem->_root = nullptr; |
100 | } |
101 | |
102 | void clear() { |
103 | while (_first) { |
104 | remove(_first); |
105 | } |
106 | } |
107 | |
108 | _FORCE_INLINE_ SelfList<T> *first() { return _first; } |
109 | _FORCE_INLINE_ const SelfList<T> *first() const { return _first; } |
110 | |
111 | _FORCE_INLINE_ List() {} |
112 | _FORCE_INLINE_ ~List() { |
113 | // A self list must be empty on destruction. |
114 | DEV_ASSERT(_first == nullptr); |
115 | } |
116 | }; |
117 | |
118 | private: |
119 | List *_root = nullptr; |
120 | T *_self = nullptr; |
121 | SelfList<T> *_next = nullptr; |
122 | SelfList<T> *_prev = nullptr; |
123 | |
124 | public: |
125 | _FORCE_INLINE_ bool in_list() const { return _root; } |
126 | _FORCE_INLINE_ void remove_from_list() { |
127 | if (_root) { |
128 | _root->remove(this); |
129 | } |
130 | } |
131 | _FORCE_INLINE_ SelfList<T> *next() { return _next; } |
132 | _FORCE_INLINE_ SelfList<T> *prev() { return _prev; } |
133 | _FORCE_INLINE_ const SelfList<T> *next() const { return _next; } |
134 | _FORCE_INLINE_ const SelfList<T> *prev() const { return _prev; } |
135 | _FORCE_INLINE_ T *self() const { return _self; } |
136 | |
137 | _FORCE_INLINE_ SelfList(T *p_self) { |
138 | _self = p_self; |
139 | } |
140 | |
141 | _FORCE_INLINE_ ~SelfList() { |
142 | if (_root) { |
143 | _root->remove(this); |
144 | } |
145 | } |
146 | }; |
147 | |
148 | #endif // SELF_LIST_H |
149 | |