2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include <wtf/Assertions.h>
27 #include <wtf/Noncopyable.h>
29 // Forward delcarations at this point avoid the need to include GLib includes
31 typedef struct _GError GError
;
32 typedef struct _GList GList
;
33 typedef struct _GCond GCond
;
34 typedef struct _GMutex GMutex
;
35 typedef struct _GPatternSpec GPatternSpec
;
36 typedef struct _GDir GDir
;
37 typedef struct _GHashTable GHashTable
;
38 extern "C" void g_free(void*);
42 template <typename T
> inline void freeOwnedGPtr(T
* ptr
);
43 template<> void freeOwnedGPtr
<GError
>(GError
*);
44 template<> void freeOwnedGPtr
<GList
>(GList
*);
45 template<> void freeOwnedGPtr
<GCond
>(GCond
*);
46 template<> void freeOwnedGPtr
<GMutex
>(GMutex
*);
47 template<> void freeOwnedGPtr
<GPatternSpec
>(GPatternSpec
*);
48 template<> void freeOwnedGPtr
<GDir
>(GDir
*);
49 template<> void freeOwnedGPtr
<GHashTable
>(GHashTable
*);
51 template <typename T
> class GOwnPtr
: public Noncopyable
{
53 explicit GOwnPtr(T
* ptr
= 0) : m_ptr(ptr
) { }
54 ~GOwnPtr() { freeOwnedGPtr(m_ptr
); }
56 T
* get() const { return m_ptr
; }
72 ASSERT(!ptr
|| m_ptr
!= ptr
);
95 bool operator!() const { return !m_ptr
; }
97 // This conversion operator allows implicit conversion to bool but not to other integer types.
98 typedef T
* GOwnPtr::*UnspecifiedBoolType
;
99 operator UnspecifiedBoolType() const { return m_ptr
? &GOwnPtr::m_ptr
: 0; }
101 void swap(GOwnPtr
& o
) { std::swap(m_ptr
, o
.m_ptr
); }
107 template <typename T
> inline void swap(GOwnPtr
<T
>& a
, GOwnPtr
<T
>& b
)
112 template <typename T
, typename U
> inline bool operator==(const GOwnPtr
<T
>& a
, U
* b
)
117 template <typename T
, typename U
> inline bool operator==(T
* a
, const GOwnPtr
<U
>& b
)
122 template <typename T
, typename U
> inline bool operator!=(const GOwnPtr
<T
>& a
, U
* b
)
127 template <typename T
, typename U
> inline bool operator!=(T
* a
, const GOwnPtr
<U
>& b
)
132 template <typename T
> inline typename GOwnPtr
<T
>::PtrType
getPtr(const GOwnPtr
<T
>& p
)
137 template <typename T
> inline void freeOwnedGPtr(T
* ptr
)