1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #if !defined(__SUIT_SMARTPTR_H)
20 #define __SUIT_SMARTPTR_H
24 /*! \brief Base counter class what children using in SmartPtr class how template */
25 class SUIT_EXPORT RefCount {
28 RefCount() : crefs( 0 ) {}
30 RefCount( const RefCount& ) : crefs( 0 ) {}
32 virtual ~RefCount() {}
33 //! operator= (does not change counter)
34 RefCount& operator=( const RefCount& ) { return *this; }
36 //! increments reference counter
41 //! decrements reference counter
44 if ( crefs > 0 && --crefs == 0 )
48 //! get reference counter value
49 int refcount() const { return crefs; }
52 unsigned long crefs; //!< reference counter
55 /*! \brief Template class that provides automatic casting for hold RefCount based objects.*/
56 template <class T> class SmartPtr {
58 //! default constructor
59 SmartPtr() : p( 0 ) {}
60 //! constructor from any RefCount-based class
61 template<class Y> SmartPtr( Y* y_ ) { p = dynamic_cast<T*>( y_ ); if ( p ) p->upcount(); }
62 //! copy constructor from any RefCount-based class
63 template<class Y> SmartPtr( const SmartPtr<Y>& y_ ) { p = dynamic_cast<T*>( y_.get() ); if ( p ) p->upcount(); }
65 SmartPtr( const SmartPtr& t_ ) : p( t_.p ) { if ( p ) p->upcount(); }
67 virtual ~SmartPtr(void)
74 T& operator*() const { return *p; }//!< return *pointer
75 T* operator->() const { return p; }//!< return pointer
76 operator T*() const { return p; }//!< return pointer
77 T* get() const { return p; }//!< return pointer
80 template<class Y> SmartPtr& operator=( const SmartPtr<Y>& y_ )
82 if ( this == &y_) return *this;
83 return operator=( y_.get() );
86 SmartPtr& operator=( const SmartPtr& t_ )
88 if ( this == &t_) return *this;
89 return operator=( t_.get() );
92 SmartPtr& operator=( T* p_ )
103 int operator==( const SmartPtr& t_ ) { return p == t_.p; }//!< comparing
104 int operator==( const T* p_ ) { return p == p_; }//!< comparing
105 friend int operator==( const T* p_, const SmartPtr& t_ ) { return t_ == p_; }//!< comparing
106 int operator!=( SmartPtr& t_ ) { return p != t_.p; }//!< comparing
107 int operator!=( T* p_ ) { return p != p_; }//!< comparing
108 friend int operator!=( T* p_, SmartPtr& t_ ) { return p_ != t_.p; }//!< comparing
111 void nullify() { if ( p ) p->downcount(); p = 0; }
113 bool isNull() const { return p == 0; }
116 T* p;//!< stored pointer
120 * Define macros SMART( C ) - same as SmartPtr(C), where \a C - class object.
122 #define SMART( C ) SmartPtr<C>
123 //! casting class T2 to class T1
124 template <class T1, class T2> SMART(T1) downcast( SMART(T2)& t )
126 return SMART(T1)(t.get());
129 #endif // __SUIT_SMARTPTR_H