]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/gobject/GOwnPtr.h
JavaScriptCore-621.1.tar.gz
[apple/javascriptcore.git] / wtf / gobject / GOwnPtr.h
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
25 #include <algorithm>
26 #include <wtf/Assertions.h>
27 #include <wtf/Noncopyable.h>
28
29 // Forward delcarations at this point avoid the need to include GLib includes
30 // in WTF headers.
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 typedef struct _GFile GFile;
39 extern "C" void g_free(void*);
40
41 namespace WTF {
42
43 template <typename T> inline void freeOwnedGPtr(T* ptr);
44 template<> void freeOwnedGPtr<GError>(GError*);
45 template<> void freeOwnedGPtr<GList>(GList*);
46 template<> void freeOwnedGPtr<GCond>(GCond*);
47 template<> void freeOwnedGPtr<GMutex>(GMutex*);
48 template<> void freeOwnedGPtr<GPatternSpec>(GPatternSpec*);
49 template<> void freeOwnedGPtr<GDir>(GDir*);
50 template<> void freeOwnedGPtr<GHashTable>(GHashTable*);
51 template<> void freeOwnedGPtr<GFile>(GFile*);
52
53 template <typename T> class GOwnPtr : public Noncopyable {
54 public:
55 explicit GOwnPtr(T* ptr = 0) : m_ptr(ptr) { }
56 ~GOwnPtr() { freeOwnedGPtr(m_ptr); }
57
58 T* get() const { return m_ptr; }
59 T* release()
60 {
61 T* ptr = m_ptr;
62 m_ptr = 0;
63 return ptr;
64 }
65
66 T*& outPtr()
67 {
68 ASSERT(!m_ptr);
69 return m_ptr;
70 }
71
72 void set(T* ptr)
73 {
74 ASSERT(!ptr || m_ptr != ptr);
75 freeOwnedGPtr(m_ptr);
76 m_ptr = ptr;
77 }
78
79 void clear()
80 {
81 freeOwnedGPtr(m_ptr);
82 m_ptr = 0;
83 }
84
85 T& operator*() const
86 {
87 ASSERT(m_ptr);
88 return *m_ptr;
89 }
90
91 T* operator->() const
92 {
93 ASSERT(m_ptr);
94 return m_ptr;
95 }
96
97 bool operator!() const { return !m_ptr; }
98
99 // This conversion operator allows implicit conversion to bool but not to other integer types.
100 typedef T* GOwnPtr::*UnspecifiedBoolType;
101 operator UnspecifiedBoolType() const { return m_ptr ? &GOwnPtr::m_ptr : 0; }
102
103 void swap(GOwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
104
105 private:
106 T* m_ptr;
107 };
108
109 template <typename T> inline void swap(GOwnPtr<T>& a, GOwnPtr<T>& b)
110 {
111 a.swap(b);
112 }
113
114 template <typename T, typename U> inline bool operator==(const GOwnPtr<T>& a, U* b)
115 {
116 return a.get() == b;
117 }
118
119 template <typename T, typename U> inline bool operator==(T* a, const GOwnPtr<U>& b)
120 {
121 return a == b.get();
122 }
123
124 template <typename T, typename U> inline bool operator!=(const GOwnPtr<T>& a, U* b)
125 {
126 return a.get() != b;
127 }
128
129 template <typename T, typename U> inline bool operator!=(T* a, const GOwnPtr<U>& b)
130 {
131 return a != b.get();
132 }
133
134 template <typename T> inline typename GOwnPtr<T>::PtrType getPtr(const GOwnPtr<T>& p)
135 {
136 return p.get();
137 }
138
139 template <typename T> inline void freeOwnedGPtr(T* ptr)
140 {
141 g_free(ptr);
142 }
143
144 } // namespace WTF
145
146 using WTF::GOwnPtr;
147
148 #endif // GOwnPtr_h