source: icXML/icXML-devel/src/xercesc/util/ValueArrayOf.c @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 7.4 KB
Line 
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 932887 2010-04-11 13:04:59Z borisk $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#if defined(XERCES_TMPLSINC)
27#include <xercesc/util/ValueArrayOf.hpp>
28#endif
29
30
31XERCES_CPP_NAMESPACE_BEGIN
32
33// ---------------------------------------------------------------------------
34//  ValueArrayOf: Constructors and Destructor
35// ---------------------------------------------------------------------------
36template <class TElem>
37ValueArrayOf<TElem>::ValueArrayOf(const XMLSize_t 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
47template <class TElem>
48ValueArrayOf<TElem>::ValueArrayOf( const TElem* values
49                                 , const XMLSize_t 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 (XMLSize_t index = 0; index < fSize; index++)
58        fArray[index] = values[index];
59}
60
61template <class TElem>
62ValueArrayOf<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 (XMLSize_t index = 0; index < fSize; index++)
70        fArray[index] = source.fArray[index];
71}
72
73template <class TElem> ValueArrayOf<TElem>::~ValueArrayOf()
74{
75    fMemoryManager->deallocate(fArray); //delete [] fArray;
76}
77
78
79// ---------------------------------------------------------------------------
80//  ValueArrayOf: Public operators
81// ---------------------------------------------------------------------------
82template <class TElem> TElem& ValueArrayOf<TElem>::
83operator[](const XMLSize_t index)
84{
85    if (index >= fSize)
86        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
87    return fArray[index];
88}
89
90template <class TElem> const TElem& ValueArrayOf<TElem>::
91operator[](const XMLSize_t index) const
92{
93    if (index >= fSize)
94        ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
95    return fArray[index];
96}
97
98template <class TElem> ValueArrayOf<TElem>& ValueArrayOf<TElem>::
99operator=(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 (XMLSize_t index = 0; index < fSize; index++)
114        fArray[index] = toAssign.fArray[index];
115
116    return *this;
117}
118
119template <class TElem> bool ValueArrayOf<TElem>::
120operator==(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 (XMLSize_t index = 0; index < fSize; index++)
129    {
130        if (fArray[index] != toCompare.fArray[index])
131            return false;
132    }
133
134    return true;
135}
136
137template <class TElem> bool ValueArrayOf<TElem>::
138operator!=(const ValueArrayOf<TElem>& toCompare) const
139{
140    return !operator==(toCompare);
141}
142
143
144// ---------------------------------------------------------------------------
145//  ValueArrayOf: Copy operations
146// ---------------------------------------------------------------------------
147template <class TElem> XMLSize_t ValueArrayOf<TElem>::
148copyFrom(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 XMLSize_t count = fSize < srcArray.fSize ?
155                                fSize : srcArray.fSize;
156
157    for (XMLSize_t index = 0; index < count; index++)
158        fArray[index] = srcArray.fArray[index];
159
160    return count;
161}
162
163
164// ---------------------------------------------------------------------------
165//  ValueArrayOf: Getter methods
166// ---------------------------------------------------------------------------
167template <class TElem> XMLSize_t ValueArrayOf<TElem>::
168length() const
169{
170    return fSize;
171}
172
173template <class TElem> TElem* ValueArrayOf<TElem>::
174rawData() const
175{
176    return fArray;
177}
178
179
180// ---------------------------------------------------------------------------
181//  ValueArrayOf: Miscellaneous methods
182// ---------------------------------------------------------------------------
183template <class TElem> void ValueArrayOf<TElem>::
184resize(const XMLSize_t 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    XMLSize_t 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 update 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// ---------------------------------------------------------------------------
217template <class TElem> ValueArrayEnumerator<TElem>::
218ValueArrayEnumerator(ValueArrayOf<TElem>* const toEnum, const bool adopt) :
219    fAdopted(adopt)
220    , fCurIndex(0)
221    , fToEnum(toEnum)
222{
223}
224
225template <class TElem> ValueArrayEnumerator<TElem>::~ValueArrayEnumerator()
226{
227    if (fAdopted)
228        delete fToEnum;
229}
230
231
232// ---------------------------------------------------------------------------
233//  ValueArrayEnumerator: Enum interface
234// ---------------------------------------------------------------------------
235template <class TElem> bool ValueArrayEnumerator<TElem>::hasMoreElements() const
236{
237    if (fCurIndex >= fToEnum->length())
238        return false;
239    return true;
240}
241
242template <class TElem> TElem& ValueArrayEnumerator<TElem>::nextElement()
243{
244    return (*fToEnum)[fCurIndex++];
245}
246
247template <class TElem> void ValueArrayEnumerator<TElem>::Reset()
248{
249    fCurIndex = 0;
250}
251
252XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.