]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/sharedptr.h
290c610adce601592b5ced87307ad880191590db
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
7 // Copyright: Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_SHAREDPTR_H_
12 #define _WX_SHAREDPTR_H_
15 #include "wx/atomic.h"
17 // ----------------------------------------------------------------------------
18 // wxSharedPtr: A smart pointer with non-intrusive reference counting.
19 // ----------------------------------------------------------------------------
25 typedef T element_type
;
27 wxEXPLICIT
wxSharedPtr( T
* ptr
= NULL
)
31 m_ref
= new reftype(ptr
);
34 ~wxSharedPtr() { Release(); }
35 wxSharedPtr(const wxSharedPtr
& tocopy
) { Acquire(tocopy
.m_ref
); }
37 wxSharedPtr
& operator=( const wxSharedPtr
& tocopy
)
42 Acquire(tocopy
.m_ref
);
47 wxSharedPtr
& operator=( T
* ptr
)
53 m_ref
= new reftype(ptr
);
58 // test for pointer validity: defining conversion to unspecified_bool_type
59 // and not more obvious bool to avoid implicit conversions to integer types
60 typedef T
*(wxSharedPtr
<T
>::*unspecified_bool_type
)() const;
61 operator unspecified_bool_type() const
63 if (m_ref
&& m_ref
->m_ptr
)
64 return &wxSharedPtr
<T
>::get
;
71 wxASSERT(m_ref
!= NULL
);
72 wxASSERT(m_ref
->m_ptr
!= NULL
);
73 return *(m_ref
->m_ptr
);
78 wxASSERT(m_ref
!= NULL
);
79 wxASSERT(m_ref
->m_ptr
!= NULL
);
85 return m_ref
? m_ref
->m_ptr
: NULL
;
88 void reset( T
* ptr
= NULL
)
92 m_ref
= new reftype(ptr
);
95 bool unique() const { return (m_ref
? m_ref
->m_count
== 1 : true); }
96 long use_count() const { return (m_ref
? (long)m_ref
->m_count
: 0); }
102 reftype( T
* ptr
= NULL
, unsigned count
= 1 ) : m_ptr(ptr
), m_count(count
) {}
107 void Acquire(reftype
* ref
)
111 wxAtomicInc( ref
->m_count
);
118 if (!wxAtomicDec( m_ref
->m_count
))
128 template <class T
, class U
>
129 bool operator == (wxSharedPtr
<T
> const &a
, wxSharedPtr
<U
> const &b
)
131 return a
.get() == b
.get();
134 template <class T
, class U
>
135 bool operator != (wxSharedPtr
<T
> const &a
, wxSharedPtr
<U
> const &b
)
137 return a
.get() != b
.get();
140 #endif // _WX_SHAREDPTR_H_