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

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