Reshuffling directories to separate include and sources, part one.
[fail.git] / include / core / weakpointer.h
blob79f598153ffcc1b1a19995a0ca593a7c12250055
1 #ifndef AWFUL_WEAKPOINTER_H_
2 #define AWFUL_WEAKPOINTER_H_
4 // TODO: make this shit work
6 namespace awful
8 template< class C > class Pointer;
9 template< class C > class ConstPointer;
11 template< class C > class weakptrproxy
13 public:
14 weakptrproxy( const C* pObj_ ) :
15 m_WeakRefCount( 1 ),
16 m_pObj( const_cast< C* >( pObj_ ) )
20 ~weakptrproxy()
22 if( m_pObj )
23 m_pObj->m_pWeakPtrProxy = 0;
26 private:
27 unsigned long m_WeakRefCount;
28 C* m_pObj;
31 template< class C, typename P > class weakptr
33 template< class C_, class P_ > friend class ptr;
35 public:
36 weakptr() :
37 m_pProxy( 0 )
41 weakptr& operator=( const weakptr& wptr_ )
43 if( m_pProxy == wptr_.m_pProxy )
44 return *this;
46 if( m_pProxy )
47 m_pProxy->remRef();
49 m_pProxy = wptr_.m_pProxy;
50 m_pProxy->addRef();
53 protected:
54 weakptr( P pObj_ )
56 if( !pObj_ )
58 m_pProxy = 0;
59 return;
62 setObj( pObj_ );
65 weakptr& operator=( P pObj_ )
67 if( m_pProxy == pObj_.m_pProxy )
68 return *this;
70 if( m_pProxy )
71 m_pProxy->remRef();
73 setObj( pObj_ );
76 private:
77 void incRef() const
79 if( m_pProxy )
80 ++m_pProxy->m_WeakRefCount;
83 void decRef()
85 if( !m_pProxy )
86 return;
88 if( !( --m_pProxy->m_WeakRefCount ) )
90 delete m_pProxy;
91 m_pProxy = 0;
95 void setObj( P pObj_ )
97 if( pObj_->m_pWeakPtrProxy )
99 m_pProxy = pObj_->m_pWeakPtrProxy;
100 incRef();
102 else
104 m_pProxy = pObj_->m_pWeakPtrProxy = new weakptrproxy< C >( pObj_ );
108 operator P() const
110 if( m_pProxy && !m_pProxy->m_pObj )
112 decRef();
113 m_pProxy = 0;
116 if( !m_pProxy )
117 return 0;
119 return m_pProxy->m_pObj;
122 mutable weakptrproxy< C >* m_pProxy;
125 template< class C > class WeakPointer : public weakptr< C, C* >
127 public:
128 WeakPointer() :
129 weakptr< C, C* >()
133 WeakPointer( const WeakPointer& Ptr_ ) :
134 weakptr< C, C* >( Ptr_ )
138 WeakPointer( const Pointer< C >& Ptr_ ) :
139 weakptr< C, C* >( Ptr_ )
144 template< class C > class WeakConstPointer : public weakptr< C, const C* >
146 public:
147 WeakConstPointer() :
148 weakptr< C, const C* >()
152 WeakConstPointer( const WeakConstPointer& Ptr_ ) :
153 weakptr< C, const C* >( Ptr_ )
157 WeakConstPointer( const WeakPointer< C >& Ptr_ ) :
158 weakptr< C, const C* >( Ptr_ )
162 WeakConstPointer( const ConstPointer< C >& Ptr_ ) :
163 weakptr< C, const C* >( Ptr_ )
167 WeakConstPointer( const Pointer< C >& Ptr_ ) :
168 weakptr< C, const C* >( Ptr_ )
174 #endif