]> git.saurik.com Git - wxWidgets.git/blob - include/wx/sharedptr.h
XRC: make wxSplitterWindow's sashpos and minpanesize dimensions.
[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 template<typename Deleter>
34 wxEXPLICIT wxSharedPtr(T* ptr, Deleter d)
35 : m_ref(NULL)
36 {
37 if (ptr)
38 m_ref = new reftype_with_deleter<Deleter>(ptr, d);
39 }
40
41 ~wxSharedPtr() { Release(); }
42 wxSharedPtr(const wxSharedPtr& tocopy) { Acquire(tocopy.m_ref); }
43
44 wxSharedPtr& operator=( const wxSharedPtr& tocopy )
45 {
46 if (this != &tocopy)
47 {
48 Release();
49 Acquire(tocopy.m_ref);
50 }
51 return *this;
52 }
53
54 wxSharedPtr& operator=( T* ptr )
55 {
56 if (get() != ptr)
57 {
58 Release();
59 if (ptr)
60 m_ref = new reftype(ptr);
61 }
62 return *this;
63 }
64
65 // test for pointer validity: defining conversion to unspecified_bool_type
66 // and not more obvious bool to avoid implicit conversions to integer types
67 typedef T *(wxSharedPtr<T>::*unspecified_bool_type)() const;
68 operator unspecified_bool_type() const
69 {
70 if (m_ref && m_ref->m_ptr)
71 return &wxSharedPtr<T>::get;
72 else
73 return NULL;
74 }
75
76 T& operator*() const
77 {
78 wxASSERT(m_ref != NULL);
79 wxASSERT(m_ref->m_ptr != NULL);
80 return *(m_ref->m_ptr);
81 }
82
83 T* operator->() const
84 {
85 wxASSERT(m_ref != NULL);
86 wxASSERT(m_ref->m_ptr != NULL);
87 return m_ref->m_ptr;
88 }
89
90 T* get() const
91 {
92 return m_ref ? m_ref->m_ptr : NULL;
93 }
94
95 void reset( T* ptr = NULL )
96 {
97 Release();
98 if (ptr)
99 m_ref = new reftype(ptr);
100 }
101
102 template<typename Deleter>
103 void reset(T* ptr, Deleter d)
104 {
105 Release();
106 if (ptr)
107 m_ref = new reftype_with_deleter<Deleter>(ptr, d);
108 }
109
110 bool unique() const { return (m_ref ? m_ref->m_count == 1 : true); }
111 long use_count() const { return (m_ref ? (long)m_ref->m_count : 0); }
112
113 private:
114
115 struct reftype
116 {
117 reftype(T* ptr) : m_ptr(ptr), m_count(1) {}
118 virtual ~reftype() {}
119 virtual void delete_ptr() { delete m_ptr; }
120
121 T* m_ptr;
122 wxAtomicInt m_count;
123 };
124
125 template<typename Deleter>
126 struct reftype_with_deleter : public reftype
127 {
128 reftype_with_deleter(T* ptr, Deleter d) : reftype(ptr), m_deleter(d) {}
129 virtual void delete_ptr() { m_deleter(this->m_ptr); }
130
131 Deleter m_deleter;
132 };
133
134 reftype* m_ref;
135
136 void Acquire(reftype* ref)
137 {
138 m_ref = ref;
139 if (ref)
140 wxAtomicInc( ref->m_count );
141 }
142
143 void Release()
144 {
145 if (m_ref)
146 {
147 if (!wxAtomicDec( m_ref->m_count ))
148 {
149 m_ref->delete_ptr();
150 delete m_ref;
151 }
152 m_ref = NULL;
153 }
154 }
155 };
156
157 template <class T, class U>
158 bool operator == (wxSharedPtr<T> const &a, wxSharedPtr<U> const &b )
159 {
160 return a.get() == b.get();
161 }
162
163 template <class T, class U>
164 bool operator != (wxSharedPtr<T> const &a, wxSharedPtr<U> const &b )
165 {
166 return a.get() != b.get();
167 }
168
169 #endif // _WX_SHAREDPTR_H_