0022815: Missing delete operator for placement new
[occt.git] / src / NCollection / NCollection_Queue.hxx
1 // File:         NCollection_Queue.hxx
2 // Created:      17.04.02 10:12:48
3 // Author:       Alexander Kartomin (akm)
4 //               <a-kartomin@opencascade.com>
5 // Copyright:    Open Cascade 2002
6
7 #ifndef NCollection_Queue_HeaderFile
8 #define NCollection_Queue_HeaderFile
9
10 #include <NCollection_TListIterator.hxx>
11
12 #if !defined No_Exception && !defined No_Standard_NoSuchObject
13 #include <Standard_NoSuchObject.hxx>
14 #endif
15
16 /**
17  * Purpose:      A queue is  a structure where Items are  added  at
18  *               the end  and removed from   the  front. The  first
19  *               entered Item  will be the  first removed. This  is
20  *               called a FIFO (First In First Out).
21  *               Inherits BaseList, adds the data item to each node.
22  */               
23 template <class TheItemType> class NCollection_Queue
24   : public NCollection_BaseCollection<TheItemType>,
25     public NCollection_BaseList
26 {
27  public:
28   typedef NCollection_TListNode<TheItemType> QueueNode;
29   typedef NCollection_TListIterator<TheItemType> Iterator;
30
31  public:
32   // ---------- PUBLIC METHODS ------------
33
34   //! Constructor
35   NCollection_Queue(const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
36     NCollection_BaseCollection<TheItemType>(theAllocator),
37     NCollection_BaseList() {}
38
39   //! Copy constructor
40   NCollection_Queue (const NCollection_Queue& theOther) :
41     NCollection_BaseCollection<TheItemType>(theOther.myAllocator),
42     NCollection_BaseList()
43   { *this = theOther; }
44
45   //! Size - Number of items
46   virtual Standard_Integer Size (void) const
47   { return Extent(); }
48
49   //! Length - number of items
50   Standard_Integer Length (void) const
51   { return Extent(); }
52
53   //! Replace this list by the items of theOther collection
54   virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
55   {
56     if (this == &theOther) 
57       return;
58     Clear();
59     TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter = 
60       theOther.CreateIterator();
61     for (; anIter.More(); anIter.Next())
62     {
63       QueueNode* pNew = new (this->myAllocator) QueueNode(anIter.Value());
64       PAppend(pNew);
65     }
66   }
67
68   //! Replace this list by the items of theOther queue
69   NCollection_Queue& operator= (const NCollection_Queue& theOther)
70   { 
71     if (this == &theOther) 
72       return *this;
73     Clear ();
74     QueueNode * pCur = (QueueNode *) theOther.PFirst();
75     while (pCur)
76     {
77       QueueNode* pNew = new (this->myAllocator) QueueNode(pCur->Value());
78       PAppend(pNew);
79       pCur = (QueueNode *) pCur->Next();
80     }
81     return *this;
82   }
83
84   //! Clear this queue
85   void Clear (void)
86   { PClear (QueueNode::delNode, this->myAllocator); }
87
88   //! Frontal item - constant
89   const TheItemType& Front (void) const
90   {
91 #if !defined No_Exception && !defined No_Standard_NoSuchObject
92     if (IsEmpty())
93       Standard_NoSuchObject::Raise ("NCollection_Queue::Front");
94 #endif
95     return ((const QueueNode *) PFirst())->Value(); 
96   }
97
98   //! Frontal item - variable
99   TheItemType& ChangeFront (void)
100   {
101 #if !defined No_Exception && !defined No_Standard_NoSuchObject
102     if (IsEmpty())
103       Standard_NoSuchObject::Raise ("NCollection_Queue::ChangeFront");
104 #endif
105     return ((QueueNode *) PFirst())->ChangeValue();
106   }
107
108   //! Push one item
109   void Push (const TheItemType& theItem)
110   { 
111     QueueNode * pNew = new (this->myAllocator) QueueNode(theItem);
112     PAppend(pNew);
113   }
114
115   //! Pop first item
116   void Pop (void) 
117   { PRemoveFirst (QueueNode::delNode, this->myAllocator); }
118
119   //! Destructor - clears the List
120   ~NCollection_Queue (void)
121   { Clear(); }
122
123  private:
124   // ----------- PRIVATE METHODS -----------
125
126   //! Creates Iterator for use on BaseCollection
127   virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& 
128     CreateIterator(void) const
129   { return *(new (this->IterAllocator()) Iterator(*this)); }
130
131 };
132
133 #endif