source: NonGTP/Xerces/xerces/include/xercesc/util/ValueArrayOf.c @ 358

Revision 358, 8.6 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2000,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * $Log: ValueArrayOf.c,v $
19 * Revision 1.7  2004/09/08 13:56:23  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.6  2003/12/19 23:02:25  cargilld
23 * More memory management updates.
24 *
25 * Revision 1.5  2003/12/17 00:18:35  cargilld
26 * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
27 *
28 * Revision 1.4  2003/05/16 06:01:52  knoaman
29 * Partial implementation of the configurable memory manager.
30 *
31 * Revision 1.3  2003/05/15 19:07:46  knoaman
32 * Partial implementation of the configurable memory manager.
33 *
34 * Revision 1.2  2002/11/04 15:22:05  tng
35 * C++ Namespace Support.
36 *
37 * Revision 1.1.1.1  2002/02/01 22:22:13  peiyongz
38 * sane_include
39 *
40 * Revision 1.3  2000/03/02 19:54:47  roddey
41 * This checkin includes many changes done while waiting for the
42 * 1.1.0 code to be finished. I can't list them all here, but a list is
43 * available elsewhere.
44 *
45 * Revision 1.2  2000/02/06 07:48:04  rahulj
46 * Year 2K copyright swat.
47 *
48 * Revision 1.1.1.1  1999/11/09 01:05:26  twl
49 * Initial checkin
50 *
51 * Revision 1.2  1999/11/08 20:45:17  rahul
52 * Swat for adding in Product name and CVS comment log variable.
53 *
54 */
55
56
57// ---------------------------------------------------------------------------
58//  Includes
59// ---------------------------------------------------------------------------
60#if defined(XERCES_TMPLSINC)
61#include <xercesc/util/ValueArrayOf.hpp>
62#endif
63
64
65XERCES_CPP_NAMESPACE_BEGIN
66
67// ---------------------------------------------------------------------------
68//  ValueArrayOf: Contructors and Destructor
69// ---------------------------------------------------------------------------
70template <class TElem>
71ValueArrayOf<TElem>::ValueArrayOf(const unsigned int size,
72                                  MemoryManager* const manager) :
73
74    fSize(size)
75    , fArray(0)
76    , fMemoryManager(manager)
77{
78    fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize];
79}
80
81template <class TElem>
82ValueArrayOf<TElem>::ValueArrayOf( const TElem* values
83                                 , const unsigned int size
84                                 , MemoryManager* const manager) :
85
86    fSize(size)
87    , fArray(0)
88    , fMemoryManager(manager)
89{
90    fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize];
91    for (unsigned int index = 0; index < fSize; index++)
92        fArray[index] = values[index];
93}
94
95template <class TElem>
96ValueArrayOf<TElem>::ValueArrayOf(const ValueArrayOf<TElem>& source) :
97
98    fSize(source.fSize)
99    , fArray(0)
100    , fMemoryManager(source.fMemoryManager)
101{
102    fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize];
103    for (unsigned int index = 0; index < fSize; index++)
104        fArray[index] = source.fArray[index];
105}
106
107template <class TElem> ValueArrayOf<TElem>::~ValueArrayOf()
108{
109    fMemoryManager->deallocate(fArray); //delete [] fArray;
110}
111
112
113// ---------------------------------------------------------------------------
114//  ValueArrayOf: Public operators
115// ---------------------------------------------------------------------------
116template <class TElem> TElem& ValueArrayOf<TElem>::
117operator[](const unsigned int index)
118{
119    if (index >= fSize)
120        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
121    return fArray[index];
122}
123
124template <class TElem> const TElem& ValueArrayOf<TElem>::
125operator[](const unsigned int index) const
126{
127    if (index >= fSize)
128        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
129    return fArray[index];
130}
131
132template <class TElem> ValueArrayOf<TElem>& ValueArrayOf<TElem>::
133operator=(const ValueArrayOf<TElem>& toAssign)
134{
135    if (this == &toAssign)
136        return *this;
137
138    // Reallocate if not the same size
139    if (toAssign.fSize != fSize)
140    {
141        fMemoryManager->deallocate(fArray); //delete [] fArray;
142        fSize = toAssign.fSize;
143        fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize];
144    }
145
146    // Copy over the source elements
147    for (unsigned int index = 0; index < fSize; index++)
148        fArray[index] = toAssign.fArray[index];
149
150    return *this;
151}
152
153template <class TElem> bool ValueArrayOf<TElem>::
154operator==(const ValueArrayOf<TElem>& toCompare) const
155{
156    if (this == &toCompare)
157        return true;
158
159    if (fSize != toCompare.fSize)
160        return false;
161
162    for (unsigned int index = 0; index < fSize; index++)
163    {
164        if (fArray[index] != toCompare.fArray[index])
165            return false;
166    }
167
168    return true;
169}
170
171template <class TElem> bool ValueArrayOf<TElem>::
172operator!=(const ValueArrayOf<TElem>& toCompare) const
173{
174    return !operator==(toCompare);
175}
176
177
178// ---------------------------------------------------------------------------
179//  ValueArrayOf: Copy operations
180// ---------------------------------------------------------------------------
181template <class TElem> unsigned int ValueArrayOf<TElem>::
182copyFrom(const ValueArrayOf<TElem>& srcArray)
183{
184    //
185    //  Copy over as many of the source elements as will fit into
186    //  this array.
187    //
188    const unsigned int count = fSize < srcArray.fSize ?
189                                fSize : srcArray.fSize;
190
191    for (unsigned int index = 0; index < count; index++)
192        fArray[index] = srcArray.fArray[index];
193
194    return count;
195}
196
197
198// ---------------------------------------------------------------------------
199//  ValueArrayOf: Getter methods
200// ---------------------------------------------------------------------------
201template <class TElem> unsigned int ValueArrayOf<TElem>::
202length() const
203{
204    return fSize;
205}
206
207template <class TElem> TElem* ValueArrayOf<TElem>::
208rawData() const
209{
210    return fArray;
211}
212
213
214// ---------------------------------------------------------------------------
215//  ValueArrayOf: Miscellaneous methods
216// ---------------------------------------------------------------------------
217template <class TElem> void ValueArrayOf<TElem>::
218resize(const unsigned int newSize)
219{
220    if (newSize == fSize)
221        return;
222
223    if (newSize < fSize)
224        ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Array_BadNewSize, fMemoryManager);
225
226    // Allocate the new array
227    TElem* newArray = (TElem*) fMemoryManager->allocate
228    (
229        newSize * sizeof(TElem)
230    ); //new TElem[newSize];
231
232    // Copy the existing values
233    unsigned int index = 0;
234    for (; index < fSize; index++)
235        newArray[index] = fArray[index];
236
237    for (; index < newSize; index++)
238        newArray[index] = TElem(0);
239
240    // Delete the old array and udpate our members
241    fMemoryManager->deallocate(fArray); //delete [] fArray;
242    fArray = newArray;
243    fSize = newSize;
244}
245
246
247
248// ---------------------------------------------------------------------------
249//  ValueArrayEnumerator: Constructors and Destructor
250// ---------------------------------------------------------------------------
251template <class TElem> ValueArrayEnumerator<TElem>::
252ValueArrayEnumerator(ValueArrayOf<TElem>* const toEnum, const bool adopt) :
253    fAdopted(adopt)
254    , fCurIndex(0)
255    , fToEnum(toEnum)
256{
257}
258
259template <class TElem> ValueArrayEnumerator<TElem>::~ValueArrayEnumerator()
260{
261    if (fAdopted)
262        delete fToEnum;
263}
264
265
266// ---------------------------------------------------------------------------
267//  ValueArrayEnumerator: Enum interface
268// ---------------------------------------------------------------------------
269template <class TElem> bool ValueArrayEnumerator<TElem>::hasMoreElements() const
270{
271    if (fCurIndex >= fToEnum->length())
272        return false;
273    return true;
274}
275
276template <class TElem> TElem& ValueArrayEnumerator<TElem>::nextElement()
277{
278    return (*fToEnum)[fCurIndex++];
279}
280
281template <class TElem> void ValueArrayEnumerator<TElem>::Reset()
282{
283    fCurIndex = 0;
284}
285
286XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.