[2674] | 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: RefArrayOf.c 568078 2007-08-21 11:43:25Z amassari $ |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | // --------------------------------------------------------------------------- |
---|
| 24 | // Includes |
---|
| 25 | // --------------------------------------------------------------------------- |
---|
| 26 | #if defined(XERCES_TMPLSINC) |
---|
| 27 | #include <xercesc/util/RefArrayOf.hpp> |
---|
| 28 | #endif |
---|
| 29 | |
---|
| 30 | XERCES_CPP_NAMESPACE_BEGIN |
---|
| 31 | |
---|
| 32 | // --------------------------------------------------------------------------- |
---|
| 33 | // RefArrayOf: Contructors and Destructor |
---|
| 34 | // --------------------------------------------------------------------------- |
---|
| 35 | template <class TElem> |
---|
| 36 | RefArrayOf<TElem>::RefArrayOf(const unsigned int size, |
---|
| 37 | MemoryManager* const manager) : |
---|
| 38 | |
---|
| 39 | fSize(size) |
---|
| 40 | , fArray(0) |
---|
| 41 | , fMemoryManager(manager) |
---|
| 42 | { |
---|
| 43 | fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize]; |
---|
| 44 | for (unsigned int index = 0; index < fSize; index++) |
---|
| 45 | fArray[index] = 0; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | template <class TElem> |
---|
| 49 | RefArrayOf<TElem>::RefArrayOf(TElem* values[], |
---|
| 50 | const unsigned int size, |
---|
| 51 | MemoryManager* const manager) : |
---|
| 52 | |
---|
| 53 | fSize(size) |
---|
| 54 | , fArray(0) |
---|
| 55 | , fMemoryManager(manager) |
---|
| 56 | { |
---|
| 57 | fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize]; |
---|
| 58 | for (unsigned int index = 0; index < fSize; index++) |
---|
| 59 | fArray[index] = values[index]; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | template <class TElem> RefArrayOf<TElem>:: |
---|
| 63 | RefArrayOf(const RefArrayOf<TElem>& source) : |
---|
| 64 | |
---|
| 65 | fSize(source.fSize) |
---|
| 66 | , fArray(0) |
---|
| 67 | , fMemoryManager(source.fMemoryManager) |
---|
| 68 | { |
---|
| 69 | fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize]; |
---|
| 70 | for (unsigned int index = 0; index < fSize; index++) |
---|
| 71 | fArray[index] = source.fArray[index]; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | template <class TElem> RefArrayOf<TElem>::~RefArrayOf() |
---|
| 75 | { |
---|
| 76 | fMemoryManager->deallocate(fArray);//delete [] fArray; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | // --------------------------------------------------------------------------- |
---|
| 81 | // RefArrayOf: Public operators |
---|
| 82 | // --------------------------------------------------------------------------- |
---|
| 83 | template <class TElem> TElem*& RefArrayOf<TElem>:: |
---|
| 84 | operator[](const unsigned int index) |
---|
| 85 | { |
---|
| 86 | if (index >= fSize) |
---|
| 87 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager); |
---|
| 88 | return fArray[index]; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | template <class TElem> const TElem* RefArrayOf<TElem>:: |
---|
| 92 | operator[](const unsigned int index) const |
---|
| 93 | { |
---|
| 94 | if (index >= fSize) |
---|
| 95 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager); |
---|
| 96 | return fArray[index]; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | template <class TElem> RefArrayOf<TElem>& RefArrayOf<TElem>:: |
---|
| 100 | operator=(const RefArrayOf<TElem>& toAssign) |
---|
| 101 | { |
---|
| 102 | if (this == &toAssign) |
---|
| 103 | return *this; |
---|
| 104 | |
---|
| 105 | // Reallocate if not the same size |
---|
| 106 | if (toAssign.fSize != fSize) |
---|
| 107 | { |
---|
| 108 | fMemoryManager->deallocate(fArray);//delete [] fArray; |
---|
| 109 | fSize = toAssign.fSize; |
---|
| 110 | fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize]; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | // Copy over the source elements |
---|
| 114 | for (unsigned int index = 0; index < fSize; index++) |
---|
| 115 | fArray[index] = toAssign.fArray[index]; |
---|
| 116 | |
---|
| 117 | return *this; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | template <class TElem> bool RefArrayOf<TElem>:: |
---|
| 121 | operator==(const RefArrayOf<TElem>& toCompare) const |
---|
| 122 | { |
---|
| 123 | if (this == &toCompare) |
---|
| 124 | return true; |
---|
| 125 | |
---|
| 126 | if (fSize != toCompare.fSize) |
---|
| 127 | return false; |
---|
| 128 | |
---|
| 129 | for (unsigned int index = 0; index < fSize; index++) |
---|
| 130 | { |
---|
| 131 | if (fArray[index] != toCompare.fArray[index]) |
---|
| 132 | return false; |
---|
| 133 | } |
---|
| 134 | return true; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | template <class TElem> bool RefArrayOf<TElem>:: |
---|
| 138 | operator!=(const RefArrayOf<TElem>& toCompare) const |
---|
| 139 | { |
---|
| 140 | return !operator==(toCompare); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | // --------------------------------------------------------------------------- |
---|
| 145 | // RefArrayOf: Copy operations |
---|
| 146 | // --------------------------------------------------------------------------- |
---|
| 147 | template <class TElem> unsigned int RefArrayOf<TElem>:: |
---|
| 148 | copyFrom(const RefArrayOf<TElem>& srcArray) |
---|
| 149 | { |
---|
| 150 | // |
---|
| 151 | // Copy over as many of the source elements as will fit into |
---|
| 152 | // this array. |
---|
| 153 | // |
---|
| 154 | const unsigned int count = fSize < srcArray.fSize ? |
---|
| 155 | fSize : srcArray.fSize; |
---|
| 156 | |
---|
| 157 | for (unsigned int index = 0; index < fSize; index++) |
---|
| 158 | fArray[index] = srcArray.fArray[index]; |
---|
| 159 | |
---|
| 160 | return count; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | // --------------------------------------------------------------------------- |
---|
| 165 | // RefArrayOf: Getter methods |
---|
| 166 | // --------------------------------------------------------------------------- |
---|
| 167 | template <class TElem> unsigned int RefArrayOf<TElem>::length() const |
---|
| 168 | { |
---|
| 169 | return fSize; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | template <class TElem> TElem** RefArrayOf<TElem>::rawData() const |
---|
| 173 | { |
---|
| 174 | return fArray; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | // --------------------------------------------------------------------------- |
---|
| 179 | // RefArrayOf: Element management methods |
---|
| 180 | // --------------------------------------------------------------------------- |
---|
| 181 | template <class TElem> void RefArrayOf<TElem>::deleteAt(const unsigned int index) |
---|
| 182 | { |
---|
| 183 | if (index >= fSize) |
---|
| 184 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager); |
---|
| 185 | |
---|
| 186 | delete fArray[index]; |
---|
| 187 | fArray[index] = 0; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | template <class TElem> void RefArrayOf<TElem>::deleteAllElements() |
---|
| 191 | { |
---|
| 192 | for (unsigned int index = 0; index < fSize; index++) |
---|
| 193 | { |
---|
| 194 | delete fArray[index]; |
---|
| 195 | fArray[index] = 0; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | template <class TElem> void RefArrayOf<TElem>::resize(const unsigned int newSize) |
---|
| 200 | { |
---|
| 201 | if (newSize == fSize) |
---|
| 202 | return; |
---|
| 203 | |
---|
| 204 | if (newSize < fSize) |
---|
| 205 | ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Array_BadNewSize, fMemoryManager); |
---|
| 206 | |
---|
| 207 | // Allocate the new array |
---|
| 208 | TElem** newArray = (TElem**) fMemoryManager->allocate |
---|
| 209 | ( |
---|
| 210 | newSize * sizeof(TElem*) |
---|
| 211 | );//new TElem*[newSize]; |
---|
| 212 | |
---|
| 213 | // Copy the existing values |
---|
| 214 | unsigned int index = 0; |
---|
| 215 | for (; index < fSize; index++) |
---|
| 216 | newArray[index] = fArray[index]; |
---|
| 217 | |
---|
| 218 | for (; index < newSize; index++) |
---|
| 219 | newArray[index] = 0; |
---|
| 220 | |
---|
| 221 | // Delete the old array and udpate our members |
---|
| 222 | fMemoryManager->deallocate(fArray);//delete [] fArray; |
---|
| 223 | fArray = newArray; |
---|
| 224 | fSize = newSize; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | // --------------------------------------------------------------------------- |
---|
| 231 | // RefArrayEnumerator: Constructors and Destructor |
---|
| 232 | // --------------------------------------------------------------------------- |
---|
| 233 | template <class TElem> RefArrayEnumerator<TElem>:: |
---|
| 234 | RefArrayEnumerator( RefArrayOf<TElem>* const toEnum |
---|
| 235 | , const bool adopt) : |
---|
| 236 | fAdopted(adopt) |
---|
| 237 | , fCurIndex(0) |
---|
| 238 | , fToEnum(toEnum) |
---|
| 239 | { |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | template <class TElem> RefArrayEnumerator<TElem>::~RefArrayEnumerator() |
---|
| 243 | { |
---|
| 244 | if (fAdopted) |
---|
| 245 | delete fToEnum; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | // --------------------------------------------------------------------------- |
---|
| 250 | // RefArrayEnumerator: Enum interface |
---|
| 251 | // --------------------------------------------------------------------------- |
---|
| 252 | template <class TElem> bool RefArrayEnumerator<TElem>::hasMoreElements() const |
---|
| 253 | { |
---|
| 254 | if (fCurIndex >= fToEnum->length()) |
---|
| 255 | return false; |
---|
| 256 | return true; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | template <class TElem> TElem& RefArrayEnumerator<TElem>::nextElement() |
---|
| 260 | { |
---|
| 261 | return *(*fToEnum)[fCurIndex++]; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | template <class TElem> void RefArrayEnumerator<TElem>::Reset() |
---|
| 265 | { |
---|
| 266 | fCurIndex = 0; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | XERCES_CPP_NAMESPACE_END |
---|