self_list.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*************************************************************************/
  2. /* self_list.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 remove(SelfList<T> *p_elem) {
  49. ERR_FAIL_COND(p_elem->_root != this);
  50. if (p_elem->_next) {
  51. p_elem->_next->_prev = p_elem->_prev;
  52. }
  53. if (p_elem->_prev) {
  54. p_elem->_prev->_next = p_elem->_next;
  55. }
  56. if (_first == p_elem) {
  57. _first = p_elem->_next;
  58. }
  59. p_elem->_next = NULL;
  60. p_elem->_prev = NULL;
  61. p_elem->_root = NULL;
  62. }
  63. _FORCE_INLINE_ SelfList<T> *first() { return _first; }
  64. _FORCE_INLINE_ const SelfList<T> *first() const { return _first; }
  65. _FORCE_INLINE_ List() { _first = NULL; }
  66. _FORCE_INLINE_ ~List() { ERR_FAIL_COND(_first != NULL); }
  67. };
  68. private:
  69. List *_root;
  70. T *_self;
  71. SelfList<T> *_next;
  72. SelfList<T> *_prev;
  73. public:
  74. _FORCE_INLINE_ bool in_list() const { return _root; }
  75. _FORCE_INLINE_ SelfList<T> *next() { return _next; }
  76. _FORCE_INLINE_ SelfList<T> *prev() { return _prev; }
  77. _FORCE_INLINE_ const SelfList<T> *next() const { return _next; }
  78. _FORCE_INLINE_ const SelfList<T> *prev() const { return _prev; }
  79. _FORCE_INLINE_ T *self() const { return _self; }
  80. _FORCE_INLINE_ SelfList(T *p_self) {
  81. _self = p_self;
  82. _next = NULL;
  83. _prev = NULL;
  84. _root = NULL;
  85. }
  86. _FORCE_INLINE_ ~SelfList() {
  87. if (_root)
  88. _root->remove(this);
  89. }
  90. };
  91. #endif // SELF_LIST_H