]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_PointerOf.hxx
Salome HOME
Fix problem of make distcheck
[modules/med.git] / src / MEDMEM / MEDMEM_PointerOf.hxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 # if ! defined( __PointerOf_HXX__ )
24 # define __PointerOf_HXX__
25
26 #include <cstdlib>
27 #include <cstring>
28 #include "MEDMEM_Utilities.hxx"
29 #include "MEDMEM_Exception.hxx"
30
31 /*!
32         The template class PointerOf embedding a standard pointer (_pointer) is in charge of
33         managing the pointed memory.\n
34         
35         the object PointerOf is the memory owner if a size is given at object construction.
36         In these cases, memory will be desallocated at object destruction. In all other cases,
37         the desallocator will only nullify pointers.
38 */
39 namespace MEDMEM {
40
41 template <typename T> class PointerOf
42 {
43 protected :
44                         /*! pointer to the pointed memory */
45         T* _pointer ;
46                         /*! boolean setted to true if memory has to be desallocated */
47         bool _done ;
48
49 public :
50         PointerOf() ;
51         ~PointerOf() ;
52         PointerOf( const int &size ) ;
53         PointerOf( const T *pointer ) ;
54         PointerOf( const int &size, const T *pointer ) ;
55         PointerOf( const PointerOf<T> & pointerOf ) ;
56   ///PointerOf( const int &size, const PointerOf<T> & pointerOf ) ;
57         operator T*() ;
58         operator const T*() const ;
59         void set( const int &size ) ;
60         void set( const T *pointer ) ;
61         void set( const int &size, const T *pointer ) ;
62         void setShallowAndOwnership( const T *pointer );
63         PointerOf<T>& operator=( const PointerOf<T> &pointer ) ;
64 } ;
65
66 // ------------------------------------------------------------ //
67 //                                                              //
68 //                      Implementation                          //
69 //                                                              //
70 // ------------------------------------------------------------ //
71
72 /*! Creates a null T* pointer and sets the boolean (for desallocation) to false. */
73 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
74 {
75 }
76
77 /*! Creates a standard T* pointer to the pointed memory. \n
78     The boolean for desallocation is setted to false. \n
79     Be aware : \n
80     - The "old" PointerOf always has propriety of the pointed memory. \n
81     - If the "old" PointerOf is detroyed, the "new" PointerOf points
82       a desallocated memory zone. */
83 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) :
84   _pointer((T*)(const T* const)pointerOf), _done(false)
85 {
86   const char* LOC = "PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)";
87   BEGIN_OF_MED(LOC);
88         MESSAGE_MED("Warning ! No Propriety Transfer");
89   END_OF_MED(LOC);
90 }
91
92 /*! 
93   Duplicate array of size size pointed in pointerOf.
94 */
95 //template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) : 
96 //  _pointer((size,(T*)pointerOf))
97 //{
98 //}
99
100 /*! If size < 0, creates a null "T*" pointer\n
101     Else allocates memory 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 )
104 {
105         if (size < 0)
106         {
107                 _pointer=(T*)NULL;
108                 _done=false;
109         }
110         else
111         {
112                 _pointer = new T[ size ] ;
113                 _done=true;
114         }
115 }
116
117 /*! Creates a standard pointer to the memory zone pointed by T*. \n
118    T* owner is in charged of memory desallocation. \n
119    Memory will not be released when erasing this PointerOf*/
120 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
121 {
122 }
123
124 /*! If size < 0, return an exception\n
125     Else duplicate array and sets desallocation boolean to true.\n
126     Memory will be desallocated  when erasing this PointerOf*/
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 /*! The destuctor desallocates memory if necessary (that is if the attribute _done equals true).\n
138     The attribute _pointer is nullified */
139 template <typename T> PointerOf<T>::~PointerOf()
140 {
141         if ( _pointer )
142         {
143                 if( _done )
144                 {
145                         MESSAGE_MED("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
146                         delete [] _pointer ;
147                         _done = false ;
148                 }
149                 else
150                 {
151                         MESSAGE_MED("_pointer is only nullified") ;
152                 }
153                 _pointer = 0 ;
154         }
155 }
156
157 /*! Creates a standard pointer (T*) to the pointed memory. \n
158     The boolean for desallocation is setted to false. \n
159     Be aware : \n
160     - The "right" PointerOf always has propriety of the pointed memory. \n
161     - If the "right" PointerOf is detroyed, the "left" PointerOf points
162       a desallocated memory zone.
163     - it works the same way as PointerOf(const PointerOf<T> & pointerOf) */
164 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
165 {
166   const char* LOC = "PointerOf<T>::operator=( const PointerOf<T> &pointer )";
167   BEGIN_OF_MED(LOC);
168         if ( &pointer != this )
169         {
170                 this->set( pointer._pointer ) ;
171         }
172   END_OF_MED(LOC);
173         return *this ;
174 }
175
176 /*! Returns _pointer.*/
177 template <typename T> PointerOf<T>::operator T*()
178 {
179         return _pointer ;
180 }
181
182
183 /*! Returns _pointer.*/
184 template <typename T> PointerOf<T>::operator const T*() const
185 {
186         return _pointer ;
187 }
188
189 /*! If necessary, released memory holded by PointerOf\n.
190     Else allocates memory and sets desallocation boolean to true.\n
191     Can be used in order to "nullify" an existing PointerOf\n
192     Memory will be desallocated  when erasing this PointerOf*/
193 template <typename T> void PointerOf<T>::set( const int &size )
194 {
195         if ( _pointer && _done )
196         {
197                 delete [] _pointer ;
198                 _pointer=0 ;
199         }
200         // if (size < 0) TODO: analyse why it does not work
201         if (size <= 0)
202         {
203                 _pointer=(T*)NULL;
204         }
205         else
206         {
207                 _pointer = new T[ size ] ;
208         }
209         _done = true ;
210         return ;
211 }
212
213 /*! If necessary, released memory holded by PointerOf\n.
214     Then, sets _pointer to the memory zone pointed by T*. \n
215     T* owner is in charged of memory desallocation. \n
216     memory will not be released when erasing this PointerOf*/
217 template <typename T> void PointerOf<T>::set( const T *pointer )
218 {
219         MESSAGE_MED( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
220         SCRUTE_MED(pointer) ;
221         SCRUTE_MED(_done) ;
222         if ( _pointer && _done )
223         {
224                 MESSAGE_MED("PointerOf<T>::set --> deleting _pointer") ;
225                 delete [] _pointer ;
226                 _pointer=0 ;
227                 _done=false ;
228         }
229         _pointer=(T*)pointer ;
230         _done=false ;
231         MESSAGE_MED( "END PointerOf<T>::set( const T *pointer )" ) ;
232         return ;
233 }
234
235 /*! If necessary, released memory holded by PointerOf\n.
236     If size < 0, return an exception\n.
237     Else allocates memory and sets desallocation boolean to true.\n
238     Can be used in order to "nullify" an existing PointerOf\n
239     Memory will be desallocated  when erasing this PointerOf*/
240 template <typename T> void PointerOf<T>::set( const int &size, const T *pointer)
241 {
242   if ( _pointer && _done )
243     {
244       delete [] _pointer ;
245       _pointer = NULL ;
246     }
247   if (size < 0)
248     throw MEDEXCEPTION("PointerOf( const int,const T*) : array size < 0");
249
250   _pointer = new T[ size ] ;
251   memcpy(_pointer,pointer,size*sizeof(T));
252   _done=true;
253
254   return ;
255 }
256
257 template <typename T> void PointerOf<T>::setShallowAndOwnership( const T *pointer )
258 {
259   if ( _pointer && _done )
260     delete [] _pointer;
261   _pointer=(T*)pointer;
262   _done=true;
263 }
264
265 }//End namespace MEDMEM
266
267 # endif         /* # if ! defined( __PointerOf_HXX__ ) */