self_list.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*************************************************************************/
  2. /* self_list.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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/typedefs.h"
  33. template <class T>
  34. class SelfList {
  35. public:
  36. class List {
  37. SelfList<T> *_first;
  38. SelfList<T> *_last;
  39. public:
  40. void add(SelfList<T> *p_elem) {
  41. ERR_FAIL_COND(p_elem->_root);
  42. p_elem->_root = this;
  43. p_elem->_next = _first;
  44. p_elem->_prev = NULL;
  45. if (_first) {
  46. _first->_prev = p_elem;
  47. } else {
  48. _last = p_elem;
  49. }
  50. _first = p_elem;
  51. }
  52. void add_last(SelfList<T> *p_elem) {
  53. ERR_FAIL_COND(p_elem->_root);
  54. p_elem->_root = this;
  55. p_elem->_next = NULL;
  56. p_elem->_prev = _last;
  57. if (_last) {
  58. _last->_next = p_elem;
  59. } else {
  60. _first = p_elem;
  61. }
  62. _last = p_elem;
  63. }
  64. void remove(SelfList<T> *p_elem) {
  65. ERR_FAIL_COND(p_elem->_root != this);
  66. if (p_elem->_next) {
  67. p_elem->_next->_prev = p_elem->_prev;
  68. }
  69. if (p_elem->_prev) {
  70. p_elem->_prev->_next = p_elem->_next;
  71. }
  72. if (_first == p_elem) {
  73. _first = p_elem->_next;
  74. }
  75. if (_last == p_elem) {
  76. _last = p_elem->_prev;
  77. }
  78. p_elem->_next = NULL;
  79. p_elem->_prev = NULL;
  80. p_elem->_root = NULL;
  81. }
  82. _FORCE_INLINE_ SelfList<T> *first() { return _first; }
  83. _FORCE_INLINE_ const SelfList<T> *first() const { return _first; }
  84. _FORCE_INLINE_ List() {
  85. _first = NULL;
  86. _last = NULL;
  87. }
  88. _FORCE_INLINE_ ~List() { ERR_FAIL_COND(_first != NULL); }
  89. };
  90. private:
  91. List *_root;
  92. T *_self;
  93. SelfList<T> *_next;
  94. SelfList<T> *_prev;
  95. public:
  96. _FORCE_INLINE_ bool in_list() const { return _root; }
  97. _FORCE_INLINE_ SelfList<T> *next() { return _next; }
  98. _FORCE_INLINE_ SelfList<T> *prev() { return _prev; }
  99. _FORCE_INLINE_ const SelfList<T> *next() const { return _next; }
  100. _FORCE_INLINE_ const SelfList<T> *prev() const { return _prev; }
  101. _FORCE_INLINE_ T *self() const { return _self; }
  102. _FORCE_INLINE_ SelfList(T *p_self) {
  103. _self = p_self;
  104. _next = NULL;
  105. _prev = NULL;
  106. _root = NULL;
  107. }
  108. _FORCE_INLINE_ ~SelfList() {
  109. if (_root)
  110. _root->remove(this);
  111. }
  112. };
  113. #endif // SELF_LIST_H