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