1 // File: NCollection_SList.hxx
2 // Created: 17.04.02 10:12:48
3 // Author: Alexander Kartomin (akm)
4 // Copyright: Open Cascade 2002
6 #ifndef NCollection_SList_HeaderFile
7 #define NCollection_SList_HeaderFile
9 #include <NCollection_BaseCollection.hxx>
11 #if !defined No_Exception && !defined No_Standard_NoSuchObject
12 #include <Standard_NoSuchObject.hxx>
16 * Purpose: An SList is a LISP like list of Items.
19 * . Or it has a Value and a Tail which is an other SList.
21 * The Tail of an empty list is an empty list.
22 * SList are shared. It means that they can be
23 * modified through other lists.
24 * SList may be used as Iterators. They have Next,
25 * More, and value methods. To iterate on the content
26 * of the list S just do.
29 * for (Iterator = S; Iterator.More(); Iterator.Next())
30 * X = Iterator.Value();
32 * Memory usage is automatically managed for SLists
33 * (using reference counts).
36 * If S1 and S2 are SLists :
39 * And the following is done :
43 * S1.Value() becomes also Y. So SList must be used
44 * with care. Mainly the SetValue() method is
47 template <class TheItemType> class NCollection_SList
48 : public NCollection_BaseCollection<TheItemType>,
49 public NCollection_BaseCollection<TheItemType>::Iterator
57 SListNode (const TheItemType& theItem,
58 const NCollection_SList& theTail) :
61 { myTail = new (theTail.myAllocator) NCollection_SList(theTail); }
63 NCollection_SList& Tail (void)
66 TheItemType& Value (void)
72 myTail->myAllocator->Free(myTail);
76 DEFINE_NCOLLECTION_ALLOC
79 // ---------- PRIVATE FIELDS ------------
80 Standard_Integer myCount; //!< Reference count
81 NCollection_SList * myTail; //!< The tail
82 TheItemType myValue; //!< Datum
84 // Everything above is private. Only SList has an access
85 friend class NCollection_SList<TheItemType>;
86 }; // End of nested class SListNode
89 // ---------- PUBLIC METHODS ------------
92 NCollection_SList(const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
93 NCollection_BaseCollection<TheItemType>(theAllocator),
97 NCollection_SList(const TheItemType& theItem,
98 const NCollection_SList& theTail) :
99 NCollection_BaseCollection<TheItemType>(theTail.myAllocator)
100 { myNode = new (theTail.myAllocator) SListNode(theItem,theTail); }
103 NCollection_SList (const NCollection_SList& theOther) :
104 NCollection_BaseCollection<TheItemType>(theOther.myAllocator)
106 myNode = theOther.myNode;
111 //! Clear the items out
117 if (myNode->myCount < 1)
120 this->myAllocator->Free(myNode);
125 //! Make this list identical to theOther
126 NCollection_SList& operator= (const NCollection_SList& theOther)
128 if (myNode != theOther.myNode)
131 theOther.myNode->myCount++;
133 this->myAllocator = theOther.myAllocator;
134 myNode = theOther.myNode;
139 //! Replace this list by the items of theOther collection
140 virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
142 if (this == &theOther)
145 TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter =
146 theOther.CreateIterator();
149 SListNode *aNode, *aPrevNode=NULL;
150 for (; anIter.More(); anIter.Next())
152 aNode = new (this->myAllocator) SListNode
153 (anIter.Value(), NCollection_SList(this->myAllocator));
157 aPrevNode->Tail().myNode = aNode;
163 Standard_Boolean IsEmpty (void) const
164 { return (myNode==NULL); }
166 //! Value - constant access
167 virtual const TheItemType& Value (void) const
169 #if !defined No_Exception && !defined No_Standard_NoSuchObject
171 Standard_NoSuchObject::Raise ("NCollection_SList::Value");
173 return myNode->Value();
176 //! ChangeValue - variable access
177 virtual TheItemType& ChangeValue (void) const
179 #if !defined No_Exception && !defined No_Standard_NoSuchObject
181 Standard_NoSuchObject::Raise ("NCollection_SList::ChangeValue");
183 return myNode->Value();
187 void SetValue (const TheItemType& theItem)
189 #if !defined No_Exception && !defined No_Standard_NoSuchObject
191 Standard_NoSuchObject::Raise ("NCollection_SList::SetValue");
193 myNode->Value() = theItem;
197 const NCollection_SList& Tail (void) const
200 return myNode->Tail();
206 NCollection_SList& ChangeTail (void)
209 return myNode->Tail();
215 void SetTail (NCollection_SList& theList)
218 myNode->Tail() = theList;
224 void Construct(const TheItemType& theItem)
225 { *this = NCollection_SList (theItem, *this); }
228 NCollection_SList Constructed(const TheItemType& theItem) const
229 { return NCollection_SList (theItem, *this); }
235 //! Initialize (~Assign)
236 void Initialize (const NCollection_SList& theOther)
237 { *this = theOther; }
239 //! Init (virtual method of base iterator)
240 void Init (const NCollection_SList& theOther)
241 { *this = theOther; }
244 virtual Standard_Boolean More (void) const
245 { return !IsEmpty(); }
248 virtual void Next (void)
251 //! Size - Number of items
252 virtual Standard_Integer Size (void) const
253 { return (IsEmpty() ? 0 : 1+myNode->Tail().Size()); }
255 //! Destructor - clears the SList
256 ~NCollection_SList (void)
261 // ----------- PRIVATE METHODS -----------
263 //! Creates Iterator for use on BaseCollection
264 virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator&
265 CreateIterator(void) const
266 { return *(new (this->IterAllocator()) NCollection_SList(*this)); }
269 // ---------- PRIVATE FIELDS ------------
272 friend class SListNode;