1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * $Id: RefStackOf.c 676911 2008-07-15 13:27:32Z amassari $
20  */
21 
22 
23 // ---------------------------------------------------------------------------
24 //  Includes
25 // ---------------------------------------------------------------------------
26 #if defined(XERCES_TMPLSINC)
27 #include <xercesc/util/RefStackOf.hpp>
28 #endif
29 
30 XERCES_CPP_NAMESPACE_BEGIN
31 
32 // ---------------------------------------------------------------------------
33 //  RefStackOf: Constructors and Destructor
34 // ---------------------------------------------------------------------------
35 template <class TElem>
RefStackOf(const XMLSize_t initElems,const bool adoptElems,MemoryManager * const manager)36 RefStackOf<TElem>::RefStackOf(const XMLSize_t initElems,
37                               const bool adoptElems,
38                               MemoryManager* const manager) :
39 
40     fVector(initElems, adoptElems, manager)
41 {
42 }
43 
~RefStackOf()44 template <class TElem> RefStackOf<TElem>::~RefStackOf()
45 {
46 }
47 
48 
49 // ---------------------------------------------------------------------------
50 //  RefStackOf: Element management methods
51 // ---------------------------------------------------------------------------
52 template <class TElem> const TElem* RefStackOf<TElem>::
elementAt(const XMLSize_t index)53 elementAt(const XMLSize_t index) const
54 {
55     if (index >= fVector.size())
56         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Stack_BadIndex, fVector.getMemoryManager());
57     return fVector.elementAt(index);
58 }
59 
popAt(const XMLSize_t index)60 template <class TElem> TElem* RefStackOf<TElem>::popAt(const XMLSize_t index)
61 {
62     if (index >= fVector.size())
63         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Stack_BadIndex, fVector.getMemoryManager());
64 
65     // Orphan off the element from the slot in the vector
66     return fVector.orphanElementAt(index);
67 }
68 
push(TElem * const toPush)69 template <class TElem> void RefStackOf<TElem>::push(TElem* const toPush)
70 {
71     fVector.addElement(toPush);
72 }
73 
peek()74 template <class TElem> const TElem* RefStackOf<TElem>::peek() const
75 {
76     const XMLSize_t curSize = fVector.size();
77     if (curSize == 0)
78         ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::Stack_EmptyStack, fVector.getMemoryManager());
79 
80     return fVector.elementAt(curSize-1);
81 }
82 
pop()83 template <class TElem> TElem* RefStackOf<TElem>::pop()
84 {
85     const XMLSize_t curSize = fVector.size();
86     if (curSize == 0)
87         ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::Stack_EmptyStack, fVector.getMemoryManager());
88 
89     // Orphan off the element from the last slot in the vector
90     return fVector.orphanElementAt(curSize-1);
91 }
92 
removeAllElements()93 template <class TElem> void RefStackOf<TElem>::removeAllElements()
94 {
95     fVector.removeAllElements();
96 }
97 
98 
99 // ---------------------------------------------------------------------------
100 //  RefStackOf: Getter methods
101 // ---------------------------------------------------------------------------
empty()102 template <class TElem> bool RefStackOf<TElem>::empty()
103 {
104     return (fVector.size() == 0);
105 }
106 
curCapacity()107 template <class TElem> XMLSize_t RefStackOf<TElem>::curCapacity()
108 {
109     return fVector.curCapacity();
110 }
111 
size()112 template <class TElem> XMLSize_t RefStackOf<TElem>::size()
113 {
114     return fVector.size();
115 }
116 
117 
118 
119 
120 // ---------------------------------------------------------------------------
121 //  RefStackEnumerator: Constructors and Destructor
122 // ---------------------------------------------------------------------------
123 template <class TElem> RefStackEnumerator<TElem>::
RefStackEnumerator(RefStackOf<TElem> * const toEnum,const bool adopt)124 RefStackEnumerator(         RefStackOf<TElem>* const    toEnum
125                     , const bool                        adopt) :
126     fAdopted(adopt)
127     , fCurIndex(0)
128     , fToEnum(toEnum)
129     , fVector(&toEnum->fVector)
130 {
131 }
132 
~RefStackEnumerator()133 template <class TElem> RefStackEnumerator<TElem>::~RefStackEnumerator()
134 {
135     if (fAdopted)
136         delete fToEnum;
137 }
138 
139 
140 // ---------------------------------------------------------------------------
141 //  RefStackEnumerator: Enum interface
142 // ---------------------------------------------------------------------------
hasMoreElements()143 template <class TElem> bool RefStackEnumerator<TElem>::hasMoreElements() const
144 {
145     if (fCurIndex >= fVector->size())
146         return false;
147     return true;
148 }
149 
nextElement()150 template <class TElem> TElem& RefStackEnumerator<TElem>::nextElement()
151 {
152     return *fVector->elementAt(fCurIndex++);
153 }
154 
Reset()155 template <class TElem> void RefStackEnumerator<TElem>::Reset()
156 {
157     fCurIndex = 0;
158 }
159 
160 XERCES_CPP_NAMESPACE_END
161