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