SALOME - SMESH
|
00001 // File: NCollection_Array1.hxx 00002 // Created: 15.04.02 17:05:16 00003 // Author: Alexander Kartomin (akm) 00004 // <a-kartomin@opencascade.com> 00005 // Copyright: Open Cascade 2002 00006 00007 #ifndef SMESH_Array1_HeaderFile 00008 #define SMESH_Array1_HeaderFile 00009 00010 #ifndef No_Exception 00011 #include <Standard_DimensionMismatch.hxx> 00012 #include <Standard_OutOfMemory.hxx> 00013 #include <Standard_OutOfRange.hxx> 00014 #endif 00015 00016 #include <NCollection_BaseCollection.hxx> 00017 00018 #ifdef WNT 00019 // Disable the warning "operator new unmatched by delete" 00020 #pragma warning (push) 00021 #pragma warning (disable:4291) 00022 #endif 00023 00024 // *********************************************** Template for Array1 class 00025 00054 template <class TheItemType> class SMESH_Array1 00055 : public NCollection_BaseCollection<TheItemType> 00056 { 00057 00058 public: 00060 class Iterator : public NCollection_BaseCollection<TheItemType>::Iterator 00061 { 00062 public: 00064 Iterator (void) : 00065 myCurrent (0), 00066 myArray (NULL) {} 00068 Iterator (const SMESH_Array1& theArray) : 00069 myCurrent (theArray.Lower()), 00070 myArray ((SMESH_Array1<TheItemType> *) &theArray) {} 00072 void Init (const SMESH_Array1& theArray) 00073 { 00074 myCurrent = theArray.Lower(); 00075 myArray = (SMESH_Array1 *) &theArray; 00076 } 00078 virtual Standard_Boolean More (void) const 00079 { return (myCurrent<=myArray->Upper()); } 00081 virtual void Next (void) 00082 { myCurrent++; } 00084 virtual const TheItemType& Value (void) const 00085 { return myArray->Value(myCurrent); } 00087 virtual TheItemType& ChangeValue (void) const 00088 { return myArray->ChangeValue(myCurrent); } 00090 void* operator new(size_t theSize, 00091 const Handle(NCollection_BaseAllocator)& theAllocator) 00092 { return theAllocator->Allocate(theSize); } 00093 private: 00094 Standard_Integer myCurrent; 00095 SMESH_Array1* myArray; 00096 }; // End of the nested class Iterator 00097 00098 public: 00099 // ---------- PUBLIC METHODS ------------ 00100 00102 SMESH_Array1(const Standard_Integer theLower, 00103 const Standard_Integer theUpper) : 00104 NCollection_BaseCollection<TheItemType> (), 00105 myLowerBound (theLower), 00106 myUpperBound (theUpper), 00107 myDeletable (Standard_True) 00108 { 00109 #if !defined No_Exception && !defined No_Standard_RangeError 00110 if (theUpper < theLower) 00111 Standard_RangeError::Raise ("SMESH_Array1::Create"); 00112 #endif 00113 TheItemType* pBegin = new TheItemType[Length()]; 00114 #if !defined No_Exception && !defined No_Standard_OutOfMemory 00115 if (!pBegin) 00116 Standard_OutOfMemory::Raise ("SMESH_Array1 : Allocation failed"); 00117 #endif 00118 00119 myData = pBegin - theLower; 00120 } 00121 00123 SMESH_Array1 (const SMESH_Array1& theOther) : 00124 NCollection_BaseCollection<TheItemType> (), 00125 myLowerBound (theOther.Lower()), 00126 myUpperBound (theOther.Upper()), 00127 myDeletable (Standard_True) 00128 { 00129 TheItemType* pBegin = new TheItemType[Length()]; 00130 #if !defined No_Exception && !defined No_Standard_OutOfMemory 00131 if (!pBegin) 00132 Standard_OutOfMemory::Raise ("SMESH_Array1 : Allocation failed"); 00133 #endif 00134 myData = pBegin - myLowerBound; 00135 00136 *this = theOther; 00137 } 00138 00140 SMESH_Array1 (const TheItemType& theBegin, 00141 const Standard_Integer theLower, 00142 const Standard_Integer theUpper) : 00143 NCollection_BaseCollection<TheItemType> (), 00144 myLowerBound (theLower), 00145 myUpperBound (theUpper), 00146 myDeletable (Standard_False) 00147 { 00148 #if !defined No_Exception && !defined No_Standard_RangeError 00149 if (theUpper < theLower) 00150 Standard_RangeError::Raise ("SMESH_Array1::Array1"); 00151 #endif 00152 myData = (TheItemType *) &theBegin - theLower; 00153 } 00154 00156 void Init (const TheItemType& theValue) 00157 { 00158 TheItemType *pCur = &myData[myLowerBound], *pEnd=&myData[myUpperBound]; 00159 for(; pCur <= pEnd; pCur++) 00160 *pCur = (TheItemType&) theValue; 00161 } 00162 00164 virtual Standard_Integer Size (void) const 00165 { return Length(); } 00167 Standard_Integer Length (void) const 00168 { return (myUpperBound-myLowerBound+1); } 00169 00171 Standard_Integer Lower (void) const 00172 { return myLowerBound; } 00174 Standard_Integer Upper (void) const 00175 { return myUpperBound; } 00176 00178 Standard_Boolean IsDeletable (void) const 00179 { return myDeletable; } 00180 00182 Standard_Boolean IsAllocated (void) const 00183 { return myDeletable; } 00184 00186 // Copies items from the other collection into the allocated 00187 // storage. Raises an exception when sizes differ. 00188 virtual void Assign (const NCollection_BaseCollection<TheItemType>& theOther) 00189 { 00190 if (&theOther == this) 00191 return; 00192 #if !defined No_Exception && !defined No_Standard_DimensionMismatch 00193 if (Length() != theOther.Size()) 00194 Standard_DimensionMismatch::Raise ("SMESH_Array1::Assign"); 00195 #endif 00196 TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& anIter2 = 00197 theOther.CreateIterator(); 00198 TheItemType * const pEndItem = &myData[myUpperBound]; 00199 for (TheItemType * pItem = &myData[myLowerBound]; 00200 pItem <= pEndItem; anIter2.Next()) 00201 * pItem ++ = anIter2.Value(); 00202 } 00203 00205 SMESH_Array1& operator= (const SMESH_Array1& theOther) 00206 { 00207 if (&theOther == this) 00208 return *this; 00209 #if !defined No_Exception && !defined No_Standard_DimensionMismatch 00210 if (Length() != theOther.Length()) 00211 Standard_DimensionMismatch::Raise ("SMESH_Array1::operator="); 00212 #endif 00213 TheItemType * pMyItem = &myData[myLowerBound]; 00214 TheItemType * const pEndItem = &(theOther.myData)[theOther.myUpperBound]; 00215 TheItemType * pItem = &(theOther.myData)[theOther.myLowerBound]; 00216 while (pItem <= pEndItem) * pMyItem ++ = * pItem ++; 00217 return *this; 00218 } 00219 00221 const TheItemType& Value (const Standard_Integer theIndex) const 00222 { 00223 #if !defined No_Exception && !defined No_Standard_OutOfRange 00224 if (theIndex < myLowerBound || theIndex > myUpperBound) 00225 Standard_OutOfRange::Raise ("SMESH_Array1::Value"); 00226 #endif 00227 return myData[theIndex]; 00228 } 00229 00231 const TheItemType& operator() (const Standard_Integer theIndex) const 00232 { return Value (theIndex); } 00233 00235 TheItemType& ChangeValue (const Standard_Integer theIndex) 00236 { 00237 #if !defined No_Exception && !defined No_Standard_OutOfRange 00238 if (theIndex < myLowerBound || theIndex > myUpperBound) 00239 Standard_OutOfRange::Raise ("SMESH_Array1::ChangeValue"); 00240 #endif 00241 return myData[theIndex]; 00242 } 00243 00245 TheItemType& operator() (const Standard_Integer theIndex) 00246 { return ChangeValue (theIndex); } 00247 00249 void SetValue (const Standard_Integer theIndex, 00250 const TheItemType& theItem) 00251 { 00252 #if !defined No_Exception && !defined No_Standard_OutOfRange 00253 if (theIndex < myLowerBound || theIndex > myUpperBound) 00254 Standard_OutOfRange::Raise ("SMESH_Array1::SetValue"); 00255 #endif 00256 myData[theIndex] = theItem; 00257 } 00258 00260 ~SMESH_Array1 (void) 00261 { if (myDeletable) delete [] &(myData[myLowerBound]); } 00262 00263 private: 00264 // ----------- PRIVATE METHODS ----------- 00265 00266 // ******** Creates Iterator for use on BaseCollection 00267 virtual 00268 TYPENAME NCollection_BaseCollection<TheItemType>::Iterator& 00269 CreateIterator(void) const 00270 { return *(new (this->IterAllocator()) Iterator(*this)); } 00271 00272 protected: 00273 // ---------- PROTECTED FIELDS ----------- 00274 Standard_Integer myLowerBound; 00275 Standard_Integer myUpperBound; 00276 Standard_Boolean myDeletable; 00277 TheItemType* myData; 00278 }; 00279 00280 #ifdef WNT 00281 #pragma warning (pop) 00282 #endif 00283 00284 #endif