1 // MED MEDMEM : MED files in memory
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : MEDMEM_PointerOf.hxx
27 # if ! defined( __PointerOf_HXX__ )
28 # define __PointerOf_HXX__
31 #include "utilities.h"
32 #include "MEDMEM_Exception.hxx"
35 The template class PointerOf embedding a standard pointer (_pointer) is in charge of
36 managing the pointed memory.\n
38 the object PointerOf is the memory owner if a size is given at object construction.
39 In these cases, memory will be desallocated at object destruction. In all other cases,
40 the desallocator will only nullify pointers.
43 template <typename T> class PointerOf
46 /*! pointer to the pointed memory */
48 /*! boolean setted to true if memory has to be desallocated */
54 PointerOf( const int &size ) ;
55 PointerOf( const T *pointer ) ;
56 PointerOf( const int &size, const T *pointer ) ;
57 PointerOf( const PointerOf<T> & pointerOf ) ;
58 PointerOf( const int &size, const PointerOf<T> & pointerOf ) ;
60 operator const T*() const ;
61 void set( const int &size ) ;
62 void set( const T *pointer ) ;
63 void set( const int &size, const T *pointer ) ;
64 PointerOf<T>& operator=( const PointerOf<T> &pointer ) ;
67 // ------------------------------------------------------------ //
71 // ------------------------------------------------------------ //
74 /*! Creates a null T* pointer and sets the boolean (for desallocation) to false. */
75 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
79 /*! Creates a standard T* pointer to the pointed memory. \n
80 The boolean for desallocation is setted to false. \n
82 - The "old" PointerOf always has propriety of the pointed memory. \n
83 - If the "old" PointerOf is detroyed, the "new" PointerOf points
84 a desallocated memory zone. */
85 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) : _done(false),
86 _pointer((T*)(const T* const)pointerOf)
88 BEGIN_OF("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
89 MESSAGE("Warning ! No Propriety Transfer");
90 END_OF ("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
94 Duplicate array of size size pointed in pointerOf.
96 template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) :
97 _pointer((size,(T*)pointerOf))
101 /*! If size <= 0, creates a null "T*" pointer\n
102 Else allocates memory and sets desallocation boolean to true./n
103 Memory will be desallocated when erasing this PointerOf*/
104 template <typename T> PointerOf<T>::PointerOf( const int &size )
113 _pointer = new T[ size ] ;
118 /*! Creates a standard pointer to the memory zone pointed by T*. /n
119 T* owner is in charged of memory desallocation. /n
120 Memory will not be released when erasing this PointerOf*/
121 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
125 /*! If size <= 0, return an exception\n
126 Else duplicate array and sets desallocation boolean to true./n
127 Memory will be desallocated when erasing this PointerOf*/
128 template <typename T> PointerOf<T>::PointerOf( const int &size, const T* pointer)
131 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
133 _pointer = new T[ size ] ;
134 memcpy(_pointer,pointer,size*sizeof(T));
138 /*! The destuctor desallocates memory if necessary (that is if the attribute _done equals true).\n
139 The attribute _pointer is nullified */
140 template <typename T> PointerOf<T>::~PointerOf()
146 MESSAGE("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
152 MESSAGE("_pointer is only nullified") ;
158 /*! Creates a standard pointer (T*) to the pointed memory. \n
159 The boolean for desallocation is setted to false. \n
161 - The "right" PointerOf always has propriety of the pointed memory. \n
162 - If the "right" PointerOf is detroyed, the "left" PointerOf points
163 a desallocated memory zone.
164 - it works the same way as PointerOf(const PointerOf<T> & pointerOf) */
165 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
167 BEGIN_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
168 if ( &pointer != this )
170 this->set( pointer._pointer ) ;
172 END_OF("PointerOf<T>::operator=( const PointerOf<T> &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
190 /*! If necessary, released memory holded by PointerOf/n.
191 Else allocates memory and sets desallocation boolean to true./n
192 Can be used in order to "nullify" an existing PointerOf/n
193 Memory will be desallocated when erasing this PointerOf*/
194 template <typename T> void PointerOf<T>::set( const int &size )
196 if ( _pointer && _done )
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( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
222 if ( _pointer && _done )
224 MESSAGE("PointerOf<T>::set --> deleting _pointer") ;
229 _pointer=(T*)pointer ;
231 MESSAGE( "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 # endif /* # if ! defined( __PointerOf_HXX__ ) */