]> git.saurik.com Git - wxWidgets.git/blob - include/wx/sharedptr.h
Comment-only update
[wxWidgets.git] / 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 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_SHAREDPTR_H_
11 #define _WX_SHAREDPTR_H_
12
13 #include "wx/defs.h"
14 #include "wx/atomic.h"
15
16 // ----------------------------------------------------------------------------
17 // wxSharedPtr: A smart pointer with non-intrusive reference counting.
18 // ----------------------------------------------------------------------------
19
20 template <class T>
21 class wxSharedPtr
22 {
23 public:
24 typedef T element_type;
25
26 wxEXPLICIT wxSharedPtr( T* ptr = NULL )
27 : m_ref(NULL)
28 {
29 if (ptr)
30 m_ref = new reftype(ptr);
31 }
32
33 ~wxSharedPtr() { Release(); }
34 wxSharedPtr(const wxSharedPtr& tocopy) { Acquire(tocopy.m_ref); }
35
36 wxSharedPtr& operator=( const wxSharedPtr& tocopy )
37 {
38 if (this != &tocopy)
39 {
40 Release();
41 Acquire(tocopy.m_ref);
42 }
43 return *this;
44 }
45
46 wxSharedPtr& operator=( T* ptr )
47 {
48 if (get() != ptr)
49 {
50 Release();
51 if (ptr)
52 m_ref = new reftype(ptr);
53 }
54 return *this;
55 }
56
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
61 {
62 if (m_ref && m_ref->m_ptr)
63 return &wxSharedPtr<T>::get;
64 else
65 return NULL;
66 }
67
68 T& operator*() const
69 {
70 wxASSERT(m_ref != NULL);
71 wxASSERT(m_ref->m_ptr != NULL);
72 return *(m_ref->m_ptr);
73 }
74
75 T* operator->() const
76 {
77 wxASSERT(m_ref != NULL);
78 wxASSERT(m_ref->m_ptr != NULL);
79 return m_ref->m_ptr;
80 }
81
82 T* get() const
83 {
84 return m_ref ? m_ref->m_ptr : NULL;
85 }
86
87 void reset( T* ptr = NULL )
88 {
89 Release();
90 if (ptr)
91 m_ref = new reftype(ptr);
92 }
93
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); }
96
97 private:
98
99 struct reftype
100 {
101 reftype( T* ptr = NULL, unsigned count = 1 ) : m_ptr(ptr), m_count(count) {}
102 T* m_ptr;
103 wxAtomicInt m_count;
104 }* m_ref;
105
106 void Acquire(reftype* ref)
107 {
108 m_ref = ref;
109 if (ref)
110 wxAtomicInc( ref->m_count );
111 }
112
113 void Release()
114 {
115 if (m_ref)
116 {
117 if (!wxAtomicDec( m_ref->m_count ))
118 {
119 delete m_ref->m_ptr;
120 delete m_ref;
121 }
122 m_ref = NULL;
123 }
124 }
125 };
126
127 template <class T, class U>
128 bool operator == (wxSharedPtr<T> const &a, wxSharedPtr<U> const &b )
129 {
130 return a.get() == b.get();
131 }
132
133 template <class T, class U>
134 bool operator != (wxSharedPtr<T> const &a, wxSharedPtr<U> const &b )
135 {
136 return a.get() != b.get();
137 }
138
139 #endif // _WX_SHAREDPTR_H_