]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/gobject/GOwnPtr.h
JavaScriptCore-903.5.tar.gz
[apple/javascriptcore.git] / wtf / gobject / GOwnPtr.h
CommitLineData
f9bf01c6
A
1/*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Collabora Ltd.
4 *
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.
9 *
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.
14 *
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.
19 *
20 */
21
22#ifndef GOwnPtr_h
23#define GOwnPtr_h
24
14957cd0
A
25#if ENABLE(GLIB_SUPPORT)
26
f9bf01c6
A
27#include <algorithm>
28#include <wtf/Assertions.h>
29#include <wtf/Noncopyable.h>
30
f9bf01c6
A
31extern "C" void g_free(void*);
32
33namespace WTF {
34
35template <typename T> inline void freeOwnedGPtr(T* ptr);
36template<> void freeOwnedGPtr<GError>(GError*);
37template<> void freeOwnedGPtr<GList>(GList*);
38template<> void freeOwnedGPtr<GCond>(GCond*);
39template<> void freeOwnedGPtr<GMutex>(GMutex*);
40template<> void freeOwnedGPtr<GPatternSpec>(GPatternSpec*);
41template<> void freeOwnedGPtr<GDir>(GDir*);
f9bf01c6 42
14957cd0
A
43template <typename T> class GOwnPtr {
44 WTF_MAKE_NONCOPYABLE(GOwnPtr);
f9bf01c6
A
45public:
46 explicit GOwnPtr(T* ptr = 0) : m_ptr(ptr) { }
47 ~GOwnPtr() { freeOwnedGPtr(m_ptr); }
48
49 T* get() const { return m_ptr; }
50 T* release()
51 {
52 T* ptr = m_ptr;
53 m_ptr = 0;
54 return ptr;
55 }
56
57 T*& outPtr()
58 {
59 ASSERT(!m_ptr);
60 return m_ptr;
61 }
62
63 void set(T* ptr)
64 {
65 ASSERT(!ptr || m_ptr != ptr);
66 freeOwnedGPtr(m_ptr);
67 m_ptr = ptr;
68 }
69
70 void clear()
71 {
14957cd0 72 T* ptr = m_ptr;
f9bf01c6 73 m_ptr = 0;
14957cd0 74 freeOwnedGPtr(ptr);
f9bf01c6
A
75 }
76
77 T& operator*() const
78 {
79 ASSERT(m_ptr);
80 return *m_ptr;
81 }
82
83 T* operator->() const
84 {
85 ASSERT(m_ptr);
86 return m_ptr;
87 }
88
89 bool operator!() const { return !m_ptr; }
90
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; }
94
95 void swap(GOwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
96
97private:
98 T* m_ptr;
99};
100
101template <typename T> inline void swap(GOwnPtr<T>& a, GOwnPtr<T>& b)
102{
103 a.swap(b);
104}
105
106template <typename T, typename U> inline bool operator==(const GOwnPtr<T>& a, U* b)
107{
108 return a.get() == b;
109}
110
111template <typename T, typename U> inline bool operator==(T* a, const GOwnPtr<U>& b)
112{
113 return a == b.get();
114}
115
116template <typename T, typename U> inline bool operator!=(const GOwnPtr<T>& a, U* b)
117{
118 return a.get() != b;
119}
120
121template <typename T, typename U> inline bool operator!=(T* a, const GOwnPtr<U>& b)
122{
123 return a != b.get();
124}
125
126template <typename T> inline typename GOwnPtr<T>::PtrType getPtr(const GOwnPtr<T>& p)
127{
128 return p.get();
129}
130
131template <typename T> inline void freeOwnedGPtr(T* ptr)
132{
133 g_free(ptr);
134}
135
136} // namespace WTF
137
138using WTF::GOwnPtr;
139
14957cd0
A
140#endif // ENABLE(GLIB_SUPPORT)
141
f9bf01c6 142#endif // GOwnPtr_h
14957cd0 143