1 #if !defined(__SUIT_SMARTPTR_H)
2 #define __SUIT_SMARTPTR_H
6 /*! \brief Base counter class what children using in SmartPtr class how template */
7 class SUIT_EXPORT RefCount {
10 RefCount() : crefs( 0 ) {}
12 RefCount( const RefCount& ) : crefs( 0 ) {}
14 virtual ~RefCount() {}
15 //! operator= (does not change counter)
16 RefCount& operator=( const RefCount& ) { return *this; }
18 //! increments reference counter
23 //! decrements reference counter
26 if ( crefs > 0 && --crefs == 0 )
30 //! get reference counter value
31 int refcount() const { return crefs; }
34 unsigned long crefs; //!< reference counter
37 /*! \brief Template class that provides automatic casting for hold RefCount based objects.*/
38 template <class T> class SmartPtr {
40 //! default constructor
41 SmartPtr() : p( 0 ) {}
42 //! constructor from any RefCount-based class
43 template<class Y> SmartPtr( Y* y_ ) { p = dynamic_cast<T*>( y_ ); if ( p ) p->upcount(); }
44 //! copy constructor from any RefCount-based class
45 template<class Y> SmartPtr( const SmartPtr<Y>& y_ ) { p = dynamic_cast<T*>( y_.get() ); if ( p ) p->upcount(); }
47 SmartPtr( const SmartPtr& t_ ) : p( t_.p ) { if ( p ) p->upcount(); }
49 virtual ~SmartPtr(void)
56 T& operator*() const { return *p; }//!< return *pointer
57 T* operator->() const { return p; }//!< return pointer
58 operator T*() const { return p; }//!< return pointer
59 T* get() const { return p; }//!< return pointer
62 template<class Y> SmartPtr& operator=( const SmartPtr<Y>& y_ )
64 if ( this == &y_) return *this;
65 return operator=( y_.get() );
68 SmartPtr& operator=( const SmartPtr& t_ )
70 if ( this == &t_) return *this;
71 return operator=( t_.get() );
74 SmartPtr& operator=( T* p_ )
85 int operator==( const SmartPtr& t_ ) { return p == t_.p; }//!< comparing
86 int operator==( const T* p_ ) { return p == p_; }//!< comparing
87 friend int operator==( const T* p_, const SmartPtr& t_ ) { return t_ == p_; }//!< comparing
88 int operator!=( SmartPtr& t_ ) { return p != t_.p; }//!< comparing
89 int operator!=( T* p_ ) { return p != p_; }//!< comparing
90 friend int operator!=( T* p_, SmartPtr& t_ ) { return p_ != t_.p; }//!< comparing
93 void nullify() { if ( p ) p->downcount(); p = 0; }
95 bool isNull() const { return p == 0; }
98 T* p;//!< stored pointer
102 * Define macros SMART( C ) - same as SmartPtr(C), where \a C - class object.
104 #define SMART( C ) SmartPtr<C>
105 //! casting class T2 to class T1
106 template <class T1, class T2> SMART(T1) downcast( SMART(T2)& t )
108 return SMART(T1)(t.get());
111 #endif // __SUIT_SMARTPTR_H