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 ) ;
44 // ------------------------------------------------------------ //
48 // ------------------------------------------------------------ //
50 using namespace MEDMEM;
52 /*! Creates a null T* pointer and sets the boolean (for desallocation) to false. */
53 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
57 /*! Creates a standard T* pointer to the pointed memory. \n
58 The boolean for desallocation is setted to false. \n
60 - The "old" PointerOf always has propriety of the pointed memory. \n
61 - If the "old" PointerOf is detroyed, the "new" PointerOf points
62 a desallocated memory zone. */
63 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) :
64 _pointer((T*)(const T* const)pointerOf), _done(false)
66 BEGIN_OF("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
67 MESSAGE("Warning ! No Propriety Transfer");
68 END_OF ("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
72 Duplicate array of size size pointed in pointerOf.
74 template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) :
75 _pointer((size,(T*)pointerOf))
79 /*! If size <= 0, creates a null "T*" pointer\n
80 Else allocates memory and sets desallocation boolean to true.\n
81 Memory will be desallocated when erasing this PointerOf*/
82 template <typename T> PointerOf<T>::PointerOf( const int &size )
91 _pointer = new T[ size ] ;
96 /*! Creates a standard pointer to the memory zone pointed by T*. \n
97 T* owner is in charged of memory desallocation. \n
98 Memory will not be released when erasing this PointerOf*/
99 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
103 /*! If size <= 0, return an exception\n
104 Else duplicate array and sets desallocation boolean to true.\n
105 Memory will be desallocated when erasing this PointerOf*/
106 template <typename T> PointerOf<T>::PointerOf( const int &size, const T* pointer)
109 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
111 _pointer = new T[ size ] ;
112 memcpy(_pointer,pointer,size*sizeof(T));
116 /*! The destuctor desallocates memory if necessary (that is if the attribute _done equals true).\n
117 The attribute _pointer is nullified */
118 template <typename T> PointerOf<T>::~PointerOf()
124 MESSAGE("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
130 MESSAGE("_pointer is only nullified") ;
136 /*! Creates a standard pointer (T*) to the pointed memory. \n
137 The boolean for desallocation is setted to false. \n
139 - The "right" PointerOf always has propriety of the pointed memory. \n
140 - If the "right" PointerOf is detroyed, the "left" PointerOf points
141 a desallocated memory zone.
142 - it works the same way as PointerOf(const PointerOf<T> & pointerOf) */
143 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
145 BEGIN_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
146 if ( &pointer != this )
148 this->set( pointer._pointer ) ;
150 END_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
154 /*! Returns _pointer.*/
155 template <typename T> PointerOf<T>::operator T*()
161 /*! Returns _pointer.*/
162 template <typename T> PointerOf<T>::operator const T*() const
168 /*! If necessary, released memory holded by PointerOf\n.
169 Else allocates memory and sets desallocation boolean to true.\n
170 Can be used in order to "nullify" an existing PointerOf\n
171 Memory will be desallocated when erasing this PointerOf*/
172 template <typename T> void PointerOf<T>::set( const int &size )
174 if ( _pointer && _done )
185 _pointer = new T[ size ] ;
191 /*! If necessary, released memory holded by PointerOf\n.
192 Then, sets _pointer to the memory zone pointed by T*. \n
193 T* owner is in charged of memory desallocation. \n
194 memory will not be released when erasing this PointerOf*/
195 template <typename T> void PointerOf<T>::set( const T *pointer )
197 MESSAGE( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
200 if ( _pointer && _done )
202 MESSAGE("PointerOf<T>::set --> deleting _pointer") ;
207 _pointer=(T*)pointer ;
209 MESSAGE( "END PointerOf<T>::set( const T *pointer )" ) ;
213 /*! If necessary, released memory holded by PointerOf\n.
214 If size <= 0, return an exception\n.
215 Else allocates memory and sets desallocation boolean to true.\n
216 Can be used in order to "nullify" an existing PointerOf\n
217 Memory will be desallocated when erasing this PointerOf*/
218 template <typename T> void PointerOf<T>::set( const int &size, const T *pointer)
220 if ( _pointer && _done )
226 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
228 _pointer = new T[ size ] ;
229 memcpy(_pointer,pointer,size*sizeof(T));
235 template <typename T> void PointerOf<T>::setShallowAndOwnership( const T *pointer )
237 if ( _pointer && _done )
239 _pointer=(T*)pointer;
243 # endif /* # if ! defined( __PointerOf_HXX__ ) */