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