Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cList.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cList_H
7 #define _INC_cList_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "GrayCore.h"
13 #include "HResult.h"
14 #include "cUnitTestDecl.h"
15 #include "cDebugAssert.h"
16 #include "cHeapObject.h"
17 
18 namespace Gray
19 {
20  class cListBase;
21 
23  {
30  friend class cListBase; // so m_pNext and m_pPrev can be manipulated directly.
31 
32  private:
33  cListBase* m_pParent;
34  cListNodeBase* m_pNext;
35  cListNodeBase* m_pPrev;
36 
37  protected:
38  virtual void put_Parent(cListBase* pParent)
39  {
41  ASSERT(m_pParent == nullptr || pParent == nullptr || m_pParent == pParent);
42  m_pParent = pParent; // link me back to my parent object.
43  }
44 
45  protected:
46  cListNodeBase() noexcept // always just a base class.
47  : m_pParent(nullptr) // not linked yet.
48  , m_pNext(nullptr)
49  , m_pPrev(nullptr)
50  {
51  }
52 
53  public:
54  virtual ~cListNodeBase()
55  {
57  ASSERT(!hasParent());
58  }
59 
60  cListBase* get_Parent() const noexcept
61  {
62  return m_pParent;
63  }
64  cListNodeBase* get_Next() const noexcept
65  {
66  return m_pNext;
67  }
68  cListNodeBase* get_Prev() const noexcept
69  {
70  return m_pPrev;
71  }
72 
73  bool hasParent() const noexcept
74  {
76  if (m_pParent != nullptr)
77  {
78  return true;
79  }
80  // If i have no parent i shouldnt have any siblings either.
81  DEBUG_CHECK(m_pNext == nullptr && m_pPrev == nullptr);
82  return false;
83  }
84 
87  void RemoveFromParent();
88 
89  virtual HRESULT DisposeThis() // delete myself from the system.
90  {
93  RemoveFromParent(); // called before destruct so virtual RemoveListNode() is called correctly.
94  return S_OK;
95  }
96  };
97 
98  //*************************************************
99 
100  class GRAYCORE_LINK cListBase // generic list of objects based on cListNodeBase.
101  {
107 
108  friend class cListNodeBase; // so it can call RemoveListNode() to remove self.
109 
110  protected:
112  private:
113  cListNodeBase* m_pHead;
114  cListNodeBase* m_pTail;
115 
116  protected:
119  virtual void RemoveListNode(cListNodeBase* pNode);
120 
121  public:
122  cListBase() noexcept
123  : m_iCount(0)
124  , m_pHead(nullptr)
125  , m_pTail(nullptr)
126  {
127  }
128  virtual ~cListBase()
129  {
133  ASSERT(isEmpty());
134  }
135 
138  virtual void InsertListNode(cListNodeBase* pNodeNew, cListNodeBase* pNodePrev = nullptr);
139  void InsertList(cListBase* pListSrc, cListNodeBase* pNodePrev = nullptr);
140 
141  void InsertBefore(cListNodeBase* pNodeNew, const cListNodeBase* pNodeNext)
142  {
144  InsertListNode(pNodeNew, (pNodeNext != nullptr) ? (pNodeNext->get_Prev()) : get_Tail());
145  }
146  void InsertHead(cListNodeBase* pNodeNew)
147  {
148  InsertListNode(pNodeNew, nullptr);
149  }
150  void InsertTail(cListNodeBase* pNodeNew)
151  {
152  InsertListNode(pNodeNew, get_Tail());
153  }
154 
155  void DisposeAll();
156  void Empty();
157 
158  cListNodeBase* get_Head(void) const noexcept
159  {
160  return m_pHead;
161  }
162  cListNodeBase* get_Tail(void) const noexcept
163  {
164  return m_pTail;
165  }
166  ITERATE_t get_Count() const noexcept
167  {
168  return m_iCount;
169  }
170  bool isEmpty() const noexcept
171  {
172  return get_Count() == 0;
173  }
174 
176  cListNodeBase* GetAt(ITERATE_t index) const;
177 
178  bool IsMyChild(const cListNodeBase* pNode) const noexcept
179  {
180  if (pNode == nullptr)
181  return false;
182  return pNode->get_Parent() == this;
183  }
184 
186  };
187 
188  //*************************************************
189 
191  {
193  if (m_pParent != nullptr)
194  {
195  m_pParent->RemoveListNode(this); // only call this from here !
196  // ASSERT( m_pParent != pParentPrev ); // We are now unlinked. (or deleted)
197  }
198  }
199 
200  //*************************************************
201  // template type casting for lists.
202 
203  template<class _TYPE_REC = cListNodeBase>
204  class cListNodeT : public cListNodeBase
205  {
208  typedef cListNodeBase SUPER_t;
209  public:
210  _TYPE_REC* get_Next() const
211  {
213  return static_cast<_TYPE_REC*>(SUPER_t::get_Next());
214  }
215  _TYPE_REC* get_Prev() const
216  {
218  return static_cast<_TYPE_REC*>(SUPER_t::get_Prev());
219  }
220  };
221 
222  template<class _TYPE_REC /* = cListNodeBase */ >
223  class cListT : public cListBase
224  {
228  typedef cListBase SUPER_t;
230  public:
231  _TYPE_REC* GetAt(ITERATE_t index) const
232  {
234  return static_cast<_TYPE_REC*>(SUPER_t::GetAt(index));
235  }
236  _TYPE_REC* get_Head() const
237  {
238  return static_cast<_TYPE_REC*>(SUPER_t::get_Head());
239  }
240  _TYPE_REC* get_Tail() const
241  {
242  return static_cast<_TYPE_REC*>(SUPER_t::get_Tail());
243  }
244  };
245 }
246 #endif // _INC_cList_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cHeapObject.h:38
Definition: cList.h:101
cListNodeBase * get_Head(void) const noexcept
Definition: cList.h:158
virtual void RemoveListNode(cListNodeBase *pNode)
allow Override of this. called when child pObRec removed from list.
Definition: cList.cpp:52
bool IsMyChild(const cListNodeBase *pNode) const noexcept
Definition: cList.h:178
ITERATE_t get_Count() const noexcept
Definition: cList.h:166
virtual ~cListBase()
Definition: cList.h:128
cListBase() noexcept
Definition: cList.h:122
void InsertBefore(cListNodeBase *pNodeNew, const cListNodeBase *pNodeNext)
Definition: cList.h:141
bool isEmpty() const noexcept
Definition: cList.h:170
void InsertTail(cListNodeBase *pNodeNew)
Definition: cList.h:150
cListNodeBase * get_Tail(void) const noexcept
Definition: cList.h:162
void InsertHead(cListNodeBase *pNodeNew)
Definition: cList.h:146
cListNodeBase * GetAt(ITERATE_t index) const
iterate the linked list.
Definition: cList.cpp:156
ITERATE_t m_iCount
how many children? nice to get read only direct access to this for scripting.
Definition: cList.h:111
Definition: cList.h:23
cListBase * get_Parent() const noexcept
Definition: cList.h:60
cListNodeBase * get_Next() const noexcept
Definition: cList.h:64
virtual ~cListNodeBase()
Definition: cList.h:54
cListNodeBase * get_Prev() const noexcept
Definition: cList.h:68
bool hasParent() const noexcept
Definition: cList.h:73
virtual HRESULT DisposeThis()
Definition: cList.h:89
cListNodeBase() noexcept
Definition: cList.h:46
void RemoveFromParent()
Definition: cList.h:190
virtual void put_Parent(cListBase *pParent)
Definition: cList.h:38
Definition: cList.h:205
_TYPE_REC * get_Next() const
Definition: cList.h:210
_TYPE_REC * get_Prev() const
Definition: cList.h:215
Definition: cList.h:224
_TYPE_REC * GetAt(ITERATE_t index) const
Definition: cList.h:231
_TYPE_REC * get_Head() const
Definition: cList.h:236
_TYPE_REC * get_Tail() const
Definition: cList.h:240
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
int ITERATE_t
like size_t but signed
Definition: Index.h:28
uint16 index
Definition: sample3.cpp:29