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