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.
17 template <typename T> class PointerOf
20 /*! pointer to the pointed memory */
22 /*! boolean setted to true if memory has to be desallocated */
28 PointerOf( const int &size ) ;
29 PointerOf( const T *pointer ) ;
30 PointerOf( const int &size, const T *pointer ) ;
31 PointerOf( const PointerOf<T> & pointerOf ) ;
32 PointerOf( const int &size, const PointerOf<T> & pointerOf ) ;
34 operator const T*() const ;
35 void set( const int &size ) ;
36 void set( const T *pointer ) ;
37 void set( const int &size, const T *pointer ) ;
38 PointerOf<T>& operator=( const PointerOf<T> &pointer ) ;
41 // ------------------------------------------------------------ //
45 // ------------------------------------------------------------ //
48 /*! Creates a null T* pointer and sets the boolean (for desallocation) to false. */
49 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
53 /*! Creates a standard T* pointer to the pointed memory. \n
54 The boolean for desallocation is setted to false. \n
56 - The "old" PointerOf always has propriety of the pointed memory. \n
57 - If the "old" PointerOf is detroyed, the "new" PointerOf points
58 a desallocated memory zone. */
59 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) :
60 _pointer((T*)(const T* const)pointerOf), _done(false)
62 BEGIN_OF("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
63 MESSAGE("Warning ! No Propriety Transfer");
64 END_OF ("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
68 Duplicate array of size size pointed in pointerOf.
70 template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) :
71 _pointer((size,(T*)pointerOf))
75 /*! If size <= 0, creates a null "T*" pointer\n
76 Else allocates memory and sets desallocation boolean to true./n
77 Memory will be desallocated when erasing this PointerOf*/
78 template <typename T> PointerOf<T>::PointerOf( const int &size )
87 _pointer = new T[ size ] ;
92 /*! Creates a standard pointer to the memory zone pointed by T*. /n
93 T* owner is in charged of memory desallocation. /n
94 Memory will not be released when erasing this PointerOf*/
95 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
99 /*! If size <= 0, return an exception\n
100 Else duplicate array and sets desallocation boolean to true./n
101 Memory will be desallocated when erasing this PointerOf*/
102 template <typename T> PointerOf<T>::PointerOf( const int &size, const T* pointer)
105 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
107 _pointer = new T[ size ] ;
108 memcpy(_pointer,pointer,size*sizeof(T));
112 /*! The destuctor desallocates memory if necessary (that is if the attribute _done equals true).\n
113 The attribute _pointer is nullified */
114 template <typename T> PointerOf<T>::~PointerOf()
120 MESSAGE("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
126 MESSAGE("_pointer is only nullified") ;
132 /*! Creates a standard pointer (T*) to the pointed memory. \n
133 The boolean for desallocation is setted to false. \n
135 - The "right" PointerOf always has propriety of the pointed memory. \n
136 - If the "right" PointerOf is detroyed, the "left" PointerOf points
137 a desallocated memory zone.
138 - it works the same way as PointerOf(const PointerOf<T> & pointerOf) */
139 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
141 BEGIN_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
142 if ( &pointer != this )
144 this->set( pointer._pointer ) ;
146 END_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
150 /*! Returns _pointer.*/
151 template <typename T> PointerOf<T>::operator T*()
157 /*! Returns _pointer.*/
158 template <typename T> PointerOf<T>::operator const T*() const
164 /*! If necessary, released memory holded by PointerOf/n.
165 Else allocates memory and sets desallocation boolean to true./n
166 Can be used in order to "nullify" an existing PointerOf/n
167 Memory will be desallocated when erasing this PointerOf*/
168 template <typename T> void PointerOf<T>::set( const int &size )
170 if ( _pointer && _done )
181 _pointer = new T[ size ] ;
187 /*! If necessary, released memory holded by PointerOf/n.
188 Then, sets _pointer to the memory zone pointed by T*. /n
189 T* owner is in charged of memory desallocation. /n
190 memory will not be released when erasing this PointerOf*/
191 template <typename T> void PointerOf<T>::set( const T *pointer )
193 MESSAGE( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
196 if ( _pointer && _done )
198 MESSAGE("PointerOf<T>::set --> deleting _pointer") ;
203 _pointer=(T*)pointer ;
205 MESSAGE( "END PointerOf<T>::set( const T *pointer )" ) ;
209 /*! If necessary, released memory holded by PointerOf/n.
210 If size <= 0, return an exception\n.
211 Else allocates memory and sets desallocation boolean to true./n
212 Can be used in order to "nullify" an existing PointerOf/n
213 Memory will be desallocated when erasing this PointerOf*/
214 template <typename T> void PointerOf<T>::set( const int &size, const T *pointer)
216 if ( _pointer && _done )
222 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
224 _pointer = new T[ size ] ;
225 memcpy(_pointer,pointer,size*sizeof(T));
231 # endif /* # if ! defined( __PointerOf_HXX__ ) */