1 // Copyright (C) 2007-2012 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
23 # if ! defined( __PointerOf_HXX__ )
24 # define __PointerOf_HXX__
28 #include "MEDMEM_Utilities.hxx"
29 #include "MEDMEM_Exception.hxx"
32 The template class PointerOf embedding a standard pointer (_pointer) is in charge of
33 managing the pointed memory.\n
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.
41 template <typename T> class PointerOf
44 /*! pointer to the pointed memory */
46 /*! boolean setted to true if memory has to be desallocated */
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 ) ;
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 ) ;
66 // ------------------------------------------------------------ //
70 // ------------------------------------------------------------ //
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)
77 /*! Creates a standard T* pointer to the pointed memory. \n
78 The boolean for desallocation is setted to false. \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)
86 const char* LOC = "PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)";
88 MESSAGE_MED("Warning ! No Propriety Transfer");
93 Duplicate array of size size pointed in pointerOf.
95 //template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) :
96 // _pointer((size,(T*)pointerOf))
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 )
112 _pointer = new T[ size ] ;
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)
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)
130 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size < 0");
132 _pointer = new T[ size ] ;
133 memcpy(_pointer,pointer,size*sizeof(T));
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()
145 MESSAGE_MED("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
151 MESSAGE_MED("_pointer is only nullified") ;
157 /*! Creates a standard pointer (T*) to the pointed memory. \n
158 The boolean for desallocation is setted to false. \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 )
166 const char* LOC = "PointerOf<T>::operator=( const PointerOf<T> &pointer )";
168 if ( &pointer != this )
170 this->set( pointer._pointer ) ;
176 /*! Returns _pointer.*/
177 template <typename T> PointerOf<T>::operator T*()
183 /*! Returns _pointer.*/
184 template <typename T> PointerOf<T>::operator const T*() const
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 )
195 if ( _pointer && _done )
200 // if (size < 0) TODO: analyse why it does not work
207 _pointer = new T[ size ] ;
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 )
219 MESSAGE_MED( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
220 SCRUTE_MED(pointer) ;
222 if ( _pointer && _done )
224 MESSAGE_MED("PointerOf<T>::set --> deleting _pointer") ;
229 _pointer=(T*)pointer ;
231 MESSAGE_MED( "END PointerOf<T>::set( const T *pointer )" ) ;
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)
242 if ( _pointer && _done )
248 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size < 0");
250 _pointer = new T[ size ] ;
251 memcpy(_pointer,pointer,size*sizeof(T));
257 template <typename T> void PointerOf<T>::setShallowAndOwnership( const T *pointer )
259 if ( _pointer && _done )
261 _pointer=(T*)pointer;
265 }//End namespace MEDMEM
267 # endif /* # if ! defined( __PointerOf_HXX__ ) */