Salome HOME
Improve methods, that returns T*
[modules/gui.git] / src / SUIT / SUIT_SmartPtr.h
1 #if !defined(__SUIT_SMARTPTR_H)
2 #define __SUIT_SMARTPTR_H
3
4 #include "SUIT.h"
5
6 class SUIT_EXPORT RefCount {
7 public:
8   // constructor
9   RefCount() : crefs( 0 ) {}
10   // copy constructor 
11   RefCount( const RefCount& ) : crefs( 0 ) {}
12   // destructor
13   virtual ~RefCount() {}
14   // operator= (does not change counter)
15   RefCount& operator=( const RefCount& ) { return *this; }
16
17   // increments reference counter
18   void upcount() { 
19     ++crefs; 
20   }
21
22   // decrements reference counter
23   void downcount()
24   {
25     if ( crefs > 0 && --crefs == 0 )
26       delete this;
27   }
28   
29   // get reference counter value
30   int refcount() const { return crefs; }
31
32 private:
33   unsigned long crefs;   // reference counter
34 };
35
36 template <class T> class SmartPtr {
37 public:
38   // default constructor
39   SmartPtr() : p( 0 ) {}
40   // constructor from any RefCount-based class
41   template<class Y> SmartPtr( Y* y_ ) { p = dynamic_cast<T*>( y_ ); if ( p ) p->upcount(); }
42   // copy constructor from any RefCount-based class
43   template<class Y> SmartPtr( const SmartPtr<Y>& y_ ) { p = dynamic_cast<T*>( y_.get() ); if ( p ) p->upcount(); }
44   // copy constructor
45   SmartPtr( const SmartPtr& t_ ) : p( t_.p ) { if ( p ) p->upcount();   }
46   // destructor
47   virtual ~SmartPtr(void)     
48   { 
49     if ( p )
50       p->downcount(); 
51   }
52
53   // getting access 
54   //operator RefCount*()      { return (RefCount*)p; }
55   T& operator*() const        { return *p;           }
56   T* operator->() const       { return  p;           }
57   operator T*() const         { return  p;           }
58   T* get() const              { return  p;           }
59
60   // assignment
61   template<class Y> SmartPtr& operator=( const SmartPtr<Y>& y_ ) 
62   { 
63     if ( this == &y_) return *this;
64     return operator=( y_.get() );
65   }
66   SmartPtr& operator=( const SmartPtr& t_ ) 
67   { 
68     if ( this == &t_) return *this;
69     return operator=( t_.get() ); 
70   }
71   SmartPtr& operator=( T* p_ ) 
72   {
73     if ( p )
74       p->downcount(); 
75     p = p_; 
76     if ( p )
77       p->upcount(); 
78     return *this;
79   }
80   
81   // comparing
82   int operator==( const SmartPtr& t_ ) { return p == t_.p; }
83   int operator==( const T* p_ )           { return p == p_; }
84   friend int operator==( const T* p_, const SmartPtr& t_ ) { return t_ == p_; }
85   int operator!=( SmartPtr& t_ ) { return p != t_.p; }
86   int operator!=( T* p_ )           { return p != p_; }
87   friend int operator!=( T* p_, SmartPtr& t_ ) { return p_ != t_.p; }
88
89   // nullify
90   void nullify() { if ( p ) p->downcount(); p = 0; }
91   // check for null
92   bool isNull() const { return p == 0; }
93
94 private:
95   T* p;  // stored pointer
96 };
97
98 #define SMART( C ) SmartPtr<C>
99
100 template <class T1, class T2> SMART(T1) downcast( SMART(T2)& t ) 
101 {
102   return SMART(T1)(t.get());
103 }
104
105 #endif // __SUIT_SMARTPTR_H