]> git.saurik.com Git - wxWidgets.git/blame - include/wx/ptr_shrd.h
Last part from weak ref patch for event sink disconnection
[wxWidgets.git] / include / wx / ptr_shrd.h
CommitLineData
5200ca36
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/ptr_shrd.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// RCS-ID: $Id: object.h 47254 2007-07-09 10:09:52Z VS $
7// Copyright: Robert Roebling
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_SHARED_PTRH__
12#define _WX_SHARED_PTRH__
13
14#include "wx/defs.h"
15#include "wx/atomic.h"
16
17// ----------------------------------------------------------------------------
18// wxSharedPtr: A smart pointer with non-intrusive reference counting.
19// ----------------------------------------------------------------------------
20
21template <class T>
22class wxSharedPtr
23{
24public:
25 typedef T element_type;
26
27 wxEXPLICIT wxSharedPtr( T* ptr = NULL )
28 : m_ref(NULL)
29 {
30 if (ptr)
31 m_ref = new reftype(ptr);
32 }
33
34 ~wxSharedPtr() { Release(); }
35 wxSharedPtr(const wxSharedPtr& tocopy) { Acquire(tocopy.m_ref); }
36
37 wxSharedPtr& operator=( const wxSharedPtr& tocopy )
38 {
39 if (this != &tocopy)
40 {
41 Release();
42 Acquire(tocopy.m_ref);
43 }
44 return *this;
45 }
46
47 wxSharedPtr& operator=( T* ptr )
48 {
49 if (get() != ptr)
50 {
51 Release();
52 if (ptr)
53 m_ref = new reftype(ptr);
54 }
55 return *this;
56 }
57
58 T& operator*() const
59 {
60 wxASSERT(m_ref != NULL);
61 wxASSERT(m_ref->m_ptr != NULL);
62 return *(m_ref->m_ptr);
63 }
64
65 T* operator->() const
66 {
67 wxASSERT(m_ref != NULL);
68 wxASSERT(m_ref->m_ptr != NULL);
69 return m_ref->m_ptr;
70 }
71
72 T* get() const
73 {
74 return m_ref ? m_ref->m_ptr : NULL;
75 }
76
77 void reset( T* ptr = NULL )
78 {
79 Release();
80 if (ptr)
81 m_ref = new reftype(ptr);
82 }
83
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); }
87
88private:
89
90 struct reftype
91 {
92 reftype( T* ptr = NULL, unsigned count = 1 ) : m_ptr(ptr), m_count(count) {}
93 T* m_ptr;
94 wxAtomicInt m_count;
95 }* m_ref;
96
97 void Acquire(reftype* ref)
98 {
99 m_ref = ref;
100 if (ref)
101 wxAtomicInc( ref->m_count );
102 }
103
104 void Release()
105 {
106 if (m_ref)
107 {
108 wxAtomicDec( m_ref->m_count );
109 if (m_ref->m_count == 0)
110 {
111 delete m_ref->m_ptr;
112 delete m_ref;
113 }
114 m_ref = NULL;
115 }
116 }
117};
118
119template <class T, class U>
120bool operator == (wxSharedPtr<T> const &a, wxSharedPtr<U> const &b )
121{
122 return a.get() == b.get();
123}
124
125template <class T, class U>
126bool operator != (wxSharedPtr<T> const &a, wxSharedPtr<U> const &b )
127{
128 return a.get() != b.get();
129}
130
131
132
133#endif // _WX_SHARED_PTRH__