self_list.h 4.0 KB

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