8a15a7046a78c5b4d040e820ee05069a357d6a5e
[occt.git] / src / NCollection / NCollection_Stack.hxx
1 // Created on: 2002-04-17
2 // Created by: Alexander Kartomin (akm)
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef NCollection_Stack_HeaderFile
17 #define NCollection_Stack_HeaderFile
18
19 #include <NCollection_BaseCollection.hxx>
20 #include <NCollection_BaseList.hxx>
21 #include <NCollection_TListNode.hxx>
22 #include <NCollection_TListIterator.hxx>
23 #if !defined No_Exception && !defined No_Standard_NoSuchObject
24 #include <Standard_NoSuchObject.hxx>
25 #endif
26
27 /**
28  * Purpose:      A stack is a structure where item can be added and
29  *               removed from the top. Like a stack of plates  in a
30  *               kitchen. The   last entered item  will be   be the
31  *               first  removed. This  is  called  a  LIFO (last In First Out).
32  *               Inherits BaseList, adding the data item to each node.
33  */               
34 template <class TheItemType> class NCollection_Stack
35   : public NCollection_BaseCollection<TheItemType>,
36     public NCollection_BaseList
37 {
38  public:
39   typedef NCollection_TListNode<TheItemType> StackNode;
40   typedef NCollection_TListIterator<TheItemType> Iterator;
41
42  public:
43   // ---------- PUBLIC METHODS ------------
44
45   //! Constructor
46   NCollection_Stack(const Handle(NCollection_BaseAllocator)& theAllocator=0L) :
47     NCollection_BaseCollection<TheItemType>(theAllocator),
48     NCollection_BaseList() {}
49
50   //! Copy constructor
51   NCollection_Stack (const NCollection_Stack& theOther) :
52     NCollection_BaseCollection<TheItemType>(theOther.myAllocator),
53     NCollection_BaseList()
54   { *this = theOther; }
55
56   //! Size - Number of items
57   virtual Standard_Integer Size (void) const
58   { return Extent(); }
59
60   //! Depth - Number of items
61   Standard_Integer Depth (void) const
62   { return Extent(); }
63
64   //! Replace this list by the items of theOther collection
65   virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther)
66   {
67     if (this == &theOther) 
68       return;
69     Clear();
70     TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter = 
71       theOther.CreateIterator();
72     for (; anIter.More(); anIter.Next())
73     {
74       StackNode* pNew = new (this->myAllocator) StackNode(anIter.Value());
75       PAppend(pNew);
76     }
77   }
78
79   //! Replace this list by the items of theOther Stack
80   NCollection_Stack& operator= (const NCollection_Stack& theOther)
81   { 
82     if (this == &theOther) 
83       return *this;
84     Clear ();
85     StackNode * pCur = (StackNode *) theOther.PFirst();
86     while (pCur)
87     {
88       StackNode* pNew = new (this->myAllocator) StackNode(pCur->Value());
89       PAppend(pNew);
90       pCur = (StackNode *) pCur->Next();
91     }
92     return *this;
93   }
94
95   //! Clear this stack
96   void Clear (void)
97   { PClear (StackNode::delNode, this->myAllocator); }
98
99   //! Top item - constant
100   const TheItemType& Top (void) const
101   {
102 #if !defined No_Exception && !defined No_Standard_NoSuchObject
103     if (IsEmpty()) Standard_NoSuchObject::Raise ("NCollection_Stack::Top");
104 #endif
105     return ((StackNode *) PFirst())->Value();
106   }
107
108   //! Top item - variable
109   TheItemType& ChangeTop (void)
110   { 
111 #if !defined No_Exception && !defined No_Standard_NoSuchObject
112     if (IsEmpty()) Standard_NoSuchObject::Raise("NCollection_Stack::ChangeTop");
113 #endif
114     return ((StackNode *) PFirst())->ChangeValue();
115   }
116
117   //! Push one item
118   void Push (const TheItemType& theItem)
119   { 
120     StackNode * pNew = new (this->myAllocator) StackNode(theItem);
121     PPrepend(pNew);
122   }
123
124   //! Pop top item
125   void Pop (void) 
126   { PRemoveFirst (StackNode::delNode, this->myAllocator); }
127
128   //! Destructor - clears the List
129   ~NCollection_Stack (void)
130   { Clear(); }
131
132  private:
133   // ----------- PRIVATE METHODS -----------
134
135   //! Creates Iterator for use on BaseCollection
136   virtual TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& 
137     CreateIterator(void) const
138   { return *(new (this->IterAllocator()) Iterator(*this)); }
139
140 };
141
142 #endif