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: ValueArrayOf.c 568078 2007-08-21 11:43:25Z amassari $ |
---|
20 | */ |
---|
21 | |
---|
22 | |
---|
23 | // --------------------------------------------------------------------------- |
---|
24 | // Includes |
---|
25 | // --------------------------------------------------------------------------- |
---|
26 | #if defined(XERCES_TMPLSINC) |
---|
27 | #include <xercesc/util/ValueArrayOf.hpp> |
---|
28 | #endif |
---|
29 | |
---|
30 | |
---|
31 | XERCES_CPP_NAMESPACE_BEGIN |
---|
32 | |
---|
33 | // --------------------------------------------------------------------------- |
---|
34 | // ValueArrayOf: Contructors and Destructor |
---|
35 | // --------------------------------------------------------------------------- |
---|
36 | template <class TElem> |
---|
37 | ValueArrayOf<TElem>::ValueArrayOf(const unsigned int size, |
---|
38 | MemoryManager* const manager) : |
---|
39 | |
---|
40 | fSize(size) |
---|
41 | , fArray(0) |
---|
42 | , fMemoryManager(manager) |
---|
43 | { |
---|
44 | fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize]; |
---|
45 | } |
---|
46 | |
---|
47 | template <class TElem> |
---|
48 | ValueArrayOf<TElem>::ValueArrayOf( const TElem* values |
---|
49 | , const unsigned int size |
---|
50 | , MemoryManager* const manager) : |
---|
51 | |
---|
52 | fSize(size) |
---|
53 | , fArray(0) |
---|
54 | , fMemoryManager(manager) |
---|
55 | { |
---|
56 | fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize]; |
---|
57 | for (unsigned int index = 0; index < fSize; index++) |
---|
58 | fArray[index] = values[index]; |
---|
59 | } |
---|
60 | |
---|
61 | template <class TElem> |
---|
62 | ValueArrayOf<TElem>::ValueArrayOf(const ValueArrayOf<TElem>& source) : |
---|
63 | XMemory(source) |
---|
64 | , fSize(source.fSize) |
---|
65 | , fArray(0) |
---|
66 | , fMemoryManager(source.fMemoryManager) |
---|
67 | { |
---|
68 | fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize]; |
---|
69 | for (unsigned int index = 0; index < fSize; index++) |
---|
70 | fArray[index] = source.fArray[index]; |
---|
71 | } |
---|
72 | |
---|
73 | template <class TElem> ValueArrayOf<TElem>::~ValueArrayOf() |
---|
74 | { |
---|
75 | fMemoryManager->deallocate(fArray); //delete [] fArray; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | // --------------------------------------------------------------------------- |
---|
80 | // ValueArrayOf: Public operators |
---|
81 | // --------------------------------------------------------------------------- |
---|
82 | template <class TElem> TElem& ValueArrayOf<TElem>:: |
---|
83 | operator[](const unsigned int index) |
---|
84 | { |
---|
85 | if (index >= fSize) |
---|
86 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager); |
---|
87 | return fArray[index]; |
---|
88 | } |
---|
89 | |
---|
90 | template <class TElem> const TElem& ValueArrayOf<TElem>:: |
---|
91 | operator[](const unsigned int index) const |
---|
92 | { |
---|
93 | if (index >= fSize) |
---|
94 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager); |
---|
95 | return fArray[index]; |
---|
96 | } |
---|
97 | |
---|
98 | template <class TElem> ValueArrayOf<TElem>& ValueArrayOf<TElem>:: |
---|
99 | operator=(const ValueArrayOf<TElem>& toAssign) |
---|
100 | { |
---|
101 | if (this == &toAssign) |
---|
102 | return *this; |
---|
103 | |
---|
104 | // Reallocate if not the same size |
---|
105 | if (toAssign.fSize != fSize) |
---|
106 | { |
---|
107 | fMemoryManager->deallocate(fArray); //delete [] fArray; |
---|
108 | fSize = toAssign.fSize; |
---|
109 | fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize]; |
---|
110 | } |
---|
111 | |
---|
112 | // Copy over the source elements |
---|
113 | for (unsigned int index = 0; index < fSize; index++) |
---|
114 | fArray[index] = toAssign.fArray[index]; |
---|
115 | |
---|
116 | return *this; |
---|
117 | } |
---|
118 | |
---|
119 | template <class TElem> bool ValueArrayOf<TElem>:: |
---|
120 | operator==(const ValueArrayOf<TElem>& toCompare) const |
---|
121 | { |
---|
122 | if (this == &toCompare) |
---|
123 | return true; |
---|
124 | |
---|
125 | if (fSize != toCompare.fSize) |
---|
126 | return false; |
---|
127 | |
---|
128 | for (unsigned int index = 0; index < fSize; index++) |
---|
129 | { |
---|
130 | if (fArray[index] != toCompare.fArray[index]) |
---|
131 | return false; |
---|
132 | } |
---|
133 | |
---|
134 | return true; |
---|
135 | } |
---|
136 | |
---|
137 | template <class TElem> bool ValueArrayOf<TElem>:: |
---|
138 | operator!=(const ValueArrayOf<TElem>& toCompare) const |
---|
139 | { |
---|
140 | return !operator==(toCompare); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | // --------------------------------------------------------------------------- |
---|
145 | // ValueArrayOf: Copy operations |
---|
146 | // --------------------------------------------------------------------------- |
---|
147 | template <class TElem> unsigned int ValueArrayOf<TElem>:: |
---|
148 | copyFrom(const ValueArrayOf<TElem>& srcArray) |
---|
149 | { |
---|
150 | // |
---|
151 | // Copy over as many of the source elements as will fit into |
---|
152 | // this array. |
---|
153 | // |
---|
154 | const unsigned int count = fSize < srcArray.fSize ? |
---|
155 | fSize : srcArray.fSize; |
---|
156 | |
---|
157 | for (unsigned int index = 0; index < count; index++) |
---|
158 | fArray[index] = srcArray.fArray[index]; |
---|
159 | |
---|
160 | return count; |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | // --------------------------------------------------------------------------- |
---|
165 | // ValueArrayOf: Getter methods |
---|
166 | // --------------------------------------------------------------------------- |
---|
167 | template <class TElem> unsigned int ValueArrayOf<TElem>:: |
---|
168 | length() const |
---|
169 | { |
---|
170 | return fSize; |
---|
171 | } |
---|
172 | |
---|
173 | template <class TElem> TElem* ValueArrayOf<TElem>:: |
---|
174 | rawData() const |
---|
175 | { |
---|
176 | return fArray; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | // --------------------------------------------------------------------------- |
---|
181 | // ValueArrayOf: Miscellaneous methods |
---|
182 | // --------------------------------------------------------------------------- |
---|
183 | template <class TElem> void ValueArrayOf<TElem>:: |
---|
184 | resize(const unsigned int newSize) |
---|
185 | { |
---|
186 | if (newSize == fSize) |
---|
187 | return; |
---|
188 | |
---|
189 | if (newSize < fSize) |
---|
190 | ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Array_BadNewSize, fMemoryManager); |
---|
191 | |
---|
192 | // Allocate the new array |
---|
193 | TElem* newArray = (TElem*) fMemoryManager->allocate |
---|
194 | ( |
---|
195 | newSize * sizeof(TElem) |
---|
196 | ); //new TElem[newSize]; |
---|
197 | |
---|
198 | // Copy the existing values |
---|
199 | unsigned int index = 0; |
---|
200 | for (; index < fSize; index++) |
---|
201 | newArray[index] = fArray[index]; |
---|
202 | |
---|
203 | for (; index < newSize; index++) |
---|
204 | newArray[index] = TElem(0); |
---|
205 | |
---|
206 | // Delete the old array and udpate our members |
---|
207 | fMemoryManager->deallocate(fArray); //delete [] fArray; |
---|
208 | fArray = newArray; |
---|
209 | fSize = newSize; |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | // --------------------------------------------------------------------------- |
---|
215 | // ValueArrayEnumerator: Constructors and Destructor |
---|
216 | // --------------------------------------------------------------------------- |
---|
217 | template <class TElem> ValueArrayEnumerator<TElem>:: |
---|
218 | ValueArrayEnumerator(ValueArrayOf<TElem>* const toEnum, const bool adopt) : |
---|
219 | fAdopted(adopt) |
---|
220 | , fCurIndex(0) |
---|
221 | , fToEnum(toEnum) |
---|
222 | { |
---|
223 | } |
---|
224 | |
---|
225 | template <class TElem> ValueArrayEnumerator<TElem>::~ValueArrayEnumerator() |
---|
226 | { |
---|
227 | if (fAdopted) |
---|
228 | delete fToEnum; |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | // --------------------------------------------------------------------------- |
---|
233 | // ValueArrayEnumerator: Enum interface |
---|
234 | // --------------------------------------------------------------------------- |
---|
235 | template <class TElem> bool ValueArrayEnumerator<TElem>::hasMoreElements() const |
---|
236 | { |
---|
237 | if (fCurIndex >= fToEnum->length()) |
---|
238 | return false; |
---|
239 | return true; |
---|
240 | } |
---|
241 | |
---|
242 | template <class TElem> TElem& ValueArrayEnumerator<TElem>::nextElement() |
---|
243 | { |
---|
244 | return (*fToEnum)[fCurIndex++]; |
---|
245 | } |
---|
246 | |
---|
247 | template <class TElem> void ValueArrayEnumerator<TElem>::Reset() |
---|
248 | { |
---|
249 | fCurIndex = 0; |
---|
250 | } |
---|
251 | |
---|
252 | XERCES_CPP_NAMESPACE_END |
---|