2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Collabora Ltd.
4 * Copyright (C) 2009 Martin Robinson
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.
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.
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.
26 #if ENABLE(GLIB_SUPPORT)
28 #include "AlwaysInline.h"
33 extern "C" void g_object_unref(gpointer
);
34 extern "C" gpointer
g_object_ref_sink(gpointer
);
38 enum GRefPtrAdoptType
{ GRefPtrAdopt
};
39 template <typename T
> inline T
* refGPtr(T
*);
40 template <typename T
> inline void derefGPtr(T
*);
41 template <typename T
> class GRefPtr
;
42 template <typename T
> GRefPtr
<T
> adoptGRef(T
*);
44 template <typename T
> class GRefPtr
{
46 GRefPtr() : m_ptr(0) { }
55 GRefPtr(const GRefPtr
& o
)
62 template <typename U
> GRefPtr(const GRefPtr
<U
>& o
)
83 T
* leakRef() WARN_UNUSED_RETURN
90 // Hash table deleted values, which are only constructed and never copied or destroyed.
91 GRefPtr(HashTableDeletedValueType
) : m_ptr(hashTableDeletedValue()) { }
92 bool isHashTableDeletedValue() const { return m_ptr
== hashTableDeletedValue(); }
94 T
* get() const { return m_ptr
; }
95 T
& operator*() const { return *m_ptr
; }
96 ALWAYS_INLINE T
* operator->() const { return m_ptr
; }
98 bool operator!() const { return !m_ptr
; }
100 // This conversion operator allows implicit conversion to bool but not to other integer types.
101 typedef T
* GRefPtr::*UnspecifiedBoolType
;
102 operator UnspecifiedBoolType() const { return m_ptr
? &GRefPtr::m_ptr
: 0; }
104 GRefPtr
& operator=(const GRefPtr
&);
105 GRefPtr
& operator=(T
*);
106 template <typename U
> GRefPtr
& operator=(const GRefPtr
<U
>&);
109 friend GRefPtr adoptGRef
<T
>(T
*);
112 static T
* hashTableDeletedValue() { return reinterpret_cast<T
*>(-1); }
113 // Adopting constructor.
114 GRefPtr(T
* ptr
, GRefPtrAdoptType
) : m_ptr(ptr
) {}
119 template <typename T
> inline GRefPtr
<T
>& GRefPtr
<T
>::operator=(const GRefPtr
<T
>& o
)
131 template <typename T
> inline GRefPtr
<T
>& GRefPtr
<T
>::operator=(T
* optr
)
142 template <class T
> inline void GRefPtr
<T
>::swap(GRefPtr
<T
>& o
)
144 std::swap(m_ptr
, o
.m_ptr
);
147 template <class T
> inline void swap(GRefPtr
<T
>& a
, GRefPtr
<T
>& b
)
152 template <typename T
, typename U
> inline bool operator==(const GRefPtr
<T
>& a
, const GRefPtr
<U
>& b
)
154 return a
.get() == b
.get();
157 template <typename T
, typename U
> inline bool operator==(const GRefPtr
<T
>& a
, U
* b
)
162 template <typename T
, typename U
> inline bool operator==(T
* a
, const GRefPtr
<U
>& b
)
167 template <typename T
, typename U
> inline bool operator!=(const GRefPtr
<T
>& a
, const GRefPtr
<U
>& b
)
169 return a
.get() != b
.get();
172 template <typename T
, typename U
> inline bool operator!=(const GRefPtr
<T
>& a
, U
* b
)
177 template <typename T
, typename U
> inline bool operator!=(T
* a
, const GRefPtr
<U
>& b
)
182 template <typename T
, typename U
> inline GRefPtr
<T
> static_pointer_cast(const GRefPtr
<U
>& p
)
184 return GRefPtr
<T
>(static_cast<T
*>(p
.get()));
187 template <typename T
, typename U
> inline GRefPtr
<T
> const_pointer_cast(const GRefPtr
<U
>& p
)
189 return GRefPtr
<T
>(const_cast<T
*>(p
.get()));
192 template <typename T
> inline T
* getPtr(const GRefPtr
<T
>& p
)
197 template <typename T
> GRefPtr
<T
> adoptGRef(T
* p
)
199 return GRefPtr
<T
>(p
, GRefPtrAdopt
);
202 template <> GHashTable
* refGPtr(GHashTable
* ptr
);
203 template <> void derefGPtr(GHashTable
* ptr
);
204 template <> GVariant
* refGPtr(GVariant
* ptr
);
205 template <> void derefGPtr(GVariant
* ptr
);
206 template <> GSource
* refGPtr(GSource
* ptr
);
207 template <> void derefGPtr(GSource
* ptr
);
209 template <typename T
> inline T
* refGPtr(T
* ptr
)
212 g_object_ref_sink(ptr
);
216 template <typename T
> inline void derefGPtr(T
* ptr
)
225 using WTF::adoptGRef
;
227 #endif // ENABLE(GLIB_SUPPORT)
229 #endif // WTF_GRefPtr_h