self_list.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef SELF_LIST_H
  31. #define SELF_LIST_H
  32. #include "core/error_macros.h"
  33. #include "core/typedefs.h"
  34. template <class T>
  35. class SelfList {
  36. public:
  37. class List {
  38. SelfList<T> *_first;
  39. SelfList<T> *_last;
  40. public:
  41. void add(SelfList<T> *p_elem) {
  42. ERR_FAIL_COND(p_elem->_root);
  43. p_elem->_root = this;
  44. p_elem->_next = _first;
  45. p_elem->_prev = nullptr;
  46. if (_first) {
  47. _first->_prev = p_elem;
  48. } else {
  49. _last = p_elem;
  50. }
  51. _first = p_elem;
  52. }
  53. void add_last(SelfList<T> *p_elem) {
  54. ERR_FAIL_COND(p_elem->_root);
  55. p_elem->_root = this;
  56. p_elem->_next = nullptr;
  57. p_elem->_prev = _last;
  58. if (_last) {
  59. _last->_next = p_elem;
  60. } else {
  61. _first = p_elem;
  62. }
  63. _last = p_elem;
  64. }
  65. void remove(SelfList<T> *p_elem) {
  66. ERR_FAIL_COND(p_elem->_root != this);
  67. if (p_elem->_next) {
  68. p_elem->_next->_prev = p_elem->_prev;
  69. }
  70. if (p_elem->_prev) {
  71. p_elem->_prev->_next = p_elem->_next;
  72. }
  73. if (_first == p_elem) {
  74. _first = p_elem->_next;
  75. }
  76. if (_last == p_elem) {
  77. _last = p_elem->_prev;
  78. }
  79. p_elem->_next = nullptr;
  80. p_elem->_prev = nullptr;
  81. p_elem->_root = nullptr;
  82. }
  83. _FORCE_INLINE_ SelfList<T> *first() { return _first; }
  84. _FORCE_INLINE_ const SelfList<T> *first() const { return _first; }
  85. _FORCE_INLINE_ List() {
  86. _first = nullptr;
  87. _last = nullptr;
  88. }
  89. _FORCE_INLINE_ ~List() { ERR_FAIL_COND(_first != nullptr); }
  90. };
  91. private:
  92. List *_root;
  93. T *_self;
  94. SelfList<T> *_next;
  95. SelfList<T> *_prev;
  96. public:
  97. _FORCE_INLINE_ bool in_list() const { return _root; }
  98. _FORCE_INLINE_ void remove_from_list() {
  99. if (_root) {
  100. _root->remove(this);
  101. }
  102. }
  103. _FORCE_INLINE_ SelfList<T> *next() { return _next; }
  104. _FORCE_INLINE_ SelfList<T> *prev() { return _prev; }
  105. _FORCE_INLINE_ const SelfList<T> *next() const { return _next; }
  106. _FORCE_INLINE_ const SelfList<T> *prev() const { return _prev; }
  107. _FORCE_INLINE_ T *self() const { return _self; }
  108. _FORCE_INLINE_ SelfList(T *p_self) {
  109. _self = p_self;
  110. _next = nullptr;
  111. _prev = nullptr;
  112. _root = nullptr;
  113. }
  114. _FORCE_INLINE_ ~SelfList() {
  115. if (_root) {
  116. _root->remove(this);
  117. }
  118. }
  119. };
  120. #endif // SELF_LIST_H