]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/gobject/GOwnPtr.h
8c7e8372f73ff790841b59a8f914d0d9a655f8b2
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.
25 #if ENABLE(GLIB_SUPPORT)
28 #include <wtf/Assertions.h>
29 #include <wtf/Noncopyable.h>
31 extern "C" void g_free(void*);
35 template <typename T
> inline void freeOwnedGPtr(T
* ptr
);
36 template<> void freeOwnedGPtr
<GError
>(GError
*);
37 template<> void freeOwnedGPtr
<GList
>(GList
*);
38 template<> void freeOwnedGPtr
<GCond
>(GCond
*);
39 template<> void freeOwnedGPtr
<GMutex
>(GMutex
*);
40 template<> void freeOwnedGPtr
<GPatternSpec
>(GPatternSpec
*);
41 template<> void freeOwnedGPtr
<GDir
>(GDir
*);
43 template <typename T
> class GOwnPtr
{
44 WTF_MAKE_NONCOPYABLE(GOwnPtr
);
46 explicit GOwnPtr(T
* ptr
= 0) : m_ptr(ptr
) { }
47 ~GOwnPtr() { freeOwnedGPtr(m_ptr
); }
49 T
* get() const { return m_ptr
; }
65 ASSERT(!ptr
|| m_ptr
!= ptr
);
89 bool operator!() const { return !m_ptr
; }
91 // This conversion operator allows implicit conversion to bool but not to other integer types.
92 typedef T
* GOwnPtr::*UnspecifiedBoolType
;
93 operator UnspecifiedBoolType() const { return m_ptr
? &GOwnPtr::m_ptr
: 0; }
95 void swap(GOwnPtr
& o
) { std::swap(m_ptr
, o
.m_ptr
); }
101 template <typename T
> inline void swap(GOwnPtr
<T
>& a
, GOwnPtr
<T
>& b
)
106 template <typename T
, typename U
> inline bool operator==(const GOwnPtr
<T
>& a
, U
* b
)
111 template <typename T
, typename U
> inline bool operator==(T
* a
, const GOwnPtr
<U
>& b
)
116 template <typename T
, typename U
> inline bool operator!=(const GOwnPtr
<T
>& a
, U
* b
)
121 template <typename T
, typename U
> inline bool operator!=(T
* a
, const GOwnPtr
<U
>& b
)
126 template <typename T
> inline typename GOwnPtr
<T
>::PtrType
getPtr(const GOwnPtr
<T
>& p
)
131 template <typename T
> inline void freeOwnedGPtr(T
* ptr
)
140 #endif // ENABLE(GLIB_SUPPORT)