]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/ptr_shrd.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Shared pointer based on the counted_ptr<> template, which
4 // is in the public domain
5 // Author: Robert Roebling, Yonat Sharon
6 // RCS-ID: $Id: object.h 47254 2007-07-09 10:09:52Z VS $
7 // Copyright: Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_SHARED_PTRH__
12 #define _WX_SHARED_PTRH__
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
);
60 wxASSERT(m_ref
!= NULL
);
61 wxASSERT(m_ref
->m_ptr
!= NULL
);
62 return *(m_ref
->m_ptr
);
67 wxASSERT(m_ref
!= NULL
);
68 wxASSERT(m_ref
->m_ptr
!= NULL
);
74 return m_ref
? m_ref
->m_ptr
: NULL
;
77 void reset( T
* ptr
= NULL
)
81 m_ref
= new reftype(ptr
);
84 bool unique() const { return (m_ref
? m_ref
->m_count
== 1 : true); }
85 long use_count() const { return (m_ref
? (long)m_ref
->m_count
: 0); }
86 operator bool() const { return (get() != NULL
); }
92 reftype( T
* ptr
= NULL
, unsigned count
= 1 ) : m_ptr(ptr
), m_count(count
) {}
97 void Acquire(reftype
* ref
)
101 wxAtomicInc( ref
->m_count
);
108 wxAtomicDec( m_ref
->m_count
);
109 if (m_ref
->m_count
== 0)
119 template <class T
, class U
>
120 bool operator == (wxSharedPtr
<T
> const &a
, wxSharedPtr
<U
> const &b
)
122 return a
.get() == b
.get();
125 template <class T
, class U
>
126 bool operator != (wxSharedPtr
<T
> const &a
, wxSharedPtr
<U
> const &b
)
128 return a
.get() != b
.get();
133 #endif // _WX_SHARED_PTRH__