Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / SUIT / SUIT_SmartPtr.h
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #if !defined(__SUIT_SMARTPTR_H)
20 #define __SUIT_SMARTPTR_H
21
22 #include "SUIT.h"
23
24 /*! \brief Base counter class what children using in SmartPtr class how template */
25 class SUIT_EXPORT RefCount {
26 public:
27   //! constructor
28   RefCount() : crefs( 0 ) {}
29   //! copy constructor 
30   RefCount( const RefCount& ) : crefs( 0 ) {}
31   //! destructor
32   virtual ~RefCount() {}
33   //! operator= (does not change counter)
34   RefCount& operator=( const RefCount& ) { return *this; }
35
36   //! increments reference counter
37   void upcount() { 
38     ++crefs; 
39   }
40
41   //! decrements reference counter
42   void downcount()
43   {
44     if ( crefs > 0 && --crefs == 0 )
45       delete this;
46   }
47   
48   //! get reference counter value
49   int refcount() const { return crefs; }
50
51 private:
52   unsigned long crefs;   //!< reference counter
53 };
54
55 /*! \brief Template class that provides automatic casting for hold RefCount based objects.*/
56 template <class T> class SmartPtr {
57 public:
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(); }
64   //! copy constructor
65   SmartPtr( const SmartPtr& t_ ) : p( t_.p ) { if ( p ) p->upcount();   }
66   //! destructor
67   virtual ~SmartPtr(void)     
68   { 
69     if ( p )
70       p->downcount(); 
71   }
72
73   // getting access
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
78
79   //! assignment
80   template<class Y> SmartPtr& operator=( const SmartPtr<Y>& y_ ) 
81   { 
82     if ( this == &y_) return *this;
83     return operator=( y_.get() );
84   }
85   //! assignment
86   SmartPtr& operator=( const SmartPtr& t_ )
87   { 
88     if ( this == &t_) return *this;
89     return operator=( t_.get() ); 
90   }
91   //!< assignment
92   SmartPtr& operator=( T* p_ )
93   {
94     if ( p )
95       p->downcount(); 
96     p = p_; 
97     if ( p )
98       p->upcount(); 
99     return *this;
100   }
101   
102   // comparing
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
109
110   //! nullify
111   void nullify() { if ( p ) p->downcount(); p = 0; }
112   //! check for null
113   bool isNull() const { return p == 0; }
114
115 private:
116   T* p;//!< stored pointer
117 };
118
119 /*! \def SMART( C )
120  * Define macros SMART( C ) - same as SmartPtr(C), where \a C - class object.
121  */
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 ) 
125 {
126   return SMART(T1)(t.get());
127 }
128
129 #endif // __SUIT_SMARTPTR_H