Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_linklist.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_LINKLIST_H
7 #define TB_LINKLIST_H
8 
9 #include "tb_core.h"
10 #include <assert.h>
11 
12 namespace tb {
13 
14 class TBLinkList;
15 class TBLink;
16 
26 {
27 public:
29  TBLinkListIterator(TBLinkList *linklist, TBLink *current_link, bool forward);
31 
34  void Reset();
35 
37  TBLink *Get() const { return m_current_link; }
38 
40  TBLink *GetAndStep();
41 
42  operator TBLink *() const { return m_current_link; }
43 
44  const TBLinkListIterator& operator = (const TBLinkListIterator &iter);
45 private:
46  TBLinkList *m_linklist;
47  TBLink *m_current_link;
48  bool m_forward;
49 
50  TBLinkListIterator *m_prev;
51  TBLinkListIterator *m_next;
52 
55  void RemoveLink(TBLink *link);
56  friend class TBLinkList;
57 
59  void Register();
60 
62  void Unregister();
63  void UnregisterAndClear();
64 };
65 
69 class TBLink
70 {
71 public:
72  TBLink() : prev(nullptr), next(nullptr), linklist(nullptr) {}
73 
75  bool IsInList() const { return linklist ? true : false; }
76 public:
77  TBLink *prev;
78  TBLink *next;
79  TBLinkList *linklist;
80 };
81 
82 template<class T>
83 class TBLinkOf : public TBLink
84 {
85 public:
86  inline T *GetPrev() const { return (T *) prev; }
87  inline T *GetNext() const { return (T *) next; }
88 };
89 
94 {
95 public:
96  TBLinkList() : first(nullptr), last(nullptr), first_iterator(nullptr) {}
97  ~TBLinkList();
98 
99  void Remove(TBLink *link);
100  void RemoveAll();
101 
102  void AddFirst(TBLink *link);
103  void AddLast(TBLink *link);
104 
105  void AddBefore(TBLink *link, TBLink *reference);
106  void AddAfter(TBLink *link, TBLink *reference);
107 
108  bool ContainsLink(TBLink *link) const { return link->linklist == this; }
109 
110  bool HasLinks() const { return first ? true : false; }
111 
112  int CountLinks() const;
113 public:
114  TBLink *first;
115  TBLink *last;
116  TBLinkListIterator *first_iterator;
117 };
118 
121 template<class T>
123 {
124 public:
126  void Remove(T *link) { m_linklist.Remove(static_cast<TBLinkOf<T>*>(link)); }
127 
129  void Delete(T *link) { m_linklist.Remove(static_cast<TBLinkOf<T>*>(link)); delete link; }
130 
132  void RemoveAll() { m_linklist.RemoveAll(); }
133 
135  void DeleteAll() { while (T *t = GetFirst()) Delete(t); }
136 
138  void AddFirst(T *link) { m_linklist.AddFirst(static_cast<TBLinkOf<T>*>(link)); }
139 
141  void AddLast(T *link) { m_linklist.AddLast(static_cast<TBLinkOf<T>*>(link)); }
142 
144  void AddBefore(T *link, T *reference) { m_linklist.AddBefore(static_cast<TBLinkOf<T>*>(link), reference); }
145 
147  void AddAfter(T *link, T *reference) { m_linklist.AddAfter(static_cast<TBLinkOf<T>*>(link), reference); }
148 
150  bool ContainsLink(T *link) const { return m_linklist.ContainsLink(static_cast<TBLinkOf<T>*>(link)); }
151 
153  T *GetFirst() const { return (T *) static_cast<TBLinkOf<T>*>(m_linklist.first); }
154 
156  T *GetLast() const { return (T *) static_cast<TBLinkOf<T>*>(m_linklist.last); }
157 
159  bool HasLinks() const { return m_linklist.HasLinks(); }
160 
162  int CountLinks() const { return m_linklist.CountLinks(); }
163 
166  {
167  public:
168  Iterator(TBLinkListOf<T> *linklistof, bool forward)
169  : TBLinkListIterator(&linklistof->m_linklist, forward ? linklistof->m_linklist.first : linklistof->m_linklist.last, forward) {}
170  Iterator(TBLinkListOf<T> *linklistof, T *link, bool forward) : TBLinkListIterator(&linklistof->m_linklist, link, forward) {}
171  inline T *Get() const { return (T *) static_cast<TBLinkOf<T>*>(TBLinkListIterator::Get()); }
172  inline T *GetAndStep() { return (T *) static_cast<TBLinkOf<T>*>(TBLinkListIterator::GetAndStep()); }
173  inline operator T *() const { return (T *) static_cast<TBLinkOf<T>*>(Get()); }
174  };
175 
177  Iterator IterateForward() { return Iterator(this, true); }
178 
180  Iterator IterateForward(T *link) { return Iterator(this, link, true); }
181 
183  Iterator IterateBackward() { return Iterator(this, false); }
184 
186  Iterator IterateBackward(T *link) { return Iterator(this, link, false); }
187 private:
188  TBLinkList m_linklist;
189 };
190 
193 template<class T>
195 {
196 public:
198 };
199 
200 } // namespace tb
201 
202 #endif // TB_LINKLIST_H