]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/sharedptr.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/sharedptr.h
3 // Purpose: Shared pointer based on the counted_ptr<> template, which
4 // is in the public domain
5 // Author: Robert Roebling, Yonat Sharon
6 // Copyright: Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_SHAREDPTR_H_
11 #define _WX_SHAREDPTR_H_
14 #include "wx/atomic.h"
16 // ----------------------------------------------------------------------------
17 // wxSharedPtr: A smart pointer with non-intrusive reference counting.
18 // ----------------------------------------------------------------------------
24 typedef T element_type
;
26 wxEXPLICIT
wxSharedPtr( T
* ptr
= NULL
)
30 m_ref
= new reftype(ptr
);
33 ~wxSharedPtr() { Release(); }
34 wxSharedPtr(const wxSharedPtr
& tocopy
) { Acquire(tocopy
.m_ref
); }
36 wxSharedPtr
& operator=( const wxSharedPtr
& tocopy
)
41 Acquire(tocopy
.m_ref
);
46 wxSharedPtr
& operator=( T
* ptr
)
52 m_ref
= new reftype(ptr
);
57 // test for pointer validity: defining conversion to unspecified_bool_type
58 // and not more obvious bool to avoid implicit conversions to integer types
59 typedef T
*(wxSharedPtr
<T
>::*unspecified_bool_type
)() const;
60 operator unspecified_bool_type() const
62 if (m_ref
&& m_ref
->m_ptr
)
63 return &wxSharedPtr
<T
>::get
;
70 wxASSERT(m_ref
!= NULL
);
71 wxASSERT(m_ref
->m_ptr
!= NULL
);
72 return *(m_ref
->m_ptr
);
77 wxASSERT(m_ref
!= NULL
);
78 wxASSERT(m_ref
->m_ptr
!= NULL
);
84 return m_ref
? m_ref
->m_ptr
: NULL
;
87 void reset( T
* ptr
= NULL
)
91 m_ref
= new reftype(ptr
);
94 bool unique() const { return (m_ref
? m_ref
->m_count
== 1 : true); }
95 long use_count() const { return (m_ref
? (long)m_ref
->m_count
: 0); }
101 reftype( T
* ptr
= NULL
, unsigned count
= 1 ) : m_ptr(ptr
), m_count(count
) {}
106 void Acquire(reftype
* ref
)
110 wxAtomicInc( ref
->m_count
);
117 if (!wxAtomicDec( m_ref
->m_count
))
127 template <class T
, class U
>
128 bool operator == (wxSharedPtr
<T
> const &a
, wxSharedPtr
<U
> const &b
)
130 return a
.get() == b
.get();
133 template <class T
, class U
>
134 bool operator != (wxSharedPtr
<T
> const &a
, wxSharedPtr
<U
> const &b
)
136 return a
.get() != b
.get();
139 #endif // _WX_SHAREDPTR_H_