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 PointerOf<T>& operator=( const PointerOf<T> &pointer ) ;
43 // ------------------------------------------------------------ //
47 // ------------------------------------------------------------ //
49 using namespace MEDMEM;
51 /*! Creates a null T* pointer and sets the boolean (for desallocation) to false. */
52 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
56 /*! Creates a standard T* pointer to the pointed memory. \n
57 The boolean for desallocation is setted to false. \n
59 - The "old" PointerOf always has propriety of the pointed memory. \n
60 - If the "old" PointerOf is detroyed, the "new" PointerOf points
61 a desallocated memory zone. */
62 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) :
63 _pointer((T*)(const T* const)pointerOf), _done(false)
65 BEGIN_OF("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
66 MESSAGE("Warning ! No Propriety Transfer");
67 END_OF ("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
71 Duplicate array of size size pointed in pointerOf.
73 template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) :
74 _pointer((size,(T*)pointerOf))
78 /*! If size <= 0, creates a null "T*" pointer\n
79 Else allocates memory and sets desallocation boolean to true./n
80 Memory will be desallocated when erasing this PointerOf*/
81 template <typename T> PointerOf<T>::PointerOf( const int &size )
90 _pointer = new T[ size ] ;
95 /*! Creates a standard pointer to the memory zone pointed by T*. /n
96 T* owner is in charged of memory desallocation. /n
97 Memory will not be released when erasing this PointerOf*/
98 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
102 /*! If size <= 0, return an exception\n
103 Else duplicate array and sets desallocation boolean to true./n
104 Memory will be desallocated when erasing this PointerOf*/
105 template <typename T> PointerOf<T>::PointerOf( const int &size, const T* pointer)
108 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
110 _pointer = new T[ size ] ;
111 memcpy(_pointer,pointer,size*sizeof(T));
115 /*! The destuctor desallocates memory if necessary (that is if the attribute _done equals true).\n
116 The attribute _pointer is nullified */
117 template <typename T> PointerOf<T>::~PointerOf()
123 MESSAGE("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
129 MESSAGE("_pointer is only nullified") ;
135 /*! Creates a standard pointer (T*) to the pointed memory. \n
136 The boolean for desallocation is setted to false. \n
138 - The "right" PointerOf always has propriety of the pointed memory. \n
139 - If the "right" PointerOf is detroyed, the "left" PointerOf points
140 a desallocated memory zone.
141 - it works the same way as PointerOf(const PointerOf<T> & pointerOf) */
142 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
144 BEGIN_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
145 if ( &pointer != this )
147 this->set( pointer._pointer ) ;
149 END_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
153 /*! Returns _pointer.*/
154 template <typename T> PointerOf<T>::operator T*()
160 /*! Returns _pointer.*/
161 template <typename T> PointerOf<T>::operator const T*() const
167 /*! If necessary, released memory holded by PointerOf/n.
168 Else allocates memory and sets desallocation boolean to true./n
169 Can be used in order to "nullify" an existing PointerOf/n
170 Memory will be desallocated when erasing this PointerOf*/
171 template <typename T> void PointerOf<T>::set( const int &size )
173 if ( _pointer && _done )
184 _pointer = new T[ size ] ;
190 /*! If necessary, released memory holded by PointerOf/n.
191 Then, sets _pointer to the memory zone pointed by T*. /n
192 T* owner is in charged of memory desallocation. /n
193 memory will not be released when erasing this PointerOf*/
194 template <typename T> void PointerOf<T>::set( const T *pointer )
196 MESSAGE( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
199 if ( _pointer && _done )
201 MESSAGE("PointerOf<T>::set --> deleting _pointer") ;
206 _pointer=(T*)pointer ;
208 MESSAGE( "END PointerOf<T>::set( const T *pointer )" ) ;
212 /*! If necessary, released memory holded by PointerOf/n.
213 If size <= 0, return an exception\n.
214 Else allocates memory and sets desallocation boolean to true./n
215 Can be used in order to "nullify" an existing PointerOf/n
216 Memory will be desallocated when erasing this PointerOf*/
217 template <typename T> void PointerOf<T>::set( const int &size, const T *pointer)
219 if ( _pointer && _done )
225 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
227 _pointer = new T[ size ] ;
228 memcpy(_pointer,pointer,size*sizeof(T));
234 # endif /* # if ! defined( __PointerOf_HXX__ ) */