]> git.saurik.com Git - wxWidgets.git/blob - include/wx/ptr_shrd.h
rename gsockmsw files to sockmsw
[wxWidgets.git] / include / wx / ptr_shrd.h
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
21 template <class T>
22 class wxSharedPtr
23 {
24 public:
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 // test for pointer validity: defining conversion to unspecified_bool_type
59 // and not more obvious bool to avoid implicit conversions to integer types
60 typedef T *(wxSharedPtr<T>::*unspecified_bool_type)() const;
61 operator unspecified_bool_type() const
62 {
63 if (m_ref && m_ref->m_ptr)
64 return &wxSharedPtr<T>::get;
65 else
66 return NULL;
67 }
68
69 T& operator*() const
70 {
71 wxASSERT(m_ref != NULL);
72 wxASSERT(m_ref->m_ptr != NULL);
73 return *(m_ref->m_ptr);
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* get() const
84 {
85 return m_ref ? m_ref->m_ptr : NULL;
86 }
87
88 void reset( T* ptr = NULL )
89 {
90 Release();
91 if (ptr)
92 m_ref = new reftype(ptr);
93 }
94
95 bool unique() const { return (m_ref ? m_ref->m_count == 1 : true); }
96 long use_count() const { return (m_ref ? (long)m_ref->m_count : 0); }
97
98 private:
99
100 struct reftype
101 {
102 reftype( T* ptr = NULL, unsigned count = 1 ) : m_ptr(ptr), m_count(count) {}
103 T* m_ptr;
104 wxAtomicInt m_count;
105 }* m_ref;
106
107 void Acquire(reftype* ref)
108 {
109 m_ref = ref;
110 if (ref)
111 wxAtomicInc( ref->m_count );
112 }
113
114 void Release()
115 {
116 if (m_ref)
117 {
118 wxAtomicDec( m_ref->m_count );
119 if (m_ref->m_count == 0)
120 {
121 delete m_ref->m_ptr;
122 delete m_ref;
123 }
124 m_ref = NULL;
125 }
126 }
127 };
128
129 template <class T, class U>
130 bool operator == (wxSharedPtr<T> const &a, wxSharedPtr<U> const &b )
131 {
132 return a.get() == b.get();
133 }
134
135 template <class T, class U>
136 bool operator != (wxSharedPtr<T> const &a, wxSharedPtr<U> const &b )
137 {
138 return a.get() != b.get();
139 }
140
141
142
143 #endif // _WX_SHARED_PTRH__