GME  13
RefArrayVectorOf.c
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 // ---------------------------------------------------------------------------
00019 //  Includes
00020 // ---------------------------------------------------------------------------
00021 #if defined(XERCES_TMPLSINC)
00022 #include "RefArrayVectorOf.hpp"
00023 #endif
00024 
00025 XERCES_CPP_NAMESPACE_BEGIN
00026 
00027 // ---------------------------------------------------------------------------
00028 //  RefArrayVectorOf: Constructor and Destructor
00029 // ---------------------------------------------------------------------------
00030 template <class TElem>
00031 RefArrayVectorOf<TElem>::RefArrayVectorOf( const XMLSize_t maxElems
00032                                          , const bool adoptElems
00033                                          , MemoryManager* const manager)
00034     : BaseRefVectorOf<TElem>(maxElems, adoptElems, manager)
00035 {
00036 }
00037 
00038 
00039 template <class TElem> RefArrayVectorOf<TElem>::~RefArrayVectorOf()
00040 {
00041     if (this->fAdoptedElems)
00042     {
00043         for (XMLSize_t index = 0; index < this->fCurCount; index++)
00044             this->fMemoryManager->deallocate(this->fElemList[index]);//delete[] fElemList[index];
00045     }
00046     this->fMemoryManager->deallocate(this->fElemList);//delete [] fElemList;
00047 }
00048 
00049 template <class TElem> void
00050 RefArrayVectorOf<TElem>::setElementAt(TElem* const toSet, const XMLSize_t setAt)
00051 {
00052     if (setAt >= this->fCurCount)
00053         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, this->fMemoryManager);
00054 
00055     if (this->fAdoptedElems)
00056         this->fMemoryManager->deallocate(this->fElemList[setAt]);
00057 
00058     this->fElemList[setAt] = toSet;
00059 }
00060 
00061 template <class TElem> void RefArrayVectorOf<TElem>::removeAllElements()
00062 {
00063     for (XMLSize_t index = 0; index < this->fCurCount; index++)
00064     {
00065         if (this->fAdoptedElems)
00066             this->fMemoryManager->deallocate(this->fElemList[index]);
00067 
00068         // Keep unused elements zero for sanity's sake
00069         this->fElemList[index] = 0;
00070     }
00071     this->fCurCount = 0;
00072 }
00073 
00074 template <class TElem> void RefArrayVectorOf<TElem>::
00075 removeElementAt(const XMLSize_t removeAt)
00076 {
00077     if (removeAt >= this->fCurCount)
00078         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, this->fMemoryManager);
00079 
00080     if (this->fAdoptedElems)
00081         this->fMemoryManager->deallocate(this->fElemList[removeAt]);
00082 
00083     // Optimize if its the last element
00084     if (removeAt == this->fCurCount-1)
00085     {
00086         this->fElemList[removeAt] = 0;
00087         this->fCurCount--;
00088         return;
00089     }
00090 
00091     // Copy down every element above remove point
00092     for (XMLSize_t index = removeAt; index < this->fCurCount-1; index++)
00093         this->fElemList[index] = this->fElemList[index+1];
00094 
00095     // Keep unused elements zero for sanity's sake
00096     this->fElemList[this->fCurCount-1] = 0;
00097 
00098     // And bump down count
00099     this->fCurCount--;
00100 }
00101 
00102 template <class TElem> void RefArrayVectorOf<TElem>::removeLastElement()
00103 {
00104     if (!this->fCurCount)
00105         return;
00106     this->fCurCount--;
00107 
00108     if (this->fAdoptedElems)
00109         this->fMemoryManager->deallocate(this->fElemList[this->fCurCount]);
00110 }
00111 
00112 template <class TElem> void RefArrayVectorOf<TElem>::cleanup()
00113 {
00114     if (this->fAdoptedElems)
00115     {
00116         for (XMLSize_t index = 0; index < this->fCurCount; index++)
00117             this->fMemoryManager->deallocate(this->fElemList[index]);
00118     }
00119     this->fMemoryManager->deallocate(this->fElemList);//delete [] fElemList;
00120 }
00121 
00122 XERCES_CPP_NAMESPACE_END