Back to index

salome-med  6.5.0
MEDMEM_PointerOf.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 # if ! defined( __PointerOf_HXX__ )
00024 # define __PointerOf_HXX__
00025 
00026 #include <cstdlib>
00027 #include <cstring>
00028 #include "MEDMEM_Utilities.hxx"
00029 #include "MEDMEM_Exception.hxx"
00030 
00039 namespace MEDMEM {
00040 
00041 template <typename T> class PointerOf
00042 {
00043 protected :
00045         T* _pointer ;
00047         bool _done ;
00048 
00049 public :
00050         PointerOf() ;
00051         ~PointerOf() ;
00052         PointerOf( const int &size ) ;
00053         PointerOf( const T *pointer ) ;
00054         PointerOf( const int &size, const T *pointer ) ;
00055         PointerOf( const PointerOf<T> & pointerOf ) ;
00057         operator T*() ;
00058         operator const T*() const ;
00059         void set( const int &size ) ;
00060         void set( const T *pointer ) ;
00061         void set( const int &size, const T *pointer ) ;
00062         void setShallowAndOwnership( const T *pointer );
00063         PointerOf<T>& operator=( const PointerOf<T> &pointer ) ;
00064 } ;
00065 
00066 // ------------------------------------------------------------ //
00067 //                                                              //
00068 //                      Implementation                          //
00069 //                                                              //
00070 // ------------------------------------------------------------ //
00071 
00073 template <typename T> PointerOf<T>::PointerOf() : _pointer(0), _done(false)
00074 {
00075 }
00076 
00083 template <typename T> PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf) :
00084   _pointer((T*)(const T* const)pointerOf), _done(false)
00085 {
00086   const char* LOC = "PointerOf<T>::PointerOf(const PointerOf<T> & pointerOf)";
00087   BEGIN_OF_MED(LOC);
00088         MESSAGE_MED("Warning ! No Propriety Transfer");
00089   END_OF_MED(LOC);
00090 }
00091 
00095 //template <typename T> PointerOf<T>::PointerOf( const int &size, const PointerOf<T> & pointerOf) : 
00096 //  _pointer((size,(T*)pointerOf))
00097 //{
00098 //}
00099 
00103 template <typename T> PointerOf<T>::PointerOf( const int &size )
00104 {
00105         if (size < 0)
00106         {
00107                 _pointer=(T*)NULL;
00108                 _done=false;
00109         }
00110         else
00111         {
00112                 _pointer = new T[ size ] ;
00113                 _done=true;
00114         }
00115 }
00116 
00120 template <typename T> PointerOf<T>::PointerOf( const T* pointer ) : _pointer( (T*)pointer ), _done(false)
00121 {
00122 }
00123 
00127 template <typename T> PointerOf<T>::PointerOf( const int &size, const T* pointer)
00128 {
00129   if (size < 0)
00130     throw MEDEXCEPTION("PointerOf( const int,const T*) : array size < 0");
00131 
00132   _pointer = new T[ size ] ;
00133   memcpy(_pointer,pointer,size*sizeof(T));
00134   _done=true;
00135 }
00136 
00139 template <typename T> PointerOf<T>::~PointerOf()
00140 {
00141         if ( _pointer )
00142         {
00143                 if( _done )
00144                 {
00145                         MESSAGE_MED("PointerOf<T>::~PointerOf() --> deleting _pointer") ;
00146                         delete [] _pointer ;
00147                         _done = false ;
00148                 }
00149                 else
00150                 {
00151                         MESSAGE_MED("_pointer is only nullified") ;
00152                 }
00153                 _pointer = 0 ;
00154         }
00155 }
00156 
00164 template <typename T> PointerOf<T>& PointerOf<T>::operator=( const PointerOf<T> &pointer )
00165 {
00166   const char* LOC = "PointerOf<T>::operator=( const PointerOf<T> &pointer )";
00167   BEGIN_OF_MED(LOC);
00168         if ( &pointer != this )
00169         {
00170                 this->set( pointer._pointer ) ;
00171         }
00172   END_OF_MED(LOC);
00173         return *this ;
00174 }
00175 
00177 template <typename T> PointerOf<T>::operator T*()
00178 {
00179         return _pointer ;
00180 }
00181 
00182 
00184 template <typename T> PointerOf<T>::operator const T*() const
00185 {
00186         return _pointer ;
00187 }
00188 
00193 template <typename T> void PointerOf<T>::set( const int &size )
00194 {
00195         if ( _pointer && _done )
00196         {
00197                 delete [] _pointer ;
00198                 _pointer=0 ;
00199         }
00200         // if (size < 0) TODO: analyse why it does not work
00201         if (size <= 0)
00202         {
00203                 _pointer=(T*)NULL;
00204         }
00205         else
00206         {
00207                 _pointer = new T[ size ] ;
00208         }
00209         _done = true ;
00210         return ;
00211 }
00212 
00217 template <typename T> void PointerOf<T>::set( const T *pointer )
00218 {
00219         MESSAGE_MED( "BEGIN PointerOf<T>::set( const T *pointer )" ) ;
00220         SCRUTE_MED(pointer) ;
00221         SCRUTE_MED(_done) ;
00222         if ( _pointer && _done )
00223         {
00224                 MESSAGE_MED("PointerOf<T>::set --> deleting _pointer") ;
00225                 delete [] _pointer ;
00226                 _pointer=0 ;
00227                 _done=false ;
00228         }
00229         _pointer=(T*)pointer ;
00230         _done=false ;
00231         MESSAGE_MED( "END PointerOf<T>::set( const T *pointer )" ) ;
00232         return ;
00233 }
00234 
00240 template <typename T> void PointerOf<T>::set( const int &size, const T *pointer)
00241 {
00242   if ( _pointer && _done )
00243     {
00244       delete [] _pointer ;
00245       _pointer = NULL ;
00246     }
00247   if (size < 0)
00248     throw MEDEXCEPTION("PointerOf( const int,const T*) : array size < 0");
00249 
00250   _pointer = new T[ size ] ;
00251   memcpy(_pointer,pointer,size*sizeof(T));
00252   _done=true;
00253 
00254   return ;
00255 }
00256 
00257 template <typename T> void PointerOf<T>::setShallowAndOwnership( const T *pointer )
00258 {
00259   if ( _pointer && _done )
00260     delete [] _pointer;
00261   _pointer=(T*)pointer;
00262   _done=true;
00263 }
00264 
00265 }//End namespace MEDMEM
00266 
00267 # endif         /* # if ! defined( __PointerOf_HXX__ ) */