SALOME - SMESH
SMESH_Array1.hxx
Go to the documentation of this file.
1 // File: NCollection_Array1.hxx
2 // Created: 15.04.02 17:05:16
3 // Author: Alexander Kartomin (akm)
4 // <a-kartomin@opencascade.com>
5 // Copyright: Open Cascade 2002
6 
7 #ifndef SMESH_Array1_HeaderFile
8 #define SMESH_Array1_HeaderFile
9 
10 #ifndef No_Exception
11 #include <Standard_DimensionMismatch.hxx>
12 #include <Standard_OutOfMemory.hxx>
13 #include <Standard_OutOfRange.hxx>
14 #endif
15 
16 #include <NCollection_Array1.hxx>
17 
18 #ifdef WNT
19 // Disable the warning "operator new unmatched by delete"
20 #pragma warning (push)
21 #pragma warning (disable:4291)
22 #endif
23 
24 // *********************************************** Template for Array1 class
25 
54 template <class TheItemType> class SMESH_Array1
55  : public NCollection_Array1<TheItemType>
56 {
57 
58  public:
60  class Iterator : public NCollection_Array1<TheItemType>::Iterator
61  {
62  public:
64  Iterator (void) :
65  myCurrent (0),
66  myArray (NULL) {}
68  Iterator (const SMESH_Array1& theArray) :
69  myCurrent (theArray.Lower()),
70  myArray ((SMESH_Array1<TheItemType> *) &theArray) {}
72  void Init (const SMESH_Array1& theArray)
73  {
74  myCurrent = theArray.Lower();
75  myArray = (SMESH_Array1 *) &theArray;
76  }
78  virtual Standard_Boolean More (void) const
79  { return (myCurrent<=myArray->Upper()); }
81  virtual void Next (void)
82  { myCurrent++; }
84  virtual const TheItemType& Value (void) const
85  { return myArray->Value(myCurrent); }
87  virtual TheItemType& ChangeValue (void) const
88  { return myArray->ChangeValue(myCurrent); }
90  void* operator new(size_t theSize,
91  const Handle(NCollection_BaseAllocator)& theAllocator)
92  { return theAllocator->Allocate(theSize); }
93  private:
94  Standard_Integer myCurrent;
96  }; // End of the nested class Iterator
97 
98  public:
99  // ---------- PUBLIC METHODS ------------
100 
102  SMESH_Array1(const Standard_Integer theLower,
103  const Standard_Integer theUpper) :
104  NCollection_Array1<TheItemType> (),
105  myLowerBound (theLower),
106  myUpperBound (theUpper),
107  myDeletable (Standard_True)
108  {
109 #if !defined No_Exception && !defined No_Standard_RangeError
110  if (theUpper < theLower)
111  Standard_RangeError::Raise ("SMESH_Array1::Create");
112 #endif
113  TheItemType* pBegin = new TheItemType[Length()];
114 #if !defined No_Exception && !defined No_Standard_OutOfMemory
115  if (!pBegin)
116  Standard_OutOfMemory::Raise ("SMESH_Array1 : Allocation failed");
117 #endif
118 
119  myData = pBegin - theLower;
120  }
121 
123  SMESH_Array1 (const SMESH_Array1& theOther) :
124  NCollection_Array1<TheItemType> (),
125  myLowerBound (theOther.Lower()),
126  myUpperBound (theOther.Upper()),
127  myDeletable (Standard_True)
128  {
129  TheItemType* pBegin = new TheItemType[Length()];
130 #if !defined No_Exception && !defined No_Standard_OutOfMemory
131  if (!pBegin)
132  Standard_OutOfMemory::Raise ("SMESH_Array1 : Allocation failed");
133 #endif
134  myData = pBegin - myLowerBound;
135 
136  *this = theOther;
137  }
138 
140  SMESH_Array1 (const TheItemType& theBegin,
141  const Standard_Integer theLower,
142  const Standard_Integer theUpper) :
143  NCollection_Array1<TheItemType> (),
144  myLowerBound (theLower),
145  myUpperBound (theUpper),
146  myDeletable (Standard_False)
147  {
148 #if !defined No_Exception && !defined No_Standard_RangeError
149  if (theUpper < theLower)
150  Standard_RangeError::Raise ("SMESH_Array1::Array1");
151 #endif
152  myData = (TheItemType *) &theBegin - theLower;
153  }
154 
156  void Init (const TheItemType& theValue)
157  {
158  TheItemType *pCur = &myData[myLowerBound], *pEnd=&myData[myUpperBound];
159  for(; pCur <= pEnd; pCur++)
160  *pCur = (TheItemType&) theValue;
161  }
162 
164  virtual Standard_Integer Size (void) const
165  { return Length(); }
167  Standard_Integer Length (void) const
168  { return (myUpperBound-myLowerBound+1); }
169 
171  Standard_Integer Lower (void) const
172  { return myLowerBound; }
174  Standard_Integer Upper (void) const
175  { return myUpperBound; }
176 
178  Standard_Boolean IsDeletable (void) const
179  { return myDeletable; }
180 
182  Standard_Boolean IsAllocated (void) const
183  { return myDeletable; }
184 
186  // Copies items from the other collection into the allocated
187  // storage. Raises an exception when sizes differ.
188  virtual void Assign (const NCollection_Array1<TheItemType>& theOther)
189  {
190  if (&theOther == this)
191  return;
192 #if !defined No_Exception && !defined No_Standard_DimensionMismatch
193  if (Length() != theOther.Size())
194  Standard_DimensionMismatch::Raise ("SMESH_Array1::Assign");
195 #endif
196  TYPENAME NCollection_Array1<TheItemType>::Iterator& anIter2 =
197  theOther.CreateIterator();
198  TheItemType * const pEndItem = &myData[myUpperBound];
199  for (TheItemType * pItem = &myData[myLowerBound];
200  pItem <= pEndItem; anIter2.Next())
201  * pItem ++ = anIter2.Value();
202  }
203 
206  {
207  if (&theOther == this)
208  return *this;
209 #if !defined No_Exception && !defined No_Standard_DimensionMismatch
210  if (Length() != theOther.Length())
211  Standard_DimensionMismatch::Raise ("SMESH_Array1::operator=");
212 #endif
213  TheItemType * pMyItem = &myData[myLowerBound];
214  TheItemType * const pEndItem = &(theOther.myData)[theOther.myUpperBound];
215  TheItemType * pItem = &(theOther.myData)[theOther.myLowerBound];
216  while (pItem <= pEndItem) * pMyItem ++ = * pItem ++;
217  return *this;
218  }
219 
221  const TheItemType& Value (const Standard_Integer theIndex) const
222  {
223 #if !defined No_Exception && !defined No_Standard_OutOfRange
224  if (theIndex < myLowerBound || theIndex > myUpperBound)
225  Standard_OutOfRange::Raise ("SMESH_Array1::Value");
226 #endif
227  return myData[theIndex];
228  }
229 
231  const TheItemType& operator() (const Standard_Integer theIndex) const
232  { return Value (theIndex); }
233 
235  TheItemType& ChangeValue (const Standard_Integer theIndex)
236  {
237 #if !defined No_Exception && !defined No_Standard_OutOfRange
238  if (theIndex < myLowerBound || theIndex > myUpperBound)
239  Standard_OutOfRange::Raise ("SMESH_Array1::ChangeValue");
240 #endif
241  return myData[theIndex];
242  }
243 
245  TheItemType& operator() (const Standard_Integer theIndex)
246  { return ChangeValue (theIndex); }
247 
249  void SetValue (const Standard_Integer theIndex,
250  const TheItemType& theItem)
251  {
252 #if !defined No_Exception && !defined No_Standard_OutOfRange
253  if (theIndex < myLowerBound || theIndex > myUpperBound)
254  Standard_OutOfRange::Raise ("SMESH_Array1::SetValue");
255 #endif
256  myData[theIndex] = theItem;
257  }
258 
261  { if (myDeletable) delete [] &(myData[myLowerBound]); }
262 
263  private:
264  // ----------- PRIVATE METHODS -----------
265 
266  // ******** Creates Iterator for use on BaseCollection
267  virtual
269  CreateIterator(void) const
270  { return *(new (this->IterAllocator()) Iterator(*this)); }
271 
272  protected:
273  // ---------- PROTECTED FIELDS -----------
274  Standard_Integer myLowerBound;
275  Standard_Integer myUpperBound;
276  Standard_Boolean myDeletable;
277  TheItemType* myData;
278 };
279 
280 #ifdef WNT
281 #pragma warning (pop)
282 #endif
283 
284 #endif
Standard_Integer myUpperBound
void Init(const TheItemType &theValue)
Initialise the items with theValue.
Standard_Boolean myDeletable
Flag showing who allocated the array.
Iterator(void)
Empty constructor - for later Init.
virtual TYPENAME NCollection_Array1< TheItemType >::Iterator & CreateIterator(void) const
Iterator(const SMESH_Array1 &theArray)
Constructor with initialisation.
Standard_Boolean IsDeletable(void) const
myDeletable flag
virtual void Assign(const NCollection_Array1< TheItemType > &theOther)
Assign (any collection to this array)
Standard_Boolean IsAllocated(void) const
IsAllocated flag - for naming compatibility.
virtual TheItemType & ChangeValue(void) const
Variable value access.
Purpose: The class Array1 represents unidimensional arrays of fixed size known at run time...
SMESH_Array1(const TheItemType &theBegin, const Standard_Integer theLower, const Standard_Integer theUpper)
C array-based constructor.
virtual void Next(void)
Make step.
Implementation of the Iterator interface.
virtual const TheItemType & Value(void) const
Constant value access.
void Init(const SMESH_Array1 &theArray)
Initialisation.
~SMESH_Array1(void)
Destructor - releases the memory.
const TheItemType & operator()(const Standard_Integer theIndex) const
operator() - alias to Value
const TheItemType & Value(const Standard_Integer theIndex) const
Constant value access.
Standard_Integer myCurrent
Index of the current item.
virtual Standard_Integer Size(void) const
Size query.
SMESH_Array1 * myArray
Pointer to the array being iterated.
Standard_Integer myLowerBound
void SetValue(const Standard_Integer theIndex, const TheItemType &theItem)
Set value.
SMESH_Array1(const SMESH_Array1 &theOther)
Copy constructor.
SMESH_Array1(const Standard_Integer theLower, const Standard_Integer theUpper)
Constructor.
Standard_Integer Length(void) const
Length query (the same)
Standard_Integer Lower(void) const
Lower bound.
Standard_Integer Upper(void) const
Upper bound.
virtual Standard_Boolean More(void) const
Check end.
SMESH_Array1 & operator=(const SMESH_Array1 &theOther)
operator= (array to array)
TheItemType & ChangeValue(const Standard_Integer theIndex)
Variable value access.
TheItemType * myData
Pointer to &#39;0&#39;th array item.