1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 # if ! defined( __PointerOf_HXX__ )
23 # define __PointerOf_HXX__
26 #include "MEDMEM_Utilities.hxx"
27 #include "MEDMEM_Exception.hxx"
30 The template class PointerOf embedding a standard pointer (_pointer) is in charge of
31 managing the pointed memory.\n
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.
39 template <typename T> class PointerOf
42 /*! pointer to the pointed memory */
44 /*! boolean setted to true if memory has to be desallocated */
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 ) ;
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 ) ;
64 // ------------------------------------------------------------ //
68 // ------------------------------------------------------------ //
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)
75 /*! Creates a standard T* pointer to the pointed memory. \n
76 The boolean for desallocation is setted to false. \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)
84 const char* LOC = "PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)";
86 MESSAGE_MED("Warning ! No Propriety Transfer");
91 Duplicate array of size size pointed in pointerOf.
93 //template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) :
94 // _pointer((size,(T*)pointerOf))
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 )
110 _pointer = new T[ size ] ;
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)
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)
128 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size < 0");
130 _pointer = new T[ size ] ;
131 memcpy(_pointer,pointer,size*sizeof(T));
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()
143 MESSAGE_MED("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
149 MESSAGE_MED("_pointer is only nullified") ;
155 /*! Creates a standard pointer (T*) to the pointed memory. \n
156 The boolean for desallocation is setted to false. \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 )
164 const char* LOC = "PointerOf<T>::operator=( const PointerOf<T> &pointer )";
166 if ( &pointer != this )
168 this->set( pointer._pointer ) ;
174 /*! Returns _pointer.*/
175 template <typename T> PointerOf<T>::operator T*()
181 /*! Returns _pointer.*/
182 template <typename T> PointerOf<T>::operator const T*() const
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 )
193 if ( _pointer && _done )
198 // if (size < 0) TODO: analyse why it does not work
205 _pointer = new T[ size ] ;
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 )
217 MESSAGE_MED( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
218 SCRUTE_MED(pointer) ;
220 if ( _pointer && _done )
222 MESSAGE_MED("PointerOf<T>::set --> deleting _pointer") ;
227 _pointer=(T*)pointer ;
229 MESSAGE_MED( "END PointerOf<T>::set( const T *pointer )" ) ;
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)
240 if ( _pointer && _done )
246 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size < 0");
248 _pointer = new T[ size ] ;
249 memcpy(_pointer,pointer,size*sizeof(T));
255 template <typename T> void PointerOf<T>::setShallowAndOwnership( const T *pointer )
257 if ( _pointer && _done )
259 _pointer=(T*)pointer;
263 }//End namespace MEDMEM
265 # endif /* # if ! defined( __PointerOf_HXX__ ) */