Salome HOME
0bedb2c574354598430dd1556ca1f12471b76d25
[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   operator T*()               { return  p;           }
56   T& operator*()              { return *p;           }
57   const T& operator*() const  { return *p;           }
58   T* operator->()             { return  p;           }
59   const T* operator->() const { return  p;           }
60   const T* get() const        { return  p;           }
61   T* get()                    { return  p;           }
62
63   // assignment
64   template<class Y> SmartPtr& operator=( const SmartPtr<Y>& y_ ) 
65   { 
66     if ( this == &y_) return *this;
67     return operator=( y_.get() );
68   }
69   SmartPtr& operator=( const SmartPtr& t_ ) 
70   { 
71     if ( this == &t_) return *this;
72     return operator=( t_.get() ); 
73   }
74   SmartPtr& operator=( T* p_ ) 
75   {
76     if ( p )
77       p->downcount(); 
78     p = p_; 
79     if ( p )
80       p->upcount(); 
81     return *this;
82   }
83   
84   // comparing
85   int operator==( const SmartPtr& t_ ) { return p == t_.p; }
86   int operator==( const T* p_ )           { return p == p_; }
87   friend int operator==( const T* p_, const SmartPtr& t_ ) { return t_ == p_; }
88   int operator!=( SmartPtr& t_ ) { return p != t_.p; }
89   int operator!=( T* p_ )           { return p != p_; }
90   friend int operator!=( T* p_, SmartPtr& t_ ) { return p_ != t_.p; }
91
92   // nullify
93   void nullify() { if ( p ) p->downcount(); p = 0; }
94   // check for null
95   bool isNull() const { return p == 0; }
96
97 private:
98   T* p;  // stored pointer
99 };
100
101 #define SMART( C ) SmartPtr<C>
102
103 template <class T1, class T2> SMART(T1) downcast( SMART(T2)& t ) 
104 {
105   return SMART(T1)(t.get());
106 }
107
108 #endif // __SUIT_SMARTPTR_H