[358] | 1 | /*
|
---|
| 2 | * Copyright 1999-2002,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 | // Includes
|
---|
| 18 | // ---------------------------------------------------------------------------
|
---|
| 19 | #if defined(XERCES_TMPLSINC)
|
---|
| 20 | #include <xercesc/util/BaseRefVectorOf.hpp>
|
---|
| 21 | #endif
|
---|
| 22 |
|
---|
| 23 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
| 24 |
|
---|
| 25 | // ---------------------------------------------------------------------------
|
---|
| 26 | // BaseRefVectorOf: Constructors and Destructor
|
---|
| 27 | // ---------------------------------------------------------------------------
|
---|
| 28 | template <class TElem>
|
---|
| 29 | BaseRefVectorOf<TElem>::BaseRefVectorOf( const unsigned int maxElems
|
---|
| 30 | , const bool adoptElems
|
---|
| 31 | , MemoryManager* const manager) :
|
---|
| 32 |
|
---|
| 33 | fAdoptedElems(adoptElems)
|
---|
| 34 | , fCurCount(0)
|
---|
| 35 | , fMaxCount(maxElems)
|
---|
| 36 | , fElemList(0)
|
---|
| 37 | , fMemoryManager(manager)
|
---|
| 38 | {
|
---|
| 39 | // Allocate and initialize the array
|
---|
| 40 | fElemList = (TElem**) fMemoryManager->allocate(maxElems * sizeof(TElem*));//new TElem*[maxElems];
|
---|
| 41 | for (unsigned int index = 0; index < maxElems; index++)
|
---|
| 42 | fElemList[index] = 0;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | //implemented so code will link
|
---|
| 47 | template <class TElem> BaseRefVectorOf<TElem>::~BaseRefVectorOf()
|
---|
| 48 | {
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | // ---------------------------------------------------------------------------
|
---|
| 53 | // BaseRefVectorOf: Element management
|
---|
| 54 | // ---------------------------------------------------------------------------
|
---|
| 55 | template <class TElem> void BaseRefVectorOf<TElem>::addElement(TElem* const toAdd)
|
---|
| 56 | {
|
---|
| 57 | ensureExtraCapacity(1);
|
---|
| 58 | fElemList[fCurCount] = toAdd;
|
---|
| 59 | fCurCount++;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | template <class TElem> void
|
---|
| 64 | BaseRefVectorOf<TElem>::setElementAt(TElem* const toSet, const unsigned int setAt)
|
---|
| 65 | {
|
---|
| 66 | if (setAt >= fCurCount)
|
---|
| 67 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
| 68 |
|
---|
| 69 | if (fAdoptedElems)
|
---|
| 70 | delete fElemList[setAt];
|
---|
| 71 | fElemList[setAt] = toSet;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | template <class TElem> void BaseRefVectorOf<TElem>::
|
---|
| 75 | insertElementAt(TElem* const toInsert, const unsigned int insertAt)
|
---|
| 76 | {
|
---|
| 77 | if (insertAt == fCurCount)
|
---|
| 78 | {
|
---|
| 79 | addElement(toInsert);
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | if (insertAt > fCurCount)
|
---|
| 84 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
| 85 |
|
---|
| 86 | ensureExtraCapacity(1);
|
---|
| 87 |
|
---|
| 88 | // Make room for the newbie
|
---|
| 89 | for (unsigned int index = fCurCount; index > insertAt; index--)
|
---|
| 90 | fElemList[index] = fElemList[index-1];
|
---|
| 91 |
|
---|
| 92 | // And stick it in and bump the count
|
---|
| 93 | fElemList[insertAt] = toInsert;
|
---|
| 94 | fCurCount++;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | template <class TElem> TElem* BaseRefVectorOf<TElem>::
|
---|
| 98 | orphanElementAt(const unsigned int orphanAt)
|
---|
| 99 | {
|
---|
| 100 | if (orphanAt >= fCurCount)
|
---|
| 101 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
| 102 |
|
---|
| 103 | // Get the element we are going to orphan
|
---|
| 104 | TElem* retVal = fElemList[orphanAt];
|
---|
| 105 |
|
---|
| 106 | // Optimize if its the last element
|
---|
| 107 | if (orphanAt == fCurCount-1)
|
---|
| 108 | {
|
---|
| 109 | fElemList[orphanAt] = 0;
|
---|
| 110 | fCurCount--;
|
---|
| 111 | return retVal;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | // Copy down every element above orphan point
|
---|
| 115 | for (unsigned int index = orphanAt; index < fCurCount-1; index++)
|
---|
| 116 | fElemList[index] = fElemList[index+1];
|
---|
| 117 |
|
---|
| 118 | // Keep unused elements zero for sanity's sake
|
---|
| 119 | fElemList[fCurCount-1] = 0;
|
---|
| 120 |
|
---|
| 121 | // And bump down count
|
---|
| 122 | fCurCount--;
|
---|
| 123 |
|
---|
| 124 | return retVal;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | template <class TElem> void BaseRefVectorOf<TElem>::removeAllElements()
|
---|
| 128 | {
|
---|
| 129 | for (unsigned int index = 0; index < fCurCount; index++)
|
---|
| 130 | {
|
---|
| 131 | if (fAdoptedElems)
|
---|
| 132 | delete fElemList[index];
|
---|
| 133 |
|
---|
| 134 | // Keep unused elements zero for sanity's sake
|
---|
| 135 | fElemList[index] = 0;
|
---|
| 136 | }
|
---|
| 137 | fCurCount = 0;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | template <class TElem> void BaseRefVectorOf<TElem>::
|
---|
| 141 | removeElementAt(const unsigned int removeAt)
|
---|
| 142 | {
|
---|
| 143 | if (removeAt >= fCurCount)
|
---|
| 144 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
| 145 |
|
---|
| 146 | if (fAdoptedElems)
|
---|
| 147 | delete fElemList[removeAt];
|
---|
| 148 |
|
---|
| 149 | // Optimize if its the last element
|
---|
| 150 | if (removeAt == fCurCount-1)
|
---|
| 151 | {
|
---|
| 152 | fElemList[removeAt] = 0;
|
---|
| 153 | fCurCount--;
|
---|
| 154 | return;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | // Copy down every element above remove point
|
---|
| 158 | for (unsigned int index = removeAt; index < fCurCount-1; index++)
|
---|
| 159 | fElemList[index] = fElemList[index+1];
|
---|
| 160 |
|
---|
| 161 | // Keep unused elements zero for sanity's sake
|
---|
| 162 | fElemList[fCurCount-1] = 0;
|
---|
| 163 |
|
---|
| 164 | // And bump down count
|
---|
| 165 | fCurCount--;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | template <class TElem> void BaseRefVectorOf<TElem>::removeLastElement()
|
---|
| 169 | {
|
---|
| 170 | if (!fCurCount)
|
---|
| 171 | return;
|
---|
| 172 | fCurCount--;
|
---|
| 173 |
|
---|
| 174 | if (fAdoptedElems)
|
---|
| 175 | delete fElemList[fCurCount];
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | template <class TElem>
|
---|
| 179 | bool BaseRefVectorOf<TElem>::containsElement(const TElem* const toCheck) {
|
---|
| 180 |
|
---|
| 181 | for (unsigned int i = 0; i < fCurCount; i++) {
|
---|
| 182 | if (fElemList[i] == toCheck) {
|
---|
| 183 | return true;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | return false;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | //
|
---|
| 191 | // cleanup():
|
---|
| 192 | // similar to destructor
|
---|
| 193 | // called to cleanup the memory, in case destructor cannot be called
|
---|
| 194 | //
|
---|
| 195 | template <class TElem> void BaseRefVectorOf<TElem>::cleanup()
|
---|
| 196 | {
|
---|
| 197 | if (fAdoptedElems)
|
---|
| 198 | {
|
---|
| 199 | for (unsigned int index = 0; index < fCurCount; index++)
|
---|
| 200 | delete fElemList[index];
|
---|
| 201 | }
|
---|
| 202 | fMemoryManager->deallocate(fElemList);//delete [] fElemList;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | //
|
---|
| 206 | // reinitialize():
|
---|
| 207 | // similar to constructor
|
---|
| 208 | // called to re-construct the fElemList from scratch again
|
---|
| 209 | //
|
---|
| 210 | template <class TElem> void BaseRefVectorOf<TElem>::reinitialize()
|
---|
| 211 | {
|
---|
| 212 | // reinitialize the array
|
---|
| 213 | if (fElemList)
|
---|
| 214 | cleanup();
|
---|
| 215 |
|
---|
| 216 | fElemList = (TElem**) fMemoryManager->allocate(fMaxCount * sizeof(TElem*));//new TElem*[fMaxCount];
|
---|
| 217 | for (unsigned int index = 0; index < fMaxCount; index++)
|
---|
| 218 | fElemList[index] = 0;
|
---|
| 219 |
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | template <class TElem>
|
---|
| 223 | MemoryManager* BaseRefVectorOf<TElem>::getMemoryManager() const
|
---|
| 224 | {
|
---|
| 225 | return fMemoryManager;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | // ---------------------------------------------------------------------------
|
---|
| 230 | // BaseRefVectorOf: Getter methods
|
---|
| 231 | // ---------------------------------------------------------------------------
|
---|
| 232 | template <class TElem> unsigned int BaseRefVectorOf<TElem>::curCapacity() const
|
---|
| 233 | {
|
---|
| 234 | return fMaxCount;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | template <class TElem> const TElem* BaseRefVectorOf<TElem>::
|
---|
| 238 | elementAt(const unsigned int getAt) const
|
---|
| 239 | {
|
---|
| 240 | if (getAt >= fCurCount)
|
---|
| 241 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
| 242 | return fElemList[getAt];
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | template <class TElem> TElem*
|
---|
| 246 | BaseRefVectorOf<TElem>::elementAt(const unsigned int getAt)
|
---|
| 247 | {
|
---|
| 248 | if (getAt >= fCurCount)
|
---|
| 249 | ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
|
---|
| 250 | return fElemList[getAt];
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | template <class TElem> unsigned int BaseRefVectorOf<TElem>::size() const
|
---|
| 254 | {
|
---|
| 255 | return fCurCount;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 | // ---------------------------------------------------------------------------
|
---|
| 260 | // BaseRefVectorOf: Miscellaneous
|
---|
| 261 | // ---------------------------------------------------------------------------
|
---|
| 262 | template <class TElem> void BaseRefVectorOf<TElem>::
|
---|
| 263 | ensureExtraCapacity(const unsigned int length)
|
---|
| 264 | {
|
---|
| 265 | unsigned int newMax = fCurCount + length;
|
---|
| 266 |
|
---|
| 267 | if (newMax < fMaxCount)
|
---|
| 268 | return;
|
---|
| 269 |
|
---|
| 270 | // Avoid too many reallocations by providing a little more space
|
---|
| 271 | if (newMax < fMaxCount + 32)
|
---|
| 272 | newMax = fMaxCount + 32;
|
---|
| 273 |
|
---|
| 274 | // Allocate the new array and copy over the existing stuff
|
---|
| 275 | TElem** newList = (TElem**) fMemoryManager->allocate
|
---|
| 276 | (
|
---|
| 277 | newMax * sizeof(TElem*)
|
---|
| 278 | );//new TElem*[newMax];
|
---|
| 279 | unsigned int index = 0;
|
---|
| 280 | for (; index < fCurCount; index++)
|
---|
| 281 | newList[index] = fElemList[index];
|
---|
| 282 |
|
---|
| 283 | // Zero out the rest of them
|
---|
| 284 | for (; index < newMax; index++)
|
---|
| 285 | newList[index] = 0;
|
---|
| 286 |
|
---|
| 287 | // Clean up the old array and update our members
|
---|
| 288 | fMemoryManager->deallocate(fElemList);//delete [] fElemList;
|
---|
| 289 | fElemList = newList;
|
---|
| 290 | fMaxCount = newMax;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 |
|
---|
| 294 |
|
---|
| 295 | // ---------------------------------------------------------------------------
|
---|
| 296 | // AbstractBaseRefVectorEnumerator: Constructors and Destructor
|
---|
| 297 | // ---------------------------------------------------------------------------
|
---|
| 298 | template <class TElem> BaseRefVectorEnumerator<TElem>::
|
---|
| 299 | BaseRefVectorEnumerator( BaseRefVectorOf<TElem>* const toEnum
|
---|
| 300 | , const bool adopt) :
|
---|
| 301 | fAdopted(adopt)
|
---|
| 302 | , fCurIndex(0)
|
---|
| 303 | , fToEnum(toEnum)
|
---|
| 304 | {
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | template <class TElem> BaseRefVectorEnumerator<TElem>::~BaseRefVectorEnumerator()
|
---|
| 308 | {
|
---|
| 309 | if (fAdopted)
|
---|
| 310 | delete fToEnum;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | template <class TElem> BaseRefVectorEnumerator<TElem>::
|
---|
| 314 | BaseRefVectorEnumerator(const BaseRefVectorEnumerator<TElem>& toCopy) :
|
---|
| 315 | fAdopted(toCopy.fAdopted)
|
---|
| 316 | , fCurIndex(toCopy.fCurIndex)
|
---|
| 317 | , fToEnum(toCopy.fToEnum)
|
---|
| 318 | {
|
---|
| 319 | }
|
---|
| 320 | // ---------------------------------------------------------------------------
|
---|
| 321 | // RefBaseRefVectorEnumerator: Enum interface
|
---|
| 322 | // ---------------------------------------------------------------------------
|
---|
| 323 | template <class TElem> bool BaseRefVectorEnumerator<TElem>::hasMoreElements() const
|
---|
| 324 | {
|
---|
| 325 | if (fCurIndex >= fToEnum->size())
|
---|
| 326 | return false;
|
---|
| 327 | return true;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | template <class TElem> TElem& BaseRefVectorEnumerator<TElem>::nextElement()
|
---|
| 331 | {
|
---|
| 332 | return *(fToEnum->elementAt(fCurIndex++));
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | template <class TElem> void BaseRefVectorEnumerator<TElem>::Reset()
|
---|
| 336 | {
|
---|
| 337 | fCurIndex = 0;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | XERCES_CPP_NAMESPACE_END
|
---|