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