Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:11

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  * 
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  * 
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 // ---------------------------------------------------------------------------
0018 //  Includes
0019 // ---------------------------------------------------------------------------
0020 #if defined(XERCES_TMPLSINC)
0021 #include <xercesc/util/BaseRefVectorOf.hpp>
0022 #endif
0023 
0024 XERCES_CPP_NAMESPACE_BEGIN
0025 
0026 // ---------------------------------------------------------------------------
0027 //  BaseRefVectorOf: Constructors and Destructor
0028 // ---------------------------------------------------------------------------
0029 template <class TElem>
0030 BaseRefVectorOf<TElem>::BaseRefVectorOf( const XMLSize_t maxElems
0031                                        , const bool adoptElems
0032                                        , MemoryManager* const manager) :
0033 
0034     fAdoptedElems(adoptElems)
0035     , fCurCount(0)
0036     , fMaxCount(maxElems)
0037     , fElemList(0)
0038     , fMemoryManager(manager)
0039 {
0040     // Allocate and initialize the array
0041     fElemList = (TElem**) fMemoryManager->allocate(maxElems * sizeof(TElem*));//new TElem*[maxElems];
0042     for (XMLSize_t index = 0; index < maxElems; index++)
0043         fElemList[index] = 0;
0044 }
0045 
0046 
0047 //implemented so code will link
0048 template <class TElem> BaseRefVectorOf<TElem>::~BaseRefVectorOf()
0049 {
0050 }
0051 
0052 
0053 // ---------------------------------------------------------------------------
0054 //  BaseRefVectorOf: Element management
0055 // ---------------------------------------------------------------------------
0056 template <class TElem> void BaseRefVectorOf<TElem>::addElement(TElem* const toAdd)
0057 {
0058     ensureExtraCapacity(1);
0059     fElemList[fCurCount] = toAdd;
0060     fCurCount++;
0061 }
0062 
0063 
0064 template <class TElem> void
0065 BaseRefVectorOf<TElem>::setElementAt(TElem* const toSet, const XMLSize_t setAt)
0066 {
0067     if (setAt >= fCurCount)
0068         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
0069 
0070     if (fAdoptedElems)
0071         delete fElemList[setAt];
0072     fElemList[setAt] = toSet;
0073 }
0074 
0075 template <class TElem> void BaseRefVectorOf<TElem>::
0076 insertElementAt(TElem* const toInsert, const XMLSize_t insertAt)
0077 {
0078     if (insertAt == fCurCount)
0079     {
0080         addElement(toInsert);
0081         return;
0082     }
0083 
0084     if (insertAt > fCurCount)
0085         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
0086 
0087     ensureExtraCapacity(1);
0088 
0089     // Make room for the newbie
0090     for (XMLSize_t index = fCurCount; index > insertAt; index--)
0091         fElemList[index] = fElemList[index-1];
0092 
0093     // And stick it in and bump the count
0094     fElemList[insertAt] = toInsert;
0095     fCurCount++;
0096 }
0097 
0098 template <class TElem> TElem* BaseRefVectorOf<TElem>::
0099 orphanElementAt(const XMLSize_t orphanAt)
0100 {
0101     if (orphanAt >= fCurCount)
0102         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
0103 
0104     // Get the element we are going to orphan
0105     TElem* retVal = fElemList[orphanAt];
0106 
0107     // Optimize if its the last element
0108     if (orphanAt == fCurCount-1)
0109     {
0110         fElemList[orphanAt] = 0;
0111         fCurCount--;
0112         return retVal;
0113     }
0114 
0115     // Copy down every element above orphan point
0116     for (XMLSize_t index = orphanAt; index < fCurCount-1; index++)
0117         fElemList[index] = fElemList[index+1];
0118 
0119     // Keep unused elements zero for sanity's sake
0120     fElemList[fCurCount-1] = 0;
0121 
0122     // And bump down count
0123     fCurCount--;
0124 
0125     return retVal;
0126 }
0127 
0128 template <class TElem> void BaseRefVectorOf<TElem>::removeAllElements()
0129 {
0130     for (XMLSize_t index = 0; index < fCurCount; index++)
0131     {
0132         if (fAdoptedElems)
0133           delete fElemList[index];
0134 
0135         // Keep unused elements zero for sanity's sake
0136         fElemList[index] = 0;
0137     }
0138     fCurCount = 0;
0139 }
0140 
0141 template <class TElem> void BaseRefVectorOf<TElem>::
0142 removeElementAt(const XMLSize_t removeAt)
0143 {
0144     if (removeAt >= fCurCount)
0145         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
0146 
0147     if (fAdoptedElems)
0148         delete fElemList[removeAt];
0149 
0150     // Optimize if its the last element
0151     if (removeAt == fCurCount-1)
0152     {
0153         fElemList[removeAt] = 0;
0154         fCurCount--;
0155         return;
0156     }
0157 
0158     // Copy down every element above remove point
0159     for (XMLSize_t index = removeAt; index < fCurCount-1; index++)
0160         fElemList[index] = fElemList[index+1];
0161 
0162     // Keep unused elements zero for sanity's sake
0163     fElemList[fCurCount-1] = 0;
0164 
0165     // And bump down count
0166     fCurCount--;
0167 }
0168 
0169 template <class TElem> void BaseRefVectorOf<TElem>::removeLastElement()
0170 {
0171     if (!fCurCount)
0172         return;
0173     fCurCount--;
0174 
0175     if (fAdoptedElems)
0176         delete fElemList[fCurCount];
0177 }
0178 
0179 template <class TElem>
0180 bool BaseRefVectorOf<TElem>::containsElement(const TElem* const toCheck) {
0181 
0182     for (XMLSize_t i = 0; i < fCurCount; i++) {
0183         if (fElemList[i] == toCheck) {
0184             return true;
0185         }
0186     }
0187 
0188     return false;
0189 }
0190 
0191 //
0192 // cleanup():
0193 //   similar to destructor
0194 //   called to cleanup the memory, in case destructor cannot be called
0195 //
0196 template <class TElem> void BaseRefVectorOf<TElem>::cleanup()
0197 {
0198     if (fAdoptedElems)
0199     {
0200         for (XMLSize_t index = 0; index < fCurCount; index++)
0201             delete fElemList[index];
0202     }
0203     fMemoryManager->deallocate(fElemList);//delete [] fElemList;
0204 }
0205 
0206 //
0207 // reinitialize():
0208 //   similar to constructor
0209 //   called to re-construct the fElemList from scratch again
0210 //
0211 template <class TElem> void BaseRefVectorOf<TElem>::reinitialize()
0212 {
0213     // reinitialize the array
0214     if (fElemList)
0215         cleanup();
0216 
0217     fElemList = (TElem**) fMemoryManager->allocate(fMaxCount * sizeof(TElem*));//new TElem*[fMaxCount];
0218     for (XMLSize_t index = 0; index < fMaxCount; index++)
0219         fElemList[index] = 0;
0220 
0221 }
0222 
0223 template <class TElem>
0224 MemoryManager* BaseRefVectorOf<TElem>::getMemoryManager() const
0225 {
0226     return fMemoryManager;
0227 }
0228 
0229 
0230 // ---------------------------------------------------------------------------
0231 //  BaseRefVectorOf: Getter methods
0232 // ---------------------------------------------------------------------------
0233 template <class TElem> XMLSize_t BaseRefVectorOf<TElem>::curCapacity() const
0234 {
0235     return fMaxCount;
0236 }
0237 
0238 template <class TElem> const TElem* BaseRefVectorOf<TElem>::
0239 elementAt(const XMLSize_t getAt) const
0240 {
0241     if (getAt >= fCurCount)
0242         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
0243     return fElemList[getAt];
0244 }
0245 
0246 template <class TElem> TElem*
0247 BaseRefVectorOf<TElem>::elementAt(const XMLSize_t getAt)
0248 {
0249     if (getAt >= fCurCount)
0250         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
0251     return fElemList[getAt];
0252 }
0253 
0254 template <class TElem> XMLSize_t BaseRefVectorOf<TElem>::size() const
0255 {
0256     return fCurCount;
0257 }
0258 
0259 
0260 // ---------------------------------------------------------------------------
0261 //  BaseRefVectorOf: Miscellaneous
0262 // ---------------------------------------------------------------------------
0263 template <class TElem> void BaseRefVectorOf<TElem>::
0264 ensureExtraCapacity(const XMLSize_t length)
0265 {
0266     XMLSize_t newMax = fCurCount + length;
0267 
0268     if (newMax <= fMaxCount)
0269         return;
0270 
0271     // Choose how much bigger based on the current size.
0272     // This will grow half as much again.
0273     if (newMax < fMaxCount + fMaxCount/2)
0274         newMax = fMaxCount + fMaxCount/2;
0275 
0276     // Allocate the new array and copy over the existing stuff
0277     TElem** newList = (TElem**) fMemoryManager->allocate
0278     (
0279         newMax * sizeof(TElem*)
0280     );//new TElem*[newMax];
0281     XMLSize_t index = 0;
0282     for (; index < fCurCount; index++)
0283         newList[index] = fElemList[index];
0284 
0285     // Zero out the rest of them
0286     for (; index < newMax; index++)
0287         newList[index] = 0;
0288 
0289     // Clean up the old array and update our members
0290     fMemoryManager->deallocate(fElemList);//delete [] fElemList;
0291     fElemList = newList;
0292     fMaxCount = newMax;
0293 }
0294 
0295 
0296 
0297 // ---------------------------------------------------------------------------
0298 //  AbstractBaseRefVectorEnumerator: Constructors and Destructor
0299 // ---------------------------------------------------------------------------
0300 template <class TElem> BaseRefVectorEnumerator<TElem>::
0301 BaseRefVectorEnumerator(        BaseRefVectorOf<TElem>* const   toEnum
0302                     , const bool                        adopt) :
0303     fAdopted(adopt)
0304     , fCurIndex(0)
0305     , fToEnum(toEnum)
0306 {
0307 }
0308 
0309 template <class TElem> BaseRefVectorEnumerator<TElem>::~BaseRefVectorEnumerator()
0310 {
0311     if (fAdopted)
0312         delete fToEnum;
0313 }
0314 
0315 template <class TElem> BaseRefVectorEnumerator<TElem>::
0316 BaseRefVectorEnumerator(const BaseRefVectorEnumerator<TElem>& toCopy) :
0317     XMLEnumerator<TElem>(toCopy)
0318     , XMemory(toCopy)
0319     , fAdopted(toCopy.fAdopted)
0320     , fCurIndex(toCopy.fCurIndex)
0321     , fToEnum(toCopy.fToEnum)    
0322 {
0323 }
0324 // ---------------------------------------------------------------------------
0325 //  RefBaseRefVectorEnumerator: Enum interface
0326 // ---------------------------------------------------------------------------
0327 template <class TElem> bool BaseRefVectorEnumerator<TElem>::hasMoreElements() const
0328 {
0329     if (fCurIndex >= fToEnum->size())
0330         return false;
0331     return true;
0332 }
0333 
0334 template <class TElem> TElem& BaseRefVectorEnumerator<TElem>::nextElement()
0335 {
0336     return *(fToEnum->elementAt(fCurIndex++));
0337 }
0338 
0339 template <class TElem> void BaseRefVectorEnumerator<TElem>::Reset()
0340 {
0341     fCurIndex = 0;
0342 }
0343 
0344 XERCES_CPP_NAMESPACE_END