]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/gobject/GRefPtr.h
JavaScriptCore-721.26.tar.gz
[apple/javascriptcore.git] / wtf / gobject / GRefPtr.h
CommitLineData
f9bf01c6
A
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
29typedef struct _GHashTable GHashTable;
4e4e5a6f
A
30typedef void* gpointer;
31extern "C" void g_object_unref(gpointer object);
32extern "C" gpointer g_object_ref_sink(gpointer object);
f9bf01c6
A
33
34namespace WTF {
35
36enum GRefPtrAdoptType { GRefPtrAdopt };
37template <typename T> inline T* refGPtr(T*);
38template <typename T> inline void derefGPtr(T*);
39template <typename T> class GRefPtr;
40template <typename T> GRefPtr<T> adoptGRef(T*);
41template <> GHashTable* refGPtr(GHashTable* ptr);
42template <> void derefGPtr(GHashTable* ptr);
43
44template <typename T> class GRefPtr {
45public:
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
77private:
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
85template <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
97template <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
108template <class T> inline void GRefPtr<T>::swap(GRefPtr<T>& o)
109{
110 std::swap(m_ptr, o.m_ptr);
111}
112
113template <class T> inline void swap(GRefPtr<T>& a, GRefPtr<T>& b)
114{
115 a.swap(b);
116}
117
118template <typename T, typename U> inline bool operator==(const GRefPtr<T>& a, const GRefPtr<U>& b)
119{
120 return a.get() == b.get();
121}
122
123template <typename T, typename U> inline bool operator==(const GRefPtr<T>& a, U* b)
124{
125 return a.get() == b;
126}
127
128template <typename T, typename U> inline bool operator==(T* a, const GRefPtr<U>& b)
129{
130 return a == b.get();
131}
132
133template <typename T, typename U> inline bool operator!=(const GRefPtr<T>& a, const GRefPtr<U>& b)
134{
135 return a.get() != b.get();
136}
137
138template <typename T, typename U> inline bool operator!=(const GRefPtr<T>& a, U* b)
139{
140 return a.get() != b;
141}
142
143template <typename T, typename U> inline bool operator!=(T* a, const GRefPtr<U>& b)
144{
145 return a != b.get();
146}
147
148template <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
153template <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
158template <typename T> inline T* getPtr(const GRefPtr<T>& p)
159{
160 return p.get();
161}
162
163template <typename T> GRefPtr<T> adoptGRef(T* p)
164{
165 return GRefPtr<T>(p, GRefPtrAdopt);
166}
167
168template <typename T> inline T* refGPtr(T* ptr)
169{
170 if (ptr)
171 g_object_ref_sink(ptr);
172 return ptr;
173}
174
175template <typename T> inline void derefGPtr(T* ptr)
176{
177 if (ptr)
178 g_object_unref(ptr);
179}
180
181} // namespace WTF
182
183using WTF::GRefPtr;
184using WTF::refGPtr;
185using WTF::derefGPtr;
186using WTF::adoptGRef;
187using WTF::static_pointer_cast;
188using WTF::const_pointer_cast;
189
190#endif // WTF_GRefPtr_h