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