self_list.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*************************************************************************/
  2. /* self_list.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef SELF_LIST_H
  30. #define SELF_LIST_H
  31. #include "typedefs.h"
  32. template<class T>
  33. class SelfList {
  34. public:
  35. class List {
  36. SelfList<T> *_first;
  37. public:
  38. void add(SelfList<T> *p_elem) {
  39. ERR_FAIL_COND(p_elem->_root);
  40. p_elem->_root=this;
  41. p_elem->_next=_first;
  42. p_elem->_prev=NULL;
  43. if (_first)
  44. _first->_prev=p_elem;
  45. _first=p_elem;
  46. }
  47. void remove(SelfList<T> *p_elem) {
  48. ERR_FAIL_COND(p_elem->_root!=this);
  49. if (p_elem->_next) {
  50. p_elem->_next->_prev=p_elem->_prev;
  51. }
  52. if (p_elem->_prev) {
  53. p_elem->_prev->_next=p_elem->_next;
  54. }
  55. if (_first==p_elem) {
  56. _first=p_elem->_next;
  57. }
  58. p_elem->_next=NULL;
  59. p_elem->_prev=NULL;
  60. p_elem->_root=NULL;
  61. }
  62. _FORCE_INLINE_ SelfList<T> *first() { return _first; }
  63. _FORCE_INLINE_ const SelfList<T> *first() const { return _first; }
  64. _FORCE_INLINE_ List() { _first=NULL; }
  65. _FORCE_INLINE_ ~List() { ERR_FAIL_COND(_first!=NULL); }
  66. };
  67. private:
  68. List *_root;
  69. T *_self;
  70. SelfList<T> *_next;
  71. SelfList<T> *_prev;
  72. public:
  73. _FORCE_INLINE_ bool in_list() const { return _root; }
  74. _FORCE_INLINE_ SelfList<T> *next() { return _next; }
  75. _FORCE_INLINE_ SelfList<T> *prev() { return _prev; }
  76. _FORCE_INLINE_ const SelfList<T> *next() const { return _next; }
  77. _FORCE_INLINE_ const SelfList<T> *prev() const { return _prev; }
  78. _FORCE_INLINE_ T*self() const { return _self; }
  79. _FORCE_INLINE_ SelfList(T *p_self) {
  80. _self=p_self;
  81. _next=NULL;
  82. _prev=NULL;
  83. _root=NULL;
  84. }
  85. _FORCE_INLINE_ ~SelfList() {
  86. if (_root)
  87. _root->remove(this);
  88. }
  89. };
  90. #endif // SELF_LIST_H