]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/gtk/GRefPtr.h
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
;
33 enum GRefPtrAdoptType
{ GRefPtrAdopt
};
34 template <typename T
> inline T
* refGPtr(T
*);
35 template <typename T
> inline void derefGPtr(T
*);
36 template <typename T
> class GRefPtr
;
37 template <typename T
> GRefPtr
<T
> adoptGRef(T
*);
38 template <> GHashTable
* refGPtr(GHashTable
* ptr
);
39 template <> void derefGPtr(GHashTable
* ptr
);
41 template <typename T
> class GRefPtr
{
43 GRefPtr() : m_ptr(0) { }
44 GRefPtr(T
* ptr
) : m_ptr(ptr
) { if (ptr
) refGPtr(ptr
); }
45 GRefPtr(const GRefPtr
& o
) : m_ptr(o
.m_ptr
) { if (T
* ptr
= m_ptr
) refGPtr(ptr
); }
46 template <typename U
> GRefPtr(const GRefPtr
<U
>& o
) : m_ptr(o
.get()) { if (T
* ptr
= m_ptr
) refGPtr(ptr
); }
48 ~GRefPtr() { if (T
* ptr
= m_ptr
) derefGPtr(ptr
); }
57 T
* get() const { return m_ptr
; }
58 T
& operator*() const { return *m_ptr
; }
59 ALWAYS_INLINE T
* operator->() const { return m_ptr
; }
61 bool operator!() const { return !m_ptr
; }
63 // This conversion operator allows implicit conversion to bool but not to other integer types.
64 typedef T
* GRefPtr::*UnspecifiedBoolType
;
65 operator UnspecifiedBoolType() const { return m_ptr
? &GRefPtr::m_ptr
: 0; }
67 GRefPtr
& operator=(const GRefPtr
&);
68 GRefPtr
& operator=(T
*);
69 template <typename U
> GRefPtr
& operator=(const GRefPtr
<U
>&);
72 friend GRefPtr adoptGRef
<T
>(T
*);
75 static T
* hashTableDeletedValue() { return reinterpret_cast<T
*>(-1); }
76 // Adopting constructor.
77 GRefPtr(T
* ptr
, GRefPtrAdoptType
) : m_ptr(ptr
) {}
82 template <typename T
> inline GRefPtr
<T
>& GRefPtr
<T
>::operator=(const GRefPtr
<T
>& o
)
94 template <typename T
> inline GRefPtr
<T
>& GRefPtr
<T
>::operator=(T
* optr
)
105 template <class T
> inline void GRefPtr
<T
>::swap(GRefPtr
<T
>& o
)
107 std::swap(m_ptr
, o
.m_ptr
);
110 template <class T
> inline void swap(GRefPtr
<T
>& a
, GRefPtr
<T
>& b
)
115 template <typename T
, typename U
> inline bool operator==(const GRefPtr
<T
>& a
, const GRefPtr
<U
>& b
)
117 return a
.get() == b
.get();
120 template <typename T
, typename U
> inline bool operator==(const GRefPtr
<T
>& a
, U
* b
)
125 template <typename T
, typename U
> inline bool operator==(T
* a
, const GRefPtr
<U
>& b
)
130 template <typename T
, typename U
> inline bool operator!=(const GRefPtr
<T
>& a
, const GRefPtr
<U
>& b
)
132 return a
.get() != b
.get();
135 template <typename T
, typename U
> inline bool operator!=(const GRefPtr
<T
>& a
, U
* b
)
140 template <typename T
, typename U
> inline bool operator!=(T
* a
, const GRefPtr
<U
>& b
)
145 template <typename T
, typename U
> inline GRefPtr
<T
> static_pointer_cast(const GRefPtr
<U
>& p
)
147 return GRefPtr
<T
>(static_cast<T
*>(p
.get()));
150 template <typename T
, typename U
> inline GRefPtr
<T
> const_pointer_cast(const GRefPtr
<U
>& p
)
152 return GRefPtr
<T
>(const_cast<T
*>(p
.get()));
155 template <typename T
> inline T
* getPtr(const GRefPtr
<T
>& p
)
160 template <typename T
> GRefPtr
<T
> adoptGRef(T
* p
)
162 return GRefPtr
<T
>(p
, GRefPtrAdopt
);
165 template <typename T
> inline T
* refGPtr(T
* ptr
)
168 g_object_ref_sink(ptr
);
172 template <typename T
> inline void derefGPtr(T
* ptr
)
182 using WTF::derefGPtr
;
183 using WTF::adoptGRef
;
184 using WTF::static_pointer_cast
;
185 using WTF::const_pointer_cast
;
187 #endif // WTF_GRefPtr_h