00001 // Copyright (C) 2007-2011 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(__SUIT_SMARTPTR_H) 00024 #define __SUIT_SMARTPTR_H 00025 00026 #include "SUIT.h" 00027 00029 class SUIT_EXPORT RefCount { 00030 public: 00032 RefCount() : crefs( 0 ) {} 00034 RefCount( const RefCount& ) : crefs( 0 ) {} 00036 virtual ~RefCount() {} 00038 RefCount& operator=( const RefCount& ) { return *this; } 00039 00041 void upcount() { 00042 ++crefs; 00043 } 00044 00046 void downcount() 00047 { 00048 if ( crefs > 0 && --crefs == 0 ) 00049 delete this; 00050 } 00051 00053 int refcount() const { return crefs; } 00054 00055 private: 00056 unsigned long crefs; 00057 }; 00058 00060 template <class T> class SmartPtr { 00061 public: 00063 SmartPtr() : p( 0 ) {} 00065 template<class Y> SmartPtr( Y* y_ ) { p = dynamic_cast<T*>( y_ ); if ( p ) p->upcount(); } 00067 template<class Y> SmartPtr( const SmartPtr<Y>& y_ ) { p = dynamic_cast<T*>( y_.get() ); if ( p ) p->upcount(); } 00069 SmartPtr( const SmartPtr& t_ ) : p( t_.p ) { if ( p ) p->upcount(); } 00071 virtual ~SmartPtr(void) 00072 { 00073 if ( p ) 00074 p->downcount(); 00075 } 00076 00077 // getting access 00078 T& operator*() const { return *p; } 00079 T* operator->() const { return p; } 00080 operator T*() const { return p; } 00081 T* get() const { return p; } 00082 00084 template<class Y> SmartPtr& operator=( const SmartPtr<Y>& y_ ) 00085 { 00086 if ( this == &y_) return *this; 00087 return operator=( y_.get() ); 00088 } 00090 SmartPtr& operator=( const SmartPtr& t_ ) 00091 { 00092 if ( this == &t_) return *this; 00093 return operator=( t_.get() ); 00094 } 00096 SmartPtr& operator=( T* p_ ) 00097 { 00098 if ( p ) 00099 p->downcount(); 00100 p = p_; 00101 if ( p ) 00102 p->upcount(); 00103 return *this; 00104 } 00105 00106 // comparing 00107 int operator==( const SmartPtr& t_ ) { return p == t_.p; } 00108 int operator==( const T* p_ ) { return p == p_; } 00109 friend int operator==( const T* p_, const SmartPtr& t_ ) { return t_ == p_; } 00110 int operator!=( SmartPtr& t_ ) { return p != t_.p; } 00111 int operator!=( T* p_ ) { return p != p_; } 00112 friend int operator!=( T* p_, SmartPtr& t_ ) { return p_ != t_.p; } 00113 00115 void nullify() { if ( p ) p->downcount(); p = 0; } 00117 bool isNull() const { return p == 0; } 00118 00119 private: 00120 T* p; 00121 }; 00122 00126 #define SMART( C ) SmartPtr<C> 00127 00128 template <class T1, class T2> SMART(T1) downcast( SMART(T2)& t ) 00129 { 00130 return SMART(T1)(t.get()); 00131 } 00132 00133 #endif // __SUIT_SMARTPTR_H