]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/GOwnPtr.h
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.
27 #include <wtf/Assertions.h>
28 #include <wtf/Noncopyable.h>
31 template <typename T
> inline void freeOwnedGPtr(T
* ptr
) { g_free(reinterpret_cast<void*>(ptr
)); }
32 template<> void freeOwnedGPtr
<GError
>(GError
*);
33 template<> void freeOwnedGPtr
<GList
>(GList
*);
34 template<> void freeOwnedGPtr
<GCond
>(GCond
*);
35 template<> void freeOwnedGPtr
<GMutex
>(GMutex
*);
36 template<> void freeOwnedGPtr
<GPatternSpec
>(GPatternSpec
*);
37 template<> void freeOwnedGPtr
<GDir
>(GDir
*);
39 template <typename T
> class GOwnPtr
: Noncopyable
{
41 explicit GOwnPtr(T
* ptr
= 0) : m_ptr(ptr
) { }
42 ~GOwnPtr() { freeOwnedGPtr(m_ptr
); }
44 T
* get() const { return m_ptr
; }
45 T
* release() { T
* ptr
= m_ptr
; m_ptr
= 0; return ptr
; }
46 T
*& outPtr() { ASSERT(!m_ptr
); return m_ptr
; }
48 void set(T
* ptr
) { ASSERT(!ptr
|| m_ptr
!= ptr
); freeOwnedGPtr(m_ptr
); m_ptr
= ptr
; }
49 void clear() { freeOwnedGPtr(m_ptr
); m_ptr
= 0; }
51 T
& operator*() const { ASSERT(m_ptr
); return *m_ptr
; }
52 T
* operator->() const { ASSERT(m_ptr
); return m_ptr
; }
54 bool operator!() const { return !m_ptr
; }
56 // This conversion operator allows implicit conversion to bool but not to other integer types.
57 typedef T
* GOwnPtr::*UnspecifiedBoolType
;
58 operator UnspecifiedBoolType() const { return m_ptr
? &GOwnPtr::m_ptr
: 0; }
60 void swap(GOwnPtr
& o
) { std::swap(m_ptr
, o
.m_ptr
); }
66 template <typename T
> inline void swap(GOwnPtr
<T
>& a
, GOwnPtr
<T
>& b
) { a
.swap(b
); }
68 template <typename T
, typename U
> inline bool operator==(const GOwnPtr
<T
>& a
, U
* b
)
73 template <typename T
, typename U
> inline bool operator==(T
* a
, const GOwnPtr
<U
>& b
)
78 template <typename T
, typename U
> inline bool operator!=(const GOwnPtr
<T
>& a
, U
* b
)
83 template <typename T
, typename U
> inline bool operator!=(T
* a
, const GOwnPtr
<U
>& b
)
88 template <typename T
> inline typename GOwnPtr
<T
>::PtrType
getPtr(const GOwnPtr
<T
>& p
)