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
10 charge of managing the pointed memory.\n
12 the object PointerOf is the memory owner if a size is given at object
13 construction. In these cases, memory will be desallocated at object
14 destruction. In all other cases, the desallocator will only nullify pointers.
18 template <typename T> class PointerOf
22 pointer to the pointed memory
28 boolean setted to true if memory has to be desallocated
36 PointerOf( const int &size ) ;
37 PointerOf( const T *pointer ) ;
38 PointerOf( const int &size, const T *pointer ) ;
39 PointerOf( const PointerOf<T> & pointerOf ) ;
40 PointerOf( const int &size, const PointerOf<T> & pointerOf ) ;
42 operator const T*() const ;
43 void set( const int &size ) ;
44 void set( const T *pointer ) ;
45 void set( const int &size, const T *pointer ) ;
46 PointerOf<T>& operator=( const PointerOf<T> &pointer ) ;
54 using namespace MEDMEM;
57 Creates a null T* pointer and sets the boolean (for desallocation) to false.
60 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
62 BEGIN_OF("PointerOf<T>::PointerOf() empty constructor");
63 END_OF("PointerOf<T>::PointerOf() empty constructor");
67 Creates a standard T* pointer to the pointed memory. \n
68 The boolean for desallocation is setted to false. \n
70 - The "old" PointerOf always has propriety of the pointed memory. \n
71 - If the "old" PointerOf is detroyed, the "new" PointerOf points
72 a desallocated memory zone.
75 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) :
76 _pointer((T*)(const T* const)pointerOf), _done(false)
78 BEGIN_OF("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
79 MESSAGE("Warning ! No Propriety Transfer");
80 END_OF ("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
84 Duplicate array of size size pointed in pointerOf.
87 template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) :
88 _pointer((size,(T*)pointerOf))
93 If size <= 0, creates a null "T*" pointer\n
94 Else allocates memory and sets desallocation boolean to true./n
95 Memory will be desallocated when erasing this PointerOf
98 template <typename T> PointerOf<T>::PointerOf( const int &size )
107 _pointer = new T[ size ] ;
112 /*! Creates a standard pointer to the memory zone pointed by T*. /n
113 T* owner is in charged of memory desallocation. /n
114 Memory will not be released when erasing this PointerOf
117 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
122 If size <= 0, return an exception\n
123 Else duplicate array and sets desallocation boolean to true./n
124 Memory will be desallocated when erasing this PointerOf
127 template <typename T> PointerOf<T>::PointerOf( const int &size, const T* pointer)
130 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
132 _pointer = new T[ size ] ;
133 memcpy(_pointer,pointer,size*sizeof(T));
138 The destuctor desallocates memory if necessary (that is if the attribute
139 _done equals true).\n
140 The attribute _pointer is nullified
143 template <typename T> PointerOf<T>::~PointerOf()
149 MESSAGE("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
155 MESSAGE("_pointer is only nullified") ;
162 Creates a standard pointer (T*) to the pointed memory. \n
163 The boolean for desallocation is setted to false. \n
165 - The "right" PointerOf always has propriety of the pointed memory. \n
166 - If the "right" PointerOf is detroyed, the "left" PointerOf points
167 a desallocated memory zone.
168 - it works the same way as PointerOf(const PointerOf<T> & pointerOf)
171 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
173 BEGIN_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
174 if ( &pointer != this )
176 this->set( pointer._pointer ) ;
178 END_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
186 template <typename T> PointerOf<T>::operator T*()
195 template <typename T> PointerOf<T>::operator const T*() const
201 If necessary, released memory holded by PointerOf/n.
202 Else allocates memory and sets desallocation boolean to true./n
203 Can be used in order to "nullify" an existing PointerOf/n
204 Memory will be desallocated when erasing this PointerOf
207 template <typename T> void PointerOf<T>::set( const int &size )
209 if ( _pointer && _done )
220 _pointer = new T[ size ] ;
225 /*! If necessary, released memory holded by PointerOf/n.
226 Then, sets _pointer to the memory zone pointed by T*. /n
227 T* owner is in charged of memory desallocation. /n
228 memory will not be released when erasing this PointerOf*/
230 template <typename T> void PointerOf<T>::set( const T *pointer )
232 MESSAGE( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
235 if ( _pointer && _done )
237 MESSAGE("PointerOf<T>::set --> deleting _pointer") ;
242 _pointer=(T*)pointer ;
244 MESSAGE( "END PointerOf<T>::set( const T *pointer )" ) ;
248 If necessary, released memory holded by PointerOf/n.
249 If size <= 0, return an exception\n.
250 Else allocates memory and sets desallocation boolean to true./n
251 Can be used in order to "nullify" an existing PointerOf/n
252 Memory will be desallocated when erasing this PointerOf
255 template <typename T> void PointerOf<T>::set( const int &size, const T *pointer)
257 if ( _pointer && _done )
263 throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
265 _pointer = new T[ size ] ;
266 memcpy(_pointer,pointer,size*sizeof(T));
270 # endif /* # if ! defined( __PointerOf_HXX__ ) */