1 # if ! defined( __PointerOf_HXX__ )
2 # define __PointerOf_HXX__
6 #include "MEDMEM_Exception.hxx"
9 The template class PointerOf embedding a standard pointer (_pointer) is in charge of
10 managing the pointed memory.\n
12 the object PointerOf is the memory owner if a size is given at object construction.
13 In these cases, memory will be desallocated at object destruction. In all other cases,
14 the desallocator will only nullify pointers.
18 template <typename T> class PointerOf
21 /*! pointer to the pointed memory */
23 /*! boolean setted to true if memory has to be desallocated */
29 PointerOf( const int &size ) ;
30 PointerOf( const T *pointer ) ;
31 PointerOf( const int &size, const T *pointer ) ;
32 PointerOf( const PointerOf<T> & pointerOf ) ;
33 PointerOf( const int &size, const PointerOf<T> & pointerOf ) ;
35 operator const T*() const ;
36 void set( const int &size ) ;
37 void set( const T *pointer ) ;
38 void set( const int &size, const T *pointer ) ;
39 void setShallowAndOwnership( const T *pointer );
40 PointerOf<T>& operator=( const PointerOf<T> &pointer ) ;
43 // ------------------------------------------------------------ //
47 // ------------------------------------------------------------ //
49 /*! Creates a null T* pointer and sets the boolean (for desallocation) to false. */
50 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
54 /*! Creates a standard T* pointer to the pointed memory. \n
55 The boolean for desallocation is setted to false. \n
57 - The "old" PointerOf always has propriety of the pointed memory. \n
58 - If the "old" PointerOf is detroyed, the "new" PointerOf points
59 a desallocated memory zone. */
60 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) :
61 _pointer((T*)(const T* const)pointerOf), _done(false)
63 BEGIN_OF("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
64 MESSAGE("Warning ! No Propriety Transfer");
65 END_OF ("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
69 Duplicate array of size size pointed in pointerOf.
71 template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) :
72 _pointer((size,(T*)pointerOf))
76 /*! If size <= 0, creates a null "T*" pointer\n
77 Else allocates memory and sets desallocation boolean to true.\n
78 Memory will be desallocated when erasing this PointerOf*/
79 template <typename T> PointerOf<T>::PointerOf( const int &size )
88 _pointer = new T[ size ] ;
93 /*! Creates a standard pointer to the memory zone pointed by T*. \n
94 T* owner is in charged of memory desallocation. \n
95 Memory will not be released when erasing this PointerOf*/
96 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
100 /*! If size <= 0, return an exception\n
101 Else duplicate array 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, const T* pointer)
106 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
108 _pointer = new T[ size ] ;
109 memcpy(_pointer,pointer,size*sizeof(T));
113 /*! The destuctor desallocates memory if necessary (that is if the attribute _done equals true).\n
114 The attribute _pointer is nullified */
115 template <typename T> PointerOf<T>::~PointerOf()
121 MESSAGE("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
127 MESSAGE("_pointer is only nullified") ;
133 /*! Creates a standard pointer (T*) to the pointed memory. \n
134 The boolean for desallocation is setted to false. \n
136 - The "right" PointerOf always has propriety of the pointed memory. \n
137 - If the "right" PointerOf is detroyed, the "left" PointerOf points
138 a desallocated memory zone.
139 - it works the same way as PointerOf(const PointerOf<T> & pointerOf) */
140 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
142 BEGIN_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
143 if ( &pointer != this )
145 this->set( pointer._pointer ) ;
147 END_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
151 /*! Returns _pointer.*/
152 template <typename T> PointerOf<T>::operator T*()
158 /*! Returns _pointer.*/
159 template <typename T> PointerOf<T>::operator const T*() const
165 /*! If necessary, released memory holded by PointerOf\n.
166 Else allocates memory and sets desallocation boolean to true.\n
167 Can be used in order to "nullify" an existing PointerOf\n
168 Memory will be desallocated when erasing this PointerOf*/
169 template <typename T> void PointerOf<T>::set( const int &size )
171 if ( _pointer && _done )
182 _pointer = new T[ size ] ;
188 /*! If necessary, released memory holded by PointerOf\n.
189 Then, sets _pointer to the memory zone pointed by T*. \n
190 T* owner is in charged of memory desallocation. \n
191 memory will not be released when erasing this PointerOf*/
192 template <typename T> void PointerOf<T>::set( const T *pointer )
194 MESSAGE( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
197 if ( _pointer && _done )
199 MESSAGE("PointerOf<T>::set --> deleting _pointer") ;
204 _pointer=(T*)pointer ;
206 MESSAGE( "END PointerOf<T>::set( const T *pointer )" ) ;
210 /*! If necessary, released memory holded by PointerOf\n.
211 If size <= 0, return an exception\n.
212 Else allocates memory and sets desallocation boolean to true.\n
213 Can be used in order to "nullify" an existing PointerOf\n
214 Memory will be desallocated when erasing this PointerOf*/
215 template <typename T> void PointerOf<T>::set( const int &size, const T *pointer)
217 if ( _pointer && _done )
223 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
225 _pointer = new T[ size ] ;
226 memcpy(_pointer,pointer,size*sizeof(T));
232 template <typename T> void PointerOf<T>::setShallowAndOwnership( const T *pointer )
234 if ( _pointer && _done )
236 _pointer=(T*)pointer;
240 }//End namespace MEDMEM
242 # endif /* # if ! defined( __PointerOf_HXX__ ) */