source: NonGTP/Xerces/xercesc/util/RefArrayOf.c @ 188

Revision 188, 11.0 KB checked in by mattausch, 19 years ago (diff)

added xercesc to support

Line 
1/*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in
16 *    the documentation and/or other materials provided with the
17 *    distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 *    if any, must include the following acknowledgment:
21 *       "This product includes software developed by the
22 *        Apache Software Foundation (http://www.apache.org/)."
23 *    Alternately, this acknowledgment may appear in the software itself,
24 *    if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Xerces" and "Apache Software Foundation" must
27 *    not be used to endorse or promote products derived from this
28 *    software without prior written permission. For written
29 *    permission, please contact apache\@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 *    nor may "Apache" appear in their name, without prior written
33 *    permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation, and was
51 * originally based on software copyright (c) 1999, International
52 * Business Machines, Inc., http://www.ibm.com .  For more information
53 * on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57/**
58 * $Log: RefArrayOf.c,v $
59 * Revision 1.5  2003/12/17 00:18:35  cargilld
60 * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data.
61 *
62 * Revision 1.4  2003/05/16 06:01:52  knoaman
63 * Partial implementation of the configurable memory manager.
64 *
65 * Revision 1.3  2003/05/15 19:04:35  knoaman
66 * Partial implementation of the configurable memory manager.
67 *
68 * Revision 1.2  2002/11/04 15:22:04  tng
69 * C++ Namespace Support.
70 *
71 * Revision 1.1.1.1  2002/02/01 22:22:11  peiyongz
72 * sane_include
73 *
74 * Revision 1.3  2000/03/02 19:54:44  roddey
75 * This checkin includes many changes done while waiting for the
76 * 1.1.0 code to be finished. I can't list them all here, but a list is
77 * available elsewhere.
78 *
79 * Revision 1.2  2000/02/06 07:48:03  rahulj
80 * Year 2K copyright swat.
81 *
82 * Revision 1.1.1.1  1999/11/09 01:04:56  twl
83 * Initial checkin
84 *
85 * Revision 1.2  1999/11/08 20:45:12  rahul
86 * Swat for adding in Product name and CVS comment log variable.
87 *
88 */
89
90
91// ---------------------------------------------------------------------------
92//  Includes
93// ---------------------------------------------------------------------------
94#if defined(XERCES_TMPLSINC)
95#include <xercesc/util/RefArrayOf.hpp>
96#endif
97
98XERCES_CPP_NAMESPACE_BEGIN
99
100// ---------------------------------------------------------------------------
101//  RefArrayOf: Contructors and Destructor
102// ---------------------------------------------------------------------------
103template <class TElem>
104RefArrayOf<TElem>::RefArrayOf(const unsigned int size,
105                              MemoryManager* const manager) :
106
107    fSize(size)
108    , fArray(0)
109    , fMemoryManager(manager)
110{
111    fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
112    for (unsigned int index = 0; index < fSize; index++)
113        fArray[index] = 0;
114}
115
116template <class TElem>
117RefArrayOf<TElem>::RefArrayOf(TElem* values[],
118                              const unsigned int size,
119                              MemoryManager* const manager) :
120
121    fSize(size)
122    , fArray(0)
123    , fMemoryManager(manager)
124{
125    fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
126    for (unsigned int index = 0; index < fSize; index++)
127        fArray[index] = values[index];
128}
129
130template <class TElem> RefArrayOf<TElem>::
131RefArrayOf(const RefArrayOf<TElem>& source) :
132
133    fSize(source.fSize)
134    , fArray(0)
135    , fMemoryManager(source.fMemoryManager)
136{
137    fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
138    for (unsigned int index = 0; index < fSize; index++)
139        fArray[index] = source.fArray[index];
140}
141
142template <class TElem> RefArrayOf<TElem>::~RefArrayOf()
143{
144    fMemoryManager->deallocate(fArray);//delete [] fArray;
145}
146
147
148// ---------------------------------------------------------------------------
149//  RefArrayOf: Public operators
150// ---------------------------------------------------------------------------
151template <class TElem> TElem*& RefArrayOf<TElem>::
152operator[](const unsigned int index)
153{
154    if (index >= fSize)
155        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
156    return fArray[index];
157}
158
159template <class TElem> const TElem* RefArrayOf<TElem>::
160operator[](const unsigned int index) const
161{
162    if (index >= fSize)
163        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
164    return fArray[index];
165}
166
167template <class TElem> RefArrayOf<TElem>& RefArrayOf<TElem>::
168operator=(const RefArrayOf<TElem>& toAssign)
169{
170    if (this == &toAssign)
171        return *this;
172
173    // Reallocate if not the same size
174    if (toAssign.fSize != fSize)
175    {
176        fMemoryManager->deallocate(fArray);//delete [] fArray;
177        fSize = toAssign.fSize;
178        fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
179    }
180
181    // Copy over the source elements
182    for (unsigned int index = 0; index < fSize; index++)
183        fArray[index] = toAssign.fArray[index];
184
185    return *this;
186}
187
188template <class TElem> bool RefArrayOf<TElem>::
189operator==(const RefArrayOf<TElem>& toCompare) const
190{
191    if (this == &toCompare)
192        return true;
193
194    if (fSize != toCompare.fSize)
195        return false;
196
197    for (unsigned int index = 0; index < fSize; index++)
198    {
199        if (fArray[index] != toCompare.fArray[index])
200            return false;
201    }
202    return true;
203}
204
205template <class TElem> bool RefArrayOf<TElem>::
206operator!=(const RefArrayOf<TElem>& toCompare) const
207{
208    return !operator==(toCompare);
209}
210
211
212// ---------------------------------------------------------------------------
213//  RefArrayOf: Copy operations
214// ---------------------------------------------------------------------------
215template <class TElem> unsigned int RefArrayOf<TElem>::
216copyFrom(const RefArrayOf<TElem>& srcArray)
217{
218    //
219    //  Copy over as many of the source elements as will fit into
220    //  this array.
221    //
222    const unsigned int count = fSize < srcArray.fSize ?
223                                    fSize : srcArray.fSize;
224
225    for (unsigned int index = 0; index < fSize; index++)
226        fArray[index] = srcArray.fArray[index];
227
228    return count;
229}
230
231
232// ---------------------------------------------------------------------------
233//  RefArrayOf: Getter methods
234// ---------------------------------------------------------------------------
235template <class TElem> unsigned int RefArrayOf<TElem>::length() const
236{
237    return fSize;
238}
239
240template <class TElem> TElem** RefArrayOf<TElem>::rawData() const
241{
242    return fArray;
243}
244
245
246// ---------------------------------------------------------------------------
247//  RefArrayOf: Element management methods
248// ---------------------------------------------------------------------------
249template <class TElem> void RefArrayOf<TElem>::deleteAt(const unsigned int index)
250{
251    if (index >= fSize)
252        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
253
254    delete fArray[index];
255    fArray[index] = 0;
256}
257
258template <class TElem> void RefArrayOf<TElem>::deleteAllElements()
259{
260    for (unsigned int index = 0; index < fSize; index++)
261    {
262        delete fArray[index];
263        fArray[index] = 0;
264    }
265}
266
267template <class TElem> void RefArrayOf<TElem>::resize(const unsigned int newSize)
268{
269    if (newSize == fSize)
270        return;
271
272    if (newSize < fSize)
273        ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Array_BadNewSize, fMemoryManager);
274
275    // Allocate the new array
276    TElem** newArray = (TElem**) fMemoryManager->allocate
277    (
278        newSize * sizeof(TElem*)
279    );//new TElem*[newSize];
280
281    // Copy the existing values
282    unsigned int index = 0;
283    for (; index < fSize; index++)
284        newArray[index] = fArray[index];
285
286    for (; index < newSize; index++)
287        newArray[index] = 0;
288
289    // Delete the old array and udpate our members
290    fMemoryManager->deallocate(fArray);//delete [] fArray;
291    fArray = newArray;
292    fSize = newSize;
293}
294
295
296
297
298// ---------------------------------------------------------------------------
299//  RefArrayEnumerator: Constructors and Destructor
300// ---------------------------------------------------------------------------
301template <class TElem> RefArrayEnumerator<TElem>::
302RefArrayEnumerator(         RefArrayOf<TElem>* const    toEnum
303                    , const bool                        adopt) :
304    fAdopted(adopt)
305    , fCurIndex(0)
306    , fToEnum(toEnum)
307{
308}
309
310template <class TElem> RefArrayEnumerator<TElem>::~RefArrayEnumerator()
311{
312    if (fAdopted)
313        delete fToEnum;
314}
315
316
317// ---------------------------------------------------------------------------
318//  RefArrayEnumerator: Enum interface
319// ---------------------------------------------------------------------------
320template <class TElem> bool RefArrayEnumerator<TElem>::hasMoreElements() const
321{
322    if (fCurIndex >= fToEnum->length())
323        return false;
324    return true;
325}
326
327template <class TElem> TElem& RefArrayEnumerator<TElem>::nextElement()
328{
329    return *(*fToEnum)[fCurIndex++];
330}
331
332template <class TElem> void RefArrayEnumerator<TElem>::Reset()
333{
334    fCurIndex = 0;
335}
336
337XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.