secureswitools/swisistools/source/xmlparser/xerces/include/xercesc/util/BaseRefVectorOf.c
changeset 0 ba25891c3a9e
child 1 c42dffbd5b4f
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 /*
       
    18  * Licensed to the Apache Software Foundation (ASF) under one or more
       
    19  * contributor license agreements.  See the NOTICE file distributed with
       
    20  * this work for additional information regarding copyright ownership.
       
    21  * The ASF licenses this file to You under the Apache License, Version 2.0
       
    22  * (the "License"); you may not use this file except in compliance with
       
    23  * the License.  You may obtain a copy of the License at
       
    24  * 
       
    25  *      http://www.apache.org/licenses/LICENSE-2.0
       
    26  * 
       
    27  * Unless required by applicable law or agreed to in writing, software
       
    28  * distributed under the License is distributed on an "AS IS" BASIS,
       
    29  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    30  * See the License for the specific language governing permissions and
       
    31  * limitations under the License.
       
    32  */
       
    33 // ---------------------------------------------------------------------------
       
    34 //  Includes
       
    35 // ---------------------------------------------------------------------------
       
    36 #if defined(XERCES_TMPLSINC)
       
    37 #include <xercesc/util/BaseRefVectorOf.hpp>
       
    38 #endif
       
    39 
       
    40 XERCES_CPP_NAMESPACE_BEGIN
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 //  BaseRefVectorOf: Constructors and Destructor
       
    44 // ---------------------------------------------------------------------------
       
    45 template <class TElem>
       
    46 BaseRefVectorOf<TElem>::BaseRefVectorOf( const unsigned int maxElems
       
    47                                        , const bool adoptElems
       
    48                                        , MemoryManager* const manager) :
       
    49 
       
    50     fAdoptedElems(adoptElems)
       
    51     , fCurCount(0)
       
    52     , fMaxCount(maxElems)
       
    53     , fElemList(0)
       
    54     , fMemoryManager(manager)
       
    55 {
       
    56     // Allocate and initialize the array
       
    57     fElemList = (TElem**) fMemoryManager->allocate(maxElems * sizeof(TElem*));//new TElem*[maxElems];
       
    58     for (unsigned int index = 0; index < maxElems; index++)
       
    59         fElemList[index] = 0;
       
    60 }
       
    61 
       
    62 
       
    63 //implemented so code will link
       
    64 template <class TElem> BaseRefVectorOf<TElem>::~BaseRefVectorOf()
       
    65 {
       
    66 }
       
    67 
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 //  BaseRefVectorOf: Element management
       
    71 // ---------------------------------------------------------------------------
       
    72 template <class TElem> void BaseRefVectorOf<TElem>::addElement(TElem* const toAdd)
       
    73 {
       
    74     ensureExtraCapacity(1);
       
    75     fElemList[fCurCount] = toAdd;
       
    76     fCurCount++;
       
    77 }
       
    78 
       
    79 
       
    80 template <class TElem> void
       
    81 BaseRefVectorOf<TElem>::setElementAt(TElem* const toSet, const unsigned int setAt)
       
    82 {
       
    83     if (setAt >= fCurCount)
       
    84         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
       
    85 
       
    86     if (fAdoptedElems)
       
    87         delete fElemList[setAt];
       
    88     fElemList[setAt] = toSet;
       
    89 }
       
    90 
       
    91 template <class TElem> void BaseRefVectorOf<TElem>::
       
    92 insertElementAt(TElem* const toInsert, const unsigned int insertAt)
       
    93 {
       
    94     if (insertAt == fCurCount)
       
    95     {
       
    96         addElement(toInsert);
       
    97         return;
       
    98     }
       
    99 
       
   100     if (insertAt > fCurCount)
       
   101         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
       
   102 
       
   103     ensureExtraCapacity(1);
       
   104 
       
   105     // Make room for the newbie
       
   106     for (unsigned int index = fCurCount; index > insertAt; index--)
       
   107         fElemList[index] = fElemList[index-1];
       
   108 
       
   109     // And stick it in and bump the count
       
   110     fElemList[insertAt] = toInsert;
       
   111     fCurCount++;
       
   112 }
       
   113 
       
   114 template <class TElem> TElem* BaseRefVectorOf<TElem>::
       
   115 orphanElementAt(const unsigned int orphanAt)
       
   116 {
       
   117     if (orphanAt >= fCurCount)
       
   118         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
       
   119 
       
   120     // Get the element we are going to orphan
       
   121     TElem* retVal = fElemList[orphanAt];
       
   122 
       
   123     // Optimize if its the last element
       
   124     if (orphanAt == fCurCount-1)
       
   125     {
       
   126         fElemList[orphanAt] = 0;
       
   127         fCurCount--;
       
   128         return retVal;
       
   129     }
       
   130 
       
   131     // Copy down every element above orphan point
       
   132     for (unsigned int index = orphanAt; index < fCurCount-1; index++)
       
   133         fElemList[index] = fElemList[index+1];
       
   134 
       
   135     // Keep unused elements zero for sanity's sake
       
   136     fElemList[fCurCount-1] = 0;
       
   137 
       
   138     // And bump down count
       
   139     fCurCount--;
       
   140 
       
   141     return retVal;
       
   142 }
       
   143 
       
   144 template <class TElem> void BaseRefVectorOf<TElem>::removeAllElements()
       
   145 {
       
   146     for (unsigned int index = 0; index < fCurCount; index++)
       
   147     {
       
   148         if (fAdoptedElems)
       
   149           delete fElemList[index];
       
   150 
       
   151         // Keep unused elements zero for sanity's sake
       
   152         fElemList[index] = 0;
       
   153     }
       
   154     fCurCount = 0;
       
   155 }
       
   156 
       
   157 template <class TElem> void BaseRefVectorOf<TElem>::
       
   158 removeElementAt(const unsigned int removeAt)
       
   159 {
       
   160     if (removeAt >= fCurCount)
       
   161         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
       
   162 
       
   163     if (fAdoptedElems)
       
   164         delete fElemList[removeAt];
       
   165 
       
   166     // Optimize if its the last element
       
   167     if (removeAt == fCurCount-1)
       
   168     {
       
   169         fElemList[removeAt] = 0;
       
   170         fCurCount--;
       
   171         return;
       
   172     }
       
   173 
       
   174     // Copy down every element above remove point
       
   175     for (unsigned int index = removeAt; index < fCurCount-1; index++)
       
   176         fElemList[index] = fElemList[index+1];
       
   177 
       
   178     // Keep unused elements zero for sanity's sake
       
   179     fElemList[fCurCount-1] = 0;
       
   180 
       
   181     // And bump down count
       
   182     fCurCount--;
       
   183 }
       
   184 
       
   185 template <class TElem> void BaseRefVectorOf<TElem>::removeLastElement()
       
   186 {
       
   187     if (!fCurCount)
       
   188         return;
       
   189     fCurCount--;
       
   190 
       
   191     if (fAdoptedElems)
       
   192         delete fElemList[fCurCount];
       
   193 }
       
   194 
       
   195 template <class TElem>
       
   196 bool BaseRefVectorOf<TElem>::containsElement(const TElem* const toCheck) {
       
   197 
       
   198     for (unsigned int i = 0; i < fCurCount; i++) {
       
   199         if (fElemList[i] == toCheck) {
       
   200             return true;
       
   201         }
       
   202     }
       
   203 
       
   204     return false;
       
   205 }
       
   206 
       
   207 //
       
   208 // cleanup():
       
   209 //   similar to destructor
       
   210 //   called to cleanup the memory, in case destructor cannot be called
       
   211 //
       
   212 template <class TElem> void BaseRefVectorOf<TElem>::cleanup()
       
   213 {
       
   214     if (fAdoptedElems)
       
   215     {
       
   216         for (unsigned int index = 0; index < fCurCount; index++)
       
   217             delete fElemList[index];
       
   218     }
       
   219     fMemoryManager->deallocate(fElemList);//delete [] fElemList;
       
   220 }
       
   221 
       
   222 //
       
   223 // reinitialize():
       
   224 //   similar to constructor
       
   225 //   called to re-construct the fElemList from scratch again
       
   226 //
       
   227 template <class TElem> void BaseRefVectorOf<TElem>::reinitialize()
       
   228 {
       
   229     // reinitialize the array
       
   230     if (fElemList)
       
   231         cleanup();
       
   232 
       
   233     fElemList = (TElem**) fMemoryManager->allocate(fMaxCount * sizeof(TElem*));//new TElem*[fMaxCount];
       
   234     for (unsigned int index = 0; index < fMaxCount; index++)
       
   235         fElemList[index] = 0;
       
   236 
       
   237 }
       
   238 
       
   239 template <class TElem>
       
   240 MemoryManager* BaseRefVectorOf<TElem>::getMemoryManager() const
       
   241 {
       
   242     return fMemoryManager;
       
   243 }
       
   244 
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 //  BaseRefVectorOf: Getter methods
       
   248 // ---------------------------------------------------------------------------
       
   249 template <class TElem> unsigned int BaseRefVectorOf<TElem>::curCapacity() const
       
   250 {
       
   251     return fMaxCount;
       
   252 }
       
   253 
       
   254 template <class TElem> const TElem* BaseRefVectorOf<TElem>::
       
   255 elementAt(const unsigned int getAt) const
       
   256 {
       
   257     if (getAt >= fCurCount)
       
   258         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
       
   259     return fElemList[getAt];
       
   260 }
       
   261 
       
   262 template <class TElem> TElem*
       
   263 BaseRefVectorOf<TElem>::elementAt(const unsigned int getAt)
       
   264 {
       
   265     if (getAt >= fCurCount)
       
   266         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
       
   267     return fElemList[getAt];
       
   268 }
       
   269 
       
   270 template <class TElem> unsigned int BaseRefVectorOf<TElem>::size() const
       
   271 {
       
   272     return fCurCount;
       
   273 }
       
   274 
       
   275 
       
   276 // ---------------------------------------------------------------------------
       
   277 //  BaseRefVectorOf: Miscellaneous
       
   278 // ---------------------------------------------------------------------------
       
   279 template <class TElem> void BaseRefVectorOf<TElem>::
       
   280 ensureExtraCapacity(const unsigned int length)
       
   281 {
       
   282     unsigned int newMax = fCurCount + length;
       
   283 
       
   284     if (newMax <= fMaxCount)
       
   285         return;
       
   286 
       
   287 	// Choose how much bigger based on the current size.
       
   288 	// This will grow half as much again.
       
   289     if (newMax < fMaxCount + fMaxCount/2)
       
   290         newMax = fMaxCount + fMaxCount/2;
       
   291 
       
   292     // Allocate the new array and copy over the existing stuff
       
   293     TElem** newList = (TElem**) fMemoryManager->allocate
       
   294     (
       
   295         newMax * sizeof(TElem*)
       
   296     );//new TElem*[newMax];
       
   297     unsigned int index = 0;
       
   298     for (; index < fCurCount; index++)
       
   299         newList[index] = fElemList[index];
       
   300 
       
   301     // Zero out the rest of them
       
   302     for (; index < newMax; index++)
       
   303         newList[index] = 0;
       
   304 
       
   305     // Clean up the old array and update our members
       
   306     fMemoryManager->deallocate(fElemList);//delete [] fElemList;
       
   307     fElemList = newList;
       
   308     fMaxCount = newMax;
       
   309 }
       
   310 
       
   311 
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 //  AbstractBaseRefVectorEnumerator: Constructors and Destructor
       
   315 // ---------------------------------------------------------------------------
       
   316 template <class TElem> BaseRefVectorEnumerator<TElem>::
       
   317 BaseRefVectorEnumerator(        BaseRefVectorOf<TElem>* const   toEnum
       
   318                     , const bool                        adopt) :
       
   319     fAdopted(adopt)
       
   320     , fCurIndex(0)
       
   321     , fToEnum(toEnum)
       
   322 {
       
   323 }
       
   324 
       
   325 template <class TElem> BaseRefVectorEnumerator<TElem>::~BaseRefVectorEnumerator()
       
   326 {
       
   327     if (fAdopted)
       
   328         delete fToEnum;
       
   329 }
       
   330 
       
   331 template <class TElem> BaseRefVectorEnumerator<TElem>::
       
   332 BaseRefVectorEnumerator(const BaseRefVectorEnumerator<TElem>& toCopy) :
       
   333     XMLEnumerator<TElem>(toCopy)
       
   334     , XMemory(toCopy)
       
   335     , fAdopted(toCopy.fAdopted)
       
   336     , fCurIndex(toCopy.fCurIndex)
       
   337     , fToEnum(toCopy.fToEnum)    
       
   338 {
       
   339 }
       
   340 // ---------------------------------------------------------------------------
       
   341 //  RefBaseRefVectorEnumerator: Enum interface
       
   342 // ---------------------------------------------------------------------------
       
   343 template <class TElem> bool BaseRefVectorEnumerator<TElem>::hasMoreElements() const
       
   344 {
       
   345     if (fCurIndex >= fToEnum->size())
       
   346         return false;
       
   347     return true;
       
   348 }
       
   349 
       
   350 template <class TElem> TElem& BaseRefVectorEnumerator<TElem>::nextElement()
       
   351 {
       
   352     return *(fToEnum->elementAt(fCurIndex++));
       
   353 }
       
   354 
       
   355 template <class TElem> void BaseRefVectorEnumerator<TElem>::Reset()
       
   356 {
       
   357     fCurIndex = 0;
       
   358 }
       
   359 
       
   360 XERCES_CPP_NAMESPACE_END