self_list.hpp 4.3 KB

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