GME  13
RefArrayOf.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  * $Id: RefArrayOf.c 932887 2010-04-11 13:04:59Z borisk $
00020  */
00021 
00022 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 #if defined(XERCES_TMPLSINC)
00027 #include <xercesc/util/RefArrayOf.hpp>
00028 #endif
00029 
00030 XERCES_CPP_NAMESPACE_BEGIN
00031 
00032 // ---------------------------------------------------------------------------
00033 //  RefArrayOf: Constructors and Destructor
00034 // ---------------------------------------------------------------------------
00035 template <class TElem>
00036 RefArrayOf<TElem>::RefArrayOf(const XMLSize_t size,
00037                               MemoryManager* const manager) :
00038 
00039     fSize(size)
00040     , fArray(0)
00041     , fMemoryManager(manager)
00042 {
00043     fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
00044     for (XMLSize_t index = 0; index < fSize; index++)
00045         fArray[index] = 0;
00046 }
00047 
00048 template <class TElem>
00049 RefArrayOf<TElem>::RefArrayOf(TElem* values[],
00050                               const XMLSize_t size,
00051                               MemoryManager* const manager) :
00052 
00053     fSize(size)
00054     , fArray(0)
00055     , fMemoryManager(manager)
00056 {
00057     fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
00058     for (XMLSize_t index = 0; index < fSize; index++)
00059         fArray[index] = values[index];
00060 }
00061 
00062 template <class TElem> RefArrayOf<TElem>::
00063 RefArrayOf(const RefArrayOf<TElem>& source) :
00064 
00065     fSize(source.fSize)
00066     , fArray(0)
00067     , fMemoryManager(source.fMemoryManager)
00068 {
00069     fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
00070     for (XMLSize_t index = 0; index < fSize; index++)
00071         fArray[index] = source.fArray[index];
00072 }
00073 
00074 template <class TElem> RefArrayOf<TElem>::~RefArrayOf()
00075 {
00076     fMemoryManager->deallocate(fArray);//delete [] fArray;
00077 }
00078 
00079 
00080 // ---------------------------------------------------------------------------
00081 //  RefArrayOf: Public operators
00082 // ---------------------------------------------------------------------------
00083 template <class TElem> TElem*& RefArrayOf<TElem>::
00084 operator[](const XMLSize_t index)
00085 {
00086     if (index >= fSize)
00087         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
00088     return fArray[index];
00089 }
00090 
00091 template <class TElem> const TElem* RefArrayOf<TElem>::
00092 operator[](const XMLSize_t index) const
00093 {
00094     if (index >= fSize)
00095         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
00096     return fArray[index];
00097 }
00098 
00099 template <class TElem> RefArrayOf<TElem>& RefArrayOf<TElem>::
00100 operator=(const RefArrayOf<TElem>& toAssign)
00101 {
00102     if (this == &toAssign)
00103         return *this;
00104 
00105     // Reallocate if not the same size
00106     if (toAssign.fSize != fSize)
00107     {
00108         fMemoryManager->deallocate(fArray);//delete [] fArray;
00109         fSize = toAssign.fSize;
00110         fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
00111     }
00112 
00113     // Copy over the source elements
00114     for (XMLSize_t index = 0; index < fSize; index++)
00115         fArray[index] = toAssign.fArray[index];
00116 
00117     return *this;
00118 }
00119 
00120 template <class TElem> bool RefArrayOf<TElem>::
00121 operator==(const RefArrayOf<TElem>& toCompare) const
00122 {
00123     if (this == &toCompare)
00124         return true;
00125 
00126     if (fSize != toCompare.fSize)
00127         return false;
00128 
00129     for (XMLSize_t index = 0; index < fSize; index++)
00130     {
00131         if (fArray[index] != toCompare.fArray[index])
00132             return false;
00133     }
00134     return true;
00135 }
00136 
00137 template <class TElem> bool RefArrayOf<TElem>::
00138 operator!=(const RefArrayOf<TElem>& toCompare) const
00139 {
00140     return !operator==(toCompare);
00141 }
00142 
00143 
00144 // ---------------------------------------------------------------------------
00145 //  RefArrayOf: Copy operations
00146 // ---------------------------------------------------------------------------
00147 template <class TElem> XMLSize_t RefArrayOf<TElem>::
00148 copyFrom(const RefArrayOf<TElem>& srcArray)
00149 {
00150     //
00151     //  Copy over as many of the source elements as will fit into
00152     //  this array.
00153     //
00154     const XMLSize_t count = fSize < srcArray.fSize ?
00155                                     fSize : srcArray.fSize;
00156 
00157     for (XMLSize_t index = 0; index < fSize; index++)
00158         fArray[index] = srcArray.fArray[index];
00159 
00160     return count;
00161 }
00162 
00163 
00164 // ---------------------------------------------------------------------------
00165 //  RefArrayOf: Getter methods
00166 // ---------------------------------------------------------------------------
00167 template <class TElem> XMLSize_t RefArrayOf<TElem>::length() const
00168 {
00169     return fSize;
00170 }
00171 
00172 template <class TElem> TElem** RefArrayOf<TElem>::rawData() const
00173 {
00174     return fArray;
00175 }
00176 
00177 
00178 // ---------------------------------------------------------------------------
00179 //  RefArrayOf: Element management methods
00180 // ---------------------------------------------------------------------------
00181 template <class TElem> void RefArrayOf<TElem>::deleteAt(const XMLSize_t index)
00182 {
00183     if (index >= fSize)
00184         ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
00185 
00186     delete fArray[index];
00187     fArray[index] = 0;
00188 }
00189 
00190 template <class TElem> void RefArrayOf<TElem>::deleteAllElements()
00191 {
00192     for (XMLSize_t index = 0; index < fSize; index++)
00193     {
00194         delete fArray[index];
00195         fArray[index] = 0;
00196     }
00197 }
00198 
00199 template <class TElem> void RefArrayOf<TElem>::resize(const XMLSize_t newSize)
00200 {
00201     if (newSize == fSize)
00202         return;
00203 
00204     if (newSize < fSize)
00205         ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Array_BadNewSize, fMemoryManager);
00206 
00207     // Allocate the new array
00208     TElem** newArray = (TElem**) fMemoryManager->allocate
00209     (
00210         newSize * sizeof(TElem*)
00211     );//new TElem*[newSize];
00212 
00213     // Copy the existing values
00214     XMLSize_t index = 0;
00215     for (; index < fSize; index++)
00216         newArray[index] = fArray[index];
00217 
00218     for (; index < newSize; index++)
00219         newArray[index] = 0;
00220 
00221     // Delete the old array and update our members
00222     fMemoryManager->deallocate(fArray);//delete [] fArray;
00223     fArray = newArray;
00224     fSize = newSize;
00225 }
00226 
00227 
00228 
00229 
00230 // ---------------------------------------------------------------------------
00231 //  RefArrayEnumerator: Constructors and Destructor
00232 // ---------------------------------------------------------------------------
00233 template <class TElem> RefArrayEnumerator<TElem>::
00234 RefArrayEnumerator(         RefArrayOf<TElem>* const    toEnum
00235                     , const bool                        adopt) :
00236     fAdopted(adopt)
00237     , fCurIndex(0)
00238     , fToEnum(toEnum)
00239 {
00240 }
00241 
00242 template <class TElem> RefArrayEnumerator<TElem>::~RefArrayEnumerator()
00243 {
00244     if (fAdopted)
00245         delete fToEnum;
00246 }
00247 
00248 
00249 // ---------------------------------------------------------------------------
00250 //  RefArrayEnumerator: Enum interface
00251 // ---------------------------------------------------------------------------
00252 template <class TElem> bool RefArrayEnumerator<TElem>::hasMoreElements() const
00253 {
00254     if (fCurIndex >= fToEnum->length())
00255         return false;
00256     return true;
00257 }
00258 
00259 template <class TElem> TElem& RefArrayEnumerator<TElem>::nextElement()
00260 {
00261     return *(*fToEnum)[fCurIndex++];
00262 }
00263 
00264 template <class TElem> void RefArrayEnumerator<TElem>::Reset()
00265 {
00266     fCurIndex = 0;
00267 }
00268 
00269 XERCES_CPP_NAMESPACE_END