]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. | |
3 | * Copyright (C) 2008 Collabora Ltd. | |
4 | * Copyright (C) 2009 Martin Robinson | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Library General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Library General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Library General Public License | |
17 | * along with this library; see the file COPYING.LIB. If not, write to | |
18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
19 | * Boston, MA 02110-1301, USA. | |
20 | * | |
21 | */ | |
22 | ||
23 | #ifndef WTF_GRefPtr_h | |
24 | #define WTF_GRefPtr_h | |
25 | ||
26 | #include "AlwaysInline.h" | |
27 | #include <algorithm> | |
28 | ||
29 | typedef struct _GHashTable GHashTable; | |
30 | typedef void* gpointer; | |
31 | extern "C" void g_object_unref(gpointer object); | |
32 | extern "C" gpointer g_object_ref_sink(gpointer object); | |
33 | ||
34 | namespace WTF { | |
35 | ||
36 | enum GRefPtrAdoptType { GRefPtrAdopt }; | |
37 | template <typename T> inline T* refGPtr(T*); | |
38 | template <typename T> inline void derefGPtr(T*); | |
39 | template <typename T> class GRefPtr; | |
40 | template <typename T> GRefPtr<T> adoptGRef(T*); | |
41 | template <> GHashTable* refGPtr(GHashTable* ptr); | |
42 | template <> void derefGPtr(GHashTable* ptr); | |
43 | ||
44 | template <typename T> class GRefPtr { | |
45 | public: | |
46 | GRefPtr() : m_ptr(0) { } | |
47 | GRefPtr(T* ptr) : m_ptr(ptr) { if (ptr) refGPtr(ptr); } | |
48 | GRefPtr(const GRefPtr& o) : m_ptr(o.m_ptr) { if (T* ptr = m_ptr) refGPtr(ptr); } | |
49 | template <typename U> GRefPtr(const GRefPtr<U>& o) : m_ptr(o.get()) { if (T* ptr = m_ptr) refGPtr(ptr); } | |
50 | ||
51 | ~GRefPtr() { if (T* ptr = m_ptr) derefGPtr(ptr); } | |
52 | ||
53 | void clear() | |
54 | { | |
55 | if (T* ptr = m_ptr) | |
56 | derefGPtr(ptr); | |
57 | m_ptr = 0; | |
58 | } | |
59 | ||
60 | T* get() const { return m_ptr; } | |
61 | T& operator*() const { return *m_ptr; } | |
62 | ALWAYS_INLINE T* operator->() const { return m_ptr; } | |
63 | ||
64 | bool operator!() const { return !m_ptr; } | |
65 | ||
66 | // This conversion operator allows implicit conversion to bool but not to other integer types. | |
67 | typedef T* GRefPtr::*UnspecifiedBoolType; | |
68 | operator UnspecifiedBoolType() const { return m_ptr ? &GRefPtr::m_ptr : 0; } | |
69 | ||
70 | GRefPtr& operator=(const GRefPtr&); | |
71 | GRefPtr& operator=(T*); | |
72 | template <typename U> GRefPtr& operator=(const GRefPtr<U>&); | |
73 | ||
74 | void swap(GRefPtr&); | |
75 | friend GRefPtr adoptGRef<T>(T*); | |
76 | ||
77 | private: | |
78 | static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } | |
79 | // Adopting constructor. | |
80 | GRefPtr(T* ptr, GRefPtrAdoptType) : m_ptr(ptr) {} | |
81 | ||
82 | T* m_ptr; | |
83 | }; | |
84 | ||
85 | template <typename T> inline GRefPtr<T>& GRefPtr<T>::operator=(const GRefPtr<T>& o) | |
86 | { | |
87 | T* optr = o.get(); | |
88 | if (optr) | |
89 | refGPtr(optr); | |
90 | T* ptr = m_ptr; | |
91 | m_ptr = optr; | |
92 | if (ptr) | |
93 | derefGPtr(ptr); | |
94 | return *this; | |
95 | } | |
96 | ||
97 | template <typename T> inline GRefPtr<T>& GRefPtr<T>::operator=(T* optr) | |
98 | { | |
99 | T* ptr = m_ptr; | |
100 | if (optr) | |
101 | refGPtr(optr); | |
102 | m_ptr = optr; | |
103 | if (ptr) | |
104 | derefGPtr(ptr); | |
105 | return *this; | |
106 | } | |
107 | ||
108 | template <class T> inline void GRefPtr<T>::swap(GRefPtr<T>& o) | |
109 | { | |
110 | std::swap(m_ptr, o.m_ptr); | |
111 | } | |
112 | ||
113 | template <class T> inline void swap(GRefPtr<T>& a, GRefPtr<T>& b) | |
114 | { | |
115 | a.swap(b); | |
116 | } | |
117 | ||
118 | template <typename T, typename U> inline bool operator==(const GRefPtr<T>& a, const GRefPtr<U>& b) | |
119 | { | |
120 | return a.get() == b.get(); | |
121 | } | |
122 | ||
123 | template <typename T, typename U> inline bool operator==(const GRefPtr<T>& a, U* b) | |
124 | { | |
125 | return a.get() == b; | |
126 | } | |
127 | ||
128 | template <typename T, typename U> inline bool operator==(T* a, const GRefPtr<U>& b) | |
129 | { | |
130 | return a == b.get(); | |
131 | } | |
132 | ||
133 | template <typename T, typename U> inline bool operator!=(const GRefPtr<T>& a, const GRefPtr<U>& b) | |
134 | { | |
135 | return a.get() != b.get(); | |
136 | } | |
137 | ||
138 | template <typename T, typename U> inline bool operator!=(const GRefPtr<T>& a, U* b) | |
139 | { | |
140 | return a.get() != b; | |
141 | } | |
142 | ||
143 | template <typename T, typename U> inline bool operator!=(T* a, const GRefPtr<U>& b) | |
144 | { | |
145 | return a != b.get(); | |
146 | } | |
147 | ||
148 | template <typename T, typename U> inline GRefPtr<T> static_pointer_cast(const GRefPtr<U>& p) | |
149 | { | |
150 | return GRefPtr<T>(static_cast<T*>(p.get())); | |
151 | } | |
152 | ||
153 | template <typename T, typename U> inline GRefPtr<T> const_pointer_cast(const GRefPtr<U>& p) | |
154 | { | |
155 | return GRefPtr<T>(const_cast<T*>(p.get())); | |
156 | } | |
157 | ||
158 | template <typename T> inline T* getPtr(const GRefPtr<T>& p) | |
159 | { | |
160 | return p.get(); | |
161 | } | |
162 | ||
163 | template <typename T> GRefPtr<T> adoptGRef(T* p) | |
164 | { | |
165 | return GRefPtr<T>(p, GRefPtrAdopt); | |
166 | } | |
167 | ||
168 | template <typename T> inline T* refGPtr(T* ptr) | |
169 | { | |
170 | if (ptr) | |
171 | g_object_ref_sink(ptr); | |
172 | return ptr; | |
173 | } | |
174 | ||
175 | template <typename T> inline void derefGPtr(T* ptr) | |
176 | { | |
177 | if (ptr) | |
178 | g_object_unref(ptr); | |
179 | } | |
180 | ||
181 | } // namespace WTF | |
182 | ||
183 | using WTF::GRefPtr; | |
184 | using WTF::refGPtr; | |
185 | using WTF::derefGPtr; | |
186 | using WTF::adoptGRef; | |
187 | using WTF::static_pointer_cast; | |
188 | using WTF::const_pointer_cast; | |
189 | ||
190 | #endif // WTF_GRefPtr_h |