]>
Commit | Line | Data |
---|---|---|
5200ca36 | 1 | ///////////////////////////////////////////////////////////////////////////// |
664e1314 | 2 | // Name: wx/sharedptr.h |
5200ca36 RR |
3 | // Purpose: Shared pointer based on the counted_ptr<> template, which |
4 | // is in the public domain | |
5 | // Author: Robert Roebling, Yonat Sharon | |
5200ca36 RR |
6 | // Copyright: Robert Roebling |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
664e1314 VZ |
10 | #ifndef _WX_SHAREDPTR_H_ |
11 | #define _WX_SHAREDPTR_H_ | |
5200ca36 RR |
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 | ||
664e1314 | 20 | template <class T> |
5200ca36 RR |
21 | class wxSharedPtr |
22 | { | |
23 | public: | |
24 | typedef T element_type; | |
25 | ||
26 | wxEXPLICIT wxSharedPtr( T* ptr = NULL ) | |
664e1314 VZ |
27 | : m_ref(NULL) |
28 | { | |
29 | if (ptr) | |
30 | m_ref = new reftype(ptr); | |
5200ca36 | 31 | } |
664e1314 | 32 | |
5200ca36 RR |
33 | ~wxSharedPtr() { Release(); } |
34 | wxSharedPtr(const wxSharedPtr& tocopy) { Acquire(tocopy.m_ref); } | |
664e1314 | 35 | |
5200ca36 RR |
36 | wxSharedPtr& operator=( const wxSharedPtr& tocopy ) |
37 | { | |
664e1314 | 38 | if (this != &tocopy) |
5200ca36 RR |
39 | { |
40 | Release(); | |
41 | Acquire(tocopy.m_ref); | |
42 | } | |
43 | return *this; | |
44 | } | |
45 | ||
46 | wxSharedPtr& operator=( T* ptr ) | |
47 | { | |
664e1314 | 48 | if (get() != ptr) |
5200ca36 RR |
49 | { |
50 | Release(); | |
664e1314 VZ |
51 | if (ptr) |
52 | m_ref = new reftype(ptr); | |
5200ca36 RR |
53 | } |
54 | return *this; | |
55 | } | |
56 | ||
a60b0ddc RR |
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 | ||
5200ca36 | 68 | T& operator*() const |
664e1314 VZ |
69 | { |
70 | wxASSERT(m_ref != NULL); | |
71 | wxASSERT(m_ref->m_ptr != NULL); | |
5200ca36 RR |
72 | return *(m_ref->m_ptr); |
73 | } | |
664e1314 | 74 | |
5200ca36 | 75 | T* operator->() const |
664e1314 VZ |
76 | { |
77 | wxASSERT(m_ref != NULL); | |
78 | wxASSERT(m_ref->m_ptr != NULL); | |
5200ca36 RR |
79 | return m_ref->m_ptr; |
80 | } | |
664e1314 VZ |
81 | |
82 | T* get() const | |
83 | { | |
84 | return m_ref ? m_ref->m_ptr : NULL; | |
5200ca36 | 85 | } |
664e1314 | 86 | |
5200ca36 RR |
87 | void reset( T* ptr = NULL ) |
88 | { | |
89 | Release(); | |
664e1314 VZ |
90 | if (ptr) |
91 | m_ref = new reftype(ptr); | |
5200ca36 | 92 | } |
664e1314 | 93 | |
5200ca36 RR |
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); } | |
5200ca36 RR |
96 | |
97 | private: | |
98 | ||
664e1314 | 99 | struct reftype |
5200ca36 RR |
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 | ||
664e1314 | 106 | void Acquire(reftype* ref) |
5200ca36 RR |
107 | { |
108 | m_ref = ref; | |
664e1314 | 109 | if (ref) |
5200ca36 RR |
110 | wxAtomicInc( ref->m_count ); |
111 | } | |
112 | ||
113 | void Release() | |
114 | { | |
664e1314 | 115 | if (m_ref) |
5200ca36 | 116 | { |
43003176 | 117 | if (!wxAtomicDec( m_ref->m_count )) |
5200ca36 RR |
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 | ||
664e1314 | 139 | #endif // _WX_SHAREDPTR_H_ |