]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/gobject/GRefPtr.h
3a33605c29746675af5083902c062513bc44285b
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 #include "AlwaysInline.h"
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
);
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
);
44 template <typename T
> class GRefPtr
{
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
); }
51 ~GRefPtr() { if (T
* ptr
= m_ptr
) derefGPtr(ptr
); }
60 T
* get() const { return m_ptr
; }
61 T
& operator*() const { return *m_ptr
; }
62 ALWAYS_INLINE T
* operator->() const { return m_ptr
; }
64 bool operator!() const { return !m_ptr
; }
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; }
70 GRefPtr
& operator=(const GRefPtr
&);
71 GRefPtr
& operator=(T
*);
72 template <typename U
> GRefPtr
& operator=(const GRefPtr
<U
>&);
75 friend GRefPtr adoptGRef
<T
>(T
*);
78 static T
* hashTableDeletedValue() { return reinterpret_cast<T
*>(-1); }
79 // Adopting constructor.
80 GRefPtr(T
* ptr
, GRefPtrAdoptType
) : m_ptr(ptr
) {}
85 template <typename T
> inline GRefPtr
<T
>& GRefPtr
<T
>::operator=(const GRefPtr
<T
>& o
)
97 template <typename T
> inline GRefPtr
<T
>& GRefPtr
<T
>::operator=(T
* optr
)
108 template <class T
> inline void GRefPtr
<T
>::swap(GRefPtr
<T
>& o
)
110 std::swap(m_ptr
, o
.m_ptr
);
113 template <class T
> inline void swap(GRefPtr
<T
>& a
, GRefPtr
<T
>& b
)
118 template <typename T
, typename U
> inline bool operator==(const GRefPtr
<T
>& a
, const GRefPtr
<U
>& b
)
120 return a
.get() == b
.get();
123 template <typename T
, typename U
> inline bool operator==(const GRefPtr
<T
>& a
, U
* b
)
128 template <typename T
, typename U
> inline bool operator==(T
* a
, const GRefPtr
<U
>& b
)
133 template <typename T
, typename U
> inline bool operator!=(const GRefPtr
<T
>& a
, const GRefPtr
<U
>& b
)
135 return a
.get() != b
.get();
138 template <typename T
, typename U
> inline bool operator!=(const GRefPtr
<T
>& a
, U
* b
)
143 template <typename T
, typename U
> inline bool operator!=(T
* a
, const GRefPtr
<U
>& b
)
148 template <typename T
, typename U
> inline GRefPtr
<T
> static_pointer_cast(const GRefPtr
<U
>& p
)
150 return GRefPtr
<T
>(static_cast<T
*>(p
.get()));
153 template <typename T
, typename U
> inline GRefPtr
<T
> const_pointer_cast(const GRefPtr
<U
>& p
)
155 return GRefPtr
<T
>(const_cast<T
*>(p
.get()));
158 template <typename T
> inline T
* getPtr(const GRefPtr
<T
>& p
)
163 template <typename T
> GRefPtr
<T
> adoptGRef(T
* p
)
165 return GRefPtr
<T
>(p
, GRefPtrAdopt
);
168 template <typename T
> inline T
* refGPtr(T
* ptr
)
171 g_object_ref_sink(ptr
);
175 template <typename T
> inline void derefGPtr(T
* ptr
)
185 using WTF::derefGPtr
;
186 using WTF::adoptGRef
;
187 using WTF::static_pointer_cast
;
188 using WTF::const_pointer_cast
;
190 #endif // WTF_GRefPtr_h