0024911: Avoid using virtual functions in NCollection classes
[occt.git] / src / NCollection / NCollection_BaseList.hxx
CommitLineData
b311480e 1// Created on: 2002-04-17
2// Created by: Alexander Kartomin (akm)
973c2be1 3// Copyright (c) 2002-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16// Purpose: This is a base class for the List, Set, Queue and Stack
17// collections. It offers operations on abstract lists (of the
18// objects of class NCollection_ListNode).
19// Apart from this class being brand new (in TCollection said
20// collections were independent, only using the same class for
21// node representation), here is an important new feature -
22// the list length is continuously updated, so the method
23// Extent is quite quick.
7fd59977 24
25#ifndef NCollection_BaseList_HeaderFile
26#define NCollection_BaseList_HeaderFile
27
28#include <Standard_NoSuchObject.hxx>
ddf2fe8e 29#include <NCollection_DefineAlloc.hxx>
7fd59977 30#include <NCollection_ListNode.hxx>
31
7fd59977 32typedef void (* NCollection_DelListNode)
33 (NCollection_ListNode*, Handle(NCollection_BaseAllocator)& theAl);
34
35// ********************************************************** BaseList class
36class NCollection_BaseList
37{
ddf2fe8e 38public:
39 //! Memory allocation
40 DEFINE_STANDARD_ALLOC
41 DEFINE_NCOLLECTION_ALLOC
42
43public:
7fd59977 44 class Iterator
45 {
46 public:
47 // ******** Empty constructor
48 Iterator (void) :
49 myCurrent (NULL),
50 myPrevious(NULL) {}
51 // ******** Constructor with initialisation
52 Iterator (const NCollection_BaseList& theList) :
53 myCurrent (theList.myFirst),
54 myPrevious(NULL) {}
55 // ******** Initialisation
56 void Init (const NCollection_BaseList& theList)
57 {
58 myCurrent = theList.myFirst;
59 myPrevious = NULL;
60 }
9991c9c2 61 // ******** Initialisation
62 void Initialize (const NCollection_BaseList& theList)
63 {
64 Init(theList);
65 }
7fd59977 66 // ******** More
67 Standard_Boolean More (void) const
68 { return (myCurrent!=NULL); }
69 // ******** Assignment operator
70 Iterator& operator= (const Iterator& theIt)
71 {
72 if (&theIt != this)
73 {
74 myCurrent = theIt.myCurrent;
75 myPrevious = theIt.myPrevious;
76 }
77 return * this;
78 }
79//skt----------------------------------------------------
80 // ******** Comparison operator
79a35943 81 Standard_Boolean operator== (const Iterator& theIt) const
7fd59977 82 {
83 return myCurrent == theIt.myCurrent;
84 }
85//-------------------------------------------------------
79a35943 86 //! Performs comparison of two iterators
ddf2fe8e 87 Standard_Boolean IsEqual (const Iterator& theOther) const
79a35943 88 {
89 return *this == theOther;
90 }
7fd59977 91 protected:
92 void Init (const NCollection_BaseList& theList,
93 NCollection_ListNode * const thePrev)
94 {
95 myCurrent = thePrev ? thePrev -> Next() :
96 (NCollection_ListNode *)theList.PLast();
97 myPrevious = thePrev;
98 }
99 public:
100 NCollection_ListNode * myCurrent; // Pointer to the current node
101 NCollection_ListNode * myPrevious;// Pointer to the previous one
102 friend class NCollection_BaseList;
103 }; // End of nested class Iterator
104
105 public:
106 // ---------- PUBLIC METHODS ------------
107 // ******** Extent
108 // Purpose: Returns the number of nodes in the list
109 Standard_Integer Extent (void) const
110 { return myLength; }
111
112 // ******** IsEmpty
113 // Purpose: Query if the list is empty
114 Standard_Boolean IsEmpty (void) const
115 { return (myFirst == NULL); }
116
117 protected:
118 // --------- PROTECTED METHODS ----------
119
120 // ******** Constructor
121 // Purpose: Initializes an empty list
ddf2fe8e 122 NCollection_BaseList (const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
7fd59977 123 myFirst(NULL),
124 myLast(NULL),
ddf2fe8e 125 myLength(0)
126 {
127 myAllocator = (theAllocator.IsNull() ? NCollection_BaseAllocator::CommonBaseAllocator() : theAllocator);
128 }
7fd59977 129
130 // ******** PClear
131 // Purpose: deletes all nodes
ddf2fe8e 132 Standard_EXPORT void PClear (NCollection_DelListNode fDel);
7fd59977 133
134 // ******** PFirst
135 // Purpose: Returns pointer to the first node
136 const NCollection_ListNode* PFirst (void) const
137 { return myFirst; }
138
139 // ******** PLast
140 // Purpose: Returns pointer to the last node
141 const NCollection_ListNode* PLast (void) const
142 { return myLast; }
143
144 // ******** PAppend
145 // Purpose: Appends theNode at the end
146 Standard_EXPORT void PAppend (NCollection_ListNode* theNode);
147
148 // ******** PAppend
149 // Purpose: Appends theNode at the end, returns iterator to the previous
150 void PAppend (NCollection_ListNode* theNode,
151 Iterator& theIt)
152 {
153 NCollection_ListNode * aPrev = myLast;
154 PAppend (theNode);
155 theIt.Init (* this, aPrev);
156 }
157
158 // ******** PAppend
159 // Purpose: Appends theOther list at the end (clearing it)
160 Standard_EXPORT void PAppend (NCollection_BaseList& theOther);
161
162 // ******** PPrepend
163 // Purpose: Prepends theNode at the beginning
164 Standard_EXPORT void PPrepend (NCollection_ListNode* theNode);
165
166 // ******** PPrepend
167 // Purpose: Prepends theOther list at the beginning (clearing it)
168 Standard_EXPORT void PPrepend (NCollection_BaseList& theOther);
169
170 // ******** PRemoveFirst
171 // Purpose: Removes first node
172 Standard_EXPORT void PRemoveFirst
ddf2fe8e 173 (NCollection_DelListNode fDel);
7fd59977 174
175 // ******** PRemove
176 // Purpose: Removes the node pointed by theIter[ator]
177 Standard_EXPORT void PRemove
178 (Iterator& theIter,
ddf2fe8e 179 NCollection_DelListNode fDel);
7fd59977 180
181 // ******** PInsertBefore
182 // Purpose: Inserts theNode before one pointed by theIter[ator]
183 Standard_EXPORT void PInsertBefore (NCollection_ListNode* theNode,
184 Iterator& theIter);
185
186 // ******** PInsertBefore
187 // Purpose: Inserts theOther list before the node pointed by theIter[ator]
188 Standard_EXPORT void PInsertBefore (NCollection_BaseList& theOther,
189 Iterator& theIter);
190
191 // ******** PInsertAfter
192 // Purpose: Inserts theNode after one pointed by theIter[ator]
193 Standard_EXPORT void PInsertAfter (NCollection_ListNode* theNode,
194 Iterator& theIter);
195
196 // ******** PInsertAfter
197 // Purpose: Inserts theOther list after the node pointed by theIter[ator]
198 Standard_EXPORT void PInsertAfter (NCollection_BaseList& theOther,
199 Iterator& theIter);
200
201 // ******** PReverse
202 // Purpose: Reverse the list
203 Standard_EXPORT void PReverse ();
204
205 protected:
ddf2fe8e 206 // ------------ PROTECTED FIELDS ------------
207 Handle(NCollection_BaseAllocator) myAllocator;
7fd59977 208 NCollection_ListNode * myFirst; // Pointer to the head
209 NCollection_ListNode * myLast; // Pointer to the tail
210 Standard_Integer myLength; // Actual length
211
212 // ------------ FRIEND CLASSES ------------
213 friend class Iterator;
214};
215
216#endif