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