• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 extern "C" void g_free(void*);
39 
40 namespace WTF {
41 
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*);
50 
51 template <typename T> class GOwnPtr : public Noncopyable {
52 public:
m_ptr(ptr)53     explicit GOwnPtr(T* ptr = 0) : m_ptr(ptr) { }
~GOwnPtr()54     ~GOwnPtr() { freeOwnedGPtr(m_ptr); }
55 
get()56     T* get() const { return m_ptr; }
release()57     T* release()
58     {
59         T* ptr = m_ptr;
60         m_ptr = 0;
61         return ptr;
62     }
63 
outPtr()64     T*& outPtr()
65     {
66         ASSERT(!m_ptr);
67         return m_ptr;
68     }
69 
set(T * ptr)70     void set(T* ptr)
71     {
72         ASSERT(!ptr || m_ptr != ptr);
73         freeOwnedGPtr(m_ptr);
74         m_ptr = ptr;
75     }
76 
clear()77     void clear()
78     {
79         freeOwnedGPtr(m_ptr);
80         m_ptr = 0;
81     }
82 
83     T& operator*() const
84     {
85         ASSERT(m_ptr);
86         return *m_ptr;
87     }
88 
89     T* operator->() const
90     {
91         ASSERT(m_ptr);
92         return m_ptr;
93     }
94 
95     bool operator!() const { return !m_ptr; }
96 
97     // This conversion operator allows implicit conversion to bool but not to other integer types.
98     typedef T* GOwnPtr::*UnspecifiedBoolType;
UnspecifiedBoolType()99     operator UnspecifiedBoolType() const { return m_ptr ? &GOwnPtr::m_ptr : 0; }
100 
swap(GOwnPtr & o)101     void swap(GOwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
102 
103 private:
104     T* m_ptr;
105 };
106 
swap(GOwnPtr<T> & a,GOwnPtr<T> & b)107 template <typename T> inline void swap(GOwnPtr<T>& a, GOwnPtr<T>& b)
108 {
109     a.swap(b);
110 }
111 
112 template <typename T, typename U> inline bool operator==(const GOwnPtr<T>& a, U* b)
113 {
114     return a.get() == b;
115 }
116 
117 template <typename T, typename U> inline bool operator==(T* a, const GOwnPtr<U>& b)
118 {
119     return a == b.get();
120 }
121 
122 template <typename T, typename U> inline bool operator!=(const GOwnPtr<T>& a, U* b)
123 {
124     return a.get() != b;
125 }
126 
127 template <typename T, typename U> inline bool operator!=(T* a, const GOwnPtr<U>& b)
128 {
129     return a != b.get();
130 }
131 
getPtr(const GOwnPtr<T> & p)132 template <typename T> inline typename GOwnPtr<T>::PtrType getPtr(const GOwnPtr<T>& p)
133 {
134     return p.get();
135 }
136 
freeOwnedGPtr(T * ptr)137 template <typename T> inline void freeOwnedGPtr(T* ptr)
138 {
139     g_free(ptr);
140 }
141 
142 } // namespace WTF
143 
144 using WTF::GOwnPtr;
145 
146 #endif // GOwnPtr_h
147