]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_PointerOf.hxx
Salome HOME
NRI : Merge from V1_2.
[modules/med.git] / src / MEDMEM / MEDMEM_PointerOf.hxx
1 //  MED MEDMEM : MED files in memory
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : MEDMEM_PointerOf.hxx
25 //  Module : MED
26
27 # if ! defined( __PointerOf_HXX__ )
28 # define __PointerOf_HXX__
29
30 #include <cstdlib>
31 #include "utilities.h"
32 #include "MEDMEM_Exception.hxx"
33
34 /*!
35         The template class PointerOf embedding a standard pointer (_pointer) is in charge of
36         managing the pointed memory.\n
37         
38         the object PointerOf is the memory owner if a size is given at object construction.
39         In these cases, memory will be desallocated at object destruction. In all other cases,
40         the desallocator will only nullify pointers.
41 */
42
43 template <typename T> class PointerOf
44 {
45 protected :
46                         /*! pointer to the pointed memory */
47         T* _pointer ;
48                         /*! boolean setted to true if memory has to be desallocated */
49         bool _done ;
50
51 public :
52         PointerOf() ;
53         ~PointerOf() ;
54         PointerOf( const int &size ) ;
55         PointerOf( const T *pointer ) ;
56         PointerOf( const int &size, const T *pointer ) ;
57         PointerOf( const PointerOf<T> & pointerOf ) ;
58         PointerOf( const int &size, const PointerOf<T> & pointerOf ) ;
59         operator T*() ;
60         operator const T*() const ;
61         void set( const int &size ) ;
62         void set( const T *pointer ) ;
63         void set( const int &size, const T *pointer ) ;
64         PointerOf<T>& operator=( const PointerOf<T> &pointer ) ;
65 } ;
66
67 // ------------------------------------------------------------ //
68 //                                                              //
69 //                      Implementation                          //
70 //                                                              //
71 // ------------------------------------------------------------ //
72
73
74 /*! Creates a null T* pointer and sets the boolean (for desallocation) to false. */
75 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
76 {
77 }
78
79 /*! Creates a standard T* pointer to the pointed memory. \n
80     The boolean for desallocation is setted to false. \n
81     Be aware : \n
82     - The "old" PointerOf always has propriety of the pointed memory. \n
83     - If the "old" PointerOf is detroyed, the "new" PointerOf points
84       a desallocated memory zone. */
85 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) : _done(false),
86                                                                                 _pointer((T*)(const T* const)pointerOf)
87 {
88         BEGIN_OF("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
89         MESSAGE("Warning ! No Propriety Transfer");
90         END_OF  ("PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)");
91 }
92
93 /*! 
94   Duplicate array of size size pointed in pointerOf.
95 */
96 template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) : 
97   _pointer((size,(T*)pointerOf))
98 {
99 }
100
101 /*! If size <= 0, creates a null "T*" pointer\n
102     Else allocates memory and sets desallocation boolean to true./n
103     Memory will be desallocated  when erasing this PointerOf*/
104 template <typename T> PointerOf<T>::PointerOf( const int &size )
105 {
106         if (size <= 0)
107         {
108                 _pointer=(T*)NULL;
109                 _done=false;
110         }
111         else
112         {
113                 _pointer = new T[ size ] ;
114                 _done=true;
115         }
116 }
117
118 /*! Creates a standard pointer to the memory zone pointed by T*. /n
119    T* owner is in charged of memory desallocation. /n
120    Memory will not be released when erasing this PointerOf*/
121 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
122 {
123 }
124
125 /*! If size <= 0, return an exception\n
126     Else duplicate array and sets desallocation boolean to true./n
127     Memory will be desallocated  when erasing this PointerOf*/
128 template <typename T> PointerOf<T>::PointerOf( const int &size, const T* pointer)
129 {
130   if (size <= 0)
131     throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
132
133   _pointer = new T[ size ] ;
134   memcpy(_pointer,pointer,size*sizeof(T));
135   _done=true;
136 }
137
138 /*! The destuctor desallocates memory if necessary (that is if the attribute _done equals true).\n
139     The attribute _pointer is nullified */
140 template <typename T> PointerOf<T>::~PointerOf()
141 {
142         if ( _pointer )
143         {
144                 if( _done )
145                 {
146                         MESSAGE("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
147                         delete [] _pointer ;
148                         _done = false ;
149                 }
150                 else
151                 {
152                         MESSAGE("_pointer is only nullified") ;
153                 }
154                 _pointer = 0 ;
155         }
156 }
157
158 /*! Creates a standard pointer (T*) to the pointed memory. \n
159     The boolean for desallocation is setted to false. \n
160     Be aware : \n
161     - The "right" PointerOf always has propriety of the pointed memory. \n
162     - If the "right" PointerOf is detroyed, the "left" PointerOf points
163       a desallocated memory zone.
164     - it works the same way as PointerOf(const PointerOf<T> & pointerOf) */
165 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
166 {
167         BEGIN_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
168         if ( &pointer != this )
169         {
170                 this->set( pointer._pointer ) ;
171         }
172         END_OF("PointerOf<T>::operator=( const PointerOf<T> &pointer )") ;
173         return *this ;
174 }
175
176 /*! Returns _pointer.*/
177 template <typename T> PointerOf<T>::operator T*()
178 {
179         return _pointer ;
180 }
181
182
183 /*! Returns _pointer.*/
184 template <typename T> PointerOf<T>::operator const T*() const
185 {
186         return _pointer ;
187 }
188
189
190 /*! If necessary, released memory holded by PointerOf/n.
191     Else allocates memory and sets desallocation boolean to true./n
192     Can be used in order to "nullify" an existing PointerOf/n
193     Memory will be desallocated  when erasing this PointerOf*/
194 template <typename T> void PointerOf<T>::set( const int &size )
195 {
196         if ( _pointer && _done )
197         {
198                 delete [] _pointer ;
199                 _pointer=0 ;
200         }
201         if (size <= 0)
202         {
203                 _pointer=(T*)NULL;
204         }
205         else
206         {
207                 _pointer = new T[ size ] ;
208         }
209         _done = true ;
210         return ;
211 }
212
213 /*! If necessary, released memory holded by PointerOf/n.
214     Then, sets _pointer to the memory zone pointed by T*. /n
215     T* owner is in charged of memory desallocation. /n
216     memory will not be released when erasing this PointerOf*/
217 template <typename T> void PointerOf<T>::set( const T *pointer )
218 {
219         MESSAGE( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
220         SCRUTE(pointer) ;
221         SCRUTE(_done) ;
222         if ( _pointer && _done )
223         {
224                 MESSAGE("PointerOf<T>::set --> deleting _pointer") ;
225                 delete [] _pointer ;
226                 _pointer=0 ;
227                 _done=false ;
228         }
229         _pointer=(T*)pointer ;
230         _done=false ;
231         MESSAGE( "END PointerOf<T>::set( const T *pointer )" ) ;
232         return ;
233 }
234
235 /*! If necessary, released memory holded by PointerOf/n.
236     If size <= 0, return an exception\n.
237     Else allocates memory and sets desallocation boolean to true./n
238     Can be used in order to "nullify" an existing PointerOf/n
239     Memory will be desallocated  when erasing this PointerOf*/
240 template <typename T> void PointerOf<T>::set( const int &size, const T *pointer)
241 {
242   if ( _pointer && _done )
243     {
244       delete [] _pointer ;
245       _pointer = NULL ;
246     }
247   if (size <= 0)
248     throw MEDEXCEPTION("PointerOf( const int,const T*) : array size <= 0");
249
250   _pointer = new T[ size ] ;
251   memcpy(_pointer,pointer,size*sizeof(T));
252   _done=true;
253
254   return ;
255 }
256
257 # endif         /* # if ! defined( __PointerOf_HXX__ ) */