Back to index

salome-med  6.5.0
MEDCouplingAutoRefCountObjectPtr.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #ifndef __PARAMEDMEM_MEDCOUPLINGAUTOREFCOUNTOBJECTPTR_HXX__
00021 #define __PARAMEDMEM_MEDCOUPLINGAUTOREFCOUNTOBJECTPTR_HXX__
00022 
00023 #include "MEDCouplingRefCountObject.hxx"
00024 
00025 namespace ParaMEDMEM
00026 {
00027   template<class T>
00028   class MEDCouplingAutoRefCountObjectPtr
00029   {
00030   public:
00031     MEDCouplingAutoRefCountObjectPtr(const MEDCouplingAutoRefCountObjectPtr& other):_ptr(0) { referPtr(other._ptr); }
00032     MEDCouplingAutoRefCountObjectPtr(T *ptr=0):_ptr(ptr) { }
00033     ~MEDCouplingAutoRefCountObjectPtr() { destroyPtr(); }
00034     bool operator==(const MEDCouplingAutoRefCountObjectPtr& other) { return _ptr==other._ptr; }
00035     MEDCouplingAutoRefCountObjectPtr &operator=(const MEDCouplingAutoRefCountObjectPtr& other) { if(_ptr!=other._ptr) { destroyPtr(); referPtr(other._ptr); } return *this; }
00036     MEDCouplingAutoRefCountObjectPtr &operator=(T *ptr) { if(_ptr!=ptr) { destroyPtr(); _ptr=ptr; } return *this; }
00037     T *operator->() { return _ptr ; }
00038     const T *operator->() const { return _ptr; }
00039     T& operator*() { return *_ptr; }
00040     const T& operator*() const { return *_ptr; }
00041     operator T *() { return _ptr; }
00042     operator const T *() const { return _ptr; }
00043   private:
00044     void referPtr(T *ptr) { _ptr=ptr; if(_ptr) _ptr->incrRef(); }
00045     void destroyPtr() { if(_ptr) _ptr->decrRef(); }
00046   private:
00047     T *_ptr;
00048   };
00049 }
00050 
00051 #endif