]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_PointerOf.hxx
Salome HOME
correct small problem from the version in the MedFileV2_2 branch.
[modules/med.git] / src / MEDMEM / MEDMEM_PointerOf.hxx
1 # if ! defined( __PointerOf_HXX__ )
2 # define __PointerOf_HXX__
3
4 #include <cstdlib>
5 #include "utilities.h"
6 #include "MEDMEM_Exception.hxx"
7
8 /*!
9   The template class PointerOf embedding a standard pointer (_pointer) is in
10   charge of managing the pointed memory.\n
11
12   the object PointerOf is the memory owner if a size is given at object
13   construction. In these cases, memory will be desallocated at object
14   destruction. In all other cases, the desallocator will only nullify pointers.
15 */
16 namespace MEDMEM {
17
18 template <typename T> class PointerOf
19 {
20 protected :
21   /*!
22     pointer to the pointed memory
23   */
24
25   T* _pointer ;
26
27   /*!
28     boolean setted to true if memory has to be desallocated
29   */
30
31   bool _done ;
32
33 public :
34   PointerOf() ;
35   ~PointerOf() ;
36   PointerOf( const int &size ) ;
37   PointerOf( const T *pointer ) ;
38   PointerOf( const int &size, const T *pointer ) ;
39   PointerOf( const PointerOf<T> & pointerOf ) ;
40   PointerOf( const int &size, const PointerOf<T> & pointerOf ) ;
41   operator T*() ;
42   operator const T*() const ;
43   void set( const int &size ) ;
44   void set( const T *pointer ) ;
45   void set( const int &size, const T *pointer ) ;
46   PointerOf<T>& operator=( const PointerOf<T> &pointer ) ;
47 } ;
48 } ;
49
50 /*
51   Implementation
52 */
53
54 using namespace MEDMEM;
55
56 /*!
57   Creates a null T* pointer and sets the boolean (for desallocation) to false.
58 */
59
60 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
61 {
62   BEGIN_OF("PointerOf<T>::PointerOf() empty constructor");
63   END_OF("PointerOf<T>::PointerOf() empty constructor");
64 }
65
66 /* !
67    Creates a standard T* pointer to the pointed memory. \n
68    The boolean for desallocation is setted to false. \n
69    Be aware : \n
70    - The "old" PointerOf always has propriety of the pointed memory. \n
71    - If the "old" PointerOf is detroyed, the "new" PointerOf points
72    a desallocated memory zone.
73 */
74
75 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) :
76   _pointer((T*)(const T* const)pointerOf), _done(false)
77 {
78   BEGIN_OF("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
79   MESSAGE("Warning ! No Propriety Transfer");
80   END_OF  ("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
81 }
82
83 /*! 
84   Duplicate array of size size pointed in pointerOf.
85 */
86
87 template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) : 
88   _pointer((size,(T*)pointerOf))
89 {
90 }
91
92 /*!
93   If size <= 0, creates a null "T*" pointer\n
94   Else allocates memory and sets desallocation boolean to true./n
95   Memory will be desallocated  when erasing this PointerOf
96 */
97
98 template <typename T> PointerOf<T>::PointerOf( const int &size )
99 {
100   if (size <= 0)
101     {
102       _pointer=(T*)NULL;
103       _done=false;
104     }
105   else
106     {
107       _pointer = new T[ size ] ;
108       _done=true;
109     }
110 }
111
112 /*! Creates a standard pointer to the memory zone pointed by T*. /n
113   T* owner is in charged of memory desallocation. /n
114   Memory will not be released when erasing this PointerOf
115 */
116
117 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
118 {
119 }
120
121 /*!
122   If size <= 0, return an exception\n
123   Else duplicate array and sets desallocation boolean to true./n
124   Memory will be desallocated  when erasing this PointerOf
125 */
126
127 template <typename T> PointerOf<T>::PointerOf( const int &size, const T* pointer)
128 {
129   if (size <= 0)
130     throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
131   
132   _pointer = new T[ size ] ;
133   memcpy(_pointer,pointer,size*sizeof(T));
134   _done=true;
135 }
136
137 /*!
138   The destuctor desallocates memory if necessary (that is if the attribute
139   _done equals true).\n
140   The attribute _pointer is nullified
141 */
142
143 template <typename T> PointerOf<T>::~PointerOf()
144 {
145   if ( _pointer )
146     {
147       if( _done )
148         {
149           MESSAGE("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
150           delete [] _pointer ;
151           _done = false ;
152         }
153       else
154         {
155           MESSAGE("_pointer is only nullified") ;
156         }
157       _pointer = 0 ;
158     }
159 }
160
161 /*!
162   Creates a standard pointer (T*) to the pointed memory. \n
163   The boolean for desallocation is setted to false. \n
164   Be aware : \n
165   - The "right" PointerOf always has propriety of the pointed memory. \n
166   - If the "right" PointerOf is detroyed, the "left" PointerOf points
167   a desallocated memory zone.
168   - it works the same way as PointerOf(const PointerOf<T> & pointerOf)
169 */
170
171 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
172 {
173   BEGIN_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
174   if ( &pointer != this )
175     {
176       this->set( pointer._pointer ) ;
177     }
178   END_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
179   return *this ;
180 }
181
182 /*!
183   Returns _pointer.
184 */
185
186 template <typename T> PointerOf<T>::operator T*()
187 {
188   return _pointer ;
189 }
190
191 /*!
192   Returns _pointer.
193 */
194
195 template <typename T> PointerOf<T>::operator const T*() const
196 {
197   return _pointer ;
198 }
199
200 /*!
201   If necessary, released memory holded by PointerOf/n.
202   Else allocates memory and sets desallocation boolean to true./n
203   Can be used in order to "nullify" an existing PointerOf/n
204   Memory will be desallocated  when erasing this PointerOf
205 */
206
207 template <typename T> void PointerOf<T>::set( const int &size )
208 {
209   if ( _pointer && _done )
210     {
211       delete [] _pointer ;
212       _pointer=0 ;
213     }
214   if (size <= 0)
215     {
216       _pointer=(T*)NULL;
217     }
218   else
219     {
220       _pointer = new T[ size ] ;
221     }
222   _done = true ;
223 }
224
225 /*! If necessary, released memory holded by PointerOf/n.
226     Then, sets _pointer to the memory zone pointed by T*. /n
227     T* owner is in charged of memory desallocation. /n
228     memory will not be released when erasing this PointerOf*/
229
230 template <typename T> void PointerOf<T>::set( const T *pointer )
231 {
232   MESSAGE( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
233   SCRUTE(pointer) ;
234   SCRUTE(_done) ;
235   if ( _pointer && _done )
236     {
237       MESSAGE("PointerOf<T>::set --> deleting _pointer") ;
238       delete [] _pointer ;
239       _pointer=0 ;
240       _done=false ;
241     }
242   _pointer=(T*)pointer ;
243   _done=false ;
244   MESSAGE( "END PointerOf<T>::set( const T *pointer )" ) ;
245 }
246
247 /*!
248   If necessary, released memory holded by PointerOf/n.
249   If size <= 0, return an exception\n.
250   Else allocates memory and sets desallocation boolean to true./n
251   Can be used in order to "nullify" an existing PointerOf/n
252   Memory will be desallocated  when erasing this PointerOf
253 */
254
255 template <typename T> void PointerOf<T>::set( const int &size, const T *pointer)
256 {
257   if ( _pointer && _done )
258     {
259       delete [] _pointer ;
260       _pointer = NULL ;
261     }
262   if (size <= 0)
263     throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
264
265   _pointer = new T[ size ] ;
266   memcpy(_pointer,pointer,size*sizeof(T));
267   _done=true;
268 }
269
270 # endif         /* # if ! defined( __PointerOf_HXX__ ) */