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