Merge "Better solution for Docomo 1339 bug." into tizen_2.1
[framework/web/webkit-efl.git] / Source / WTF / wtf / PassRefPtr.h
1 /*
2  *  Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Library General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2 of the License, or (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Library General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Library General Public License
15  *  along with this library; see the file COPYING.LIB.  If not, write to
16  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  *  Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #ifndef WTF_PassRefPtr_h
22 #define WTF_PassRefPtr_h
23
24 #include <wtf/AlwaysInline.h>
25 #include <wtf/NullPtr.h>
26
27 namespace WTF {
28
29     template<typename T> class RefPtr;
30     template<typename T> class PassRefPtr;
31     template<typename T> PassRefPtr<T> adoptRef(T*);
32
33     inline void adopted(const void*) { }
34
35 #if !(PLATFORM(QT) && CPU(ARM))
36     #define REF_DEREF_INLINE ALWAYS_INLINE
37 #else
38     // Older version of gcc used by Harmattan SDK fails to build with ALWAYS_INLINE.
39     // See https://bugs.webkit.org/show_bug.cgi?id=37253 for details.
40     #define REF_DEREF_INLINE inline
41 #endif
42
43     template<typename T> REF_DEREF_INLINE void refIfNotNull(T* ptr)
44     {
45         if (LIKELY(ptr != 0))
46             ptr->ref();
47     }
48
49     template<typename T> REF_DEREF_INLINE void derefIfNotNull(T* ptr)
50     {
51         if (LIKELY(ptr != 0))
52             ptr->deref();
53     }
54
55     #undef REF_DEREF_INLINE
56
57     template<typename T> class PassRefPtr {
58     public:
59         PassRefPtr() : m_ptr(0) { }
60         PassRefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); }
61         // It somewhat breaks the type system to allow transfer of ownership out of
62         // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
63         // temporaries, and we don't have a need to use real const PassRefPtrs anyway.
64         PassRefPtr(const PassRefPtr& o) : m_ptr(o.leakRef()) { }
65         template<typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.leakRef()) { }
66
67         ALWAYS_INLINE ~PassRefPtr() { derefIfNotNull(m_ptr); }
68
69         template<typename U> PassRefPtr(const RefPtr<U>&);
70         
71         T* get() const { return m_ptr; }
72
73         T* leakRef() const WARN_UNUSED_RETURN;
74
75         T& operator*() const { return *m_ptr; }
76         T* operator->() const { return m_ptr; }
77
78         bool operator!() const { return !m_ptr; }
79
80         // This conversion operator allows implicit conversion to bool but not to other integer types.
81         typedef T* (PassRefPtr::*UnspecifiedBoolType);
82         operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::m_ptr : 0; }
83
84         PassRefPtr& operator=(const PassRefPtr&) { COMPILE_ASSERT(!sizeof(T*), PassRefPtr_should_never_be_assigned_to); return *this; }
85
86         friend PassRefPtr adoptRef<T>(T*);
87
88     private:
89         // adopting constructor
90         PassRefPtr(T* ptr, bool) : m_ptr(ptr) { }
91
92         mutable T* m_ptr;
93     };
94     
95     template<typename T> template<typename U> inline PassRefPtr<T>::PassRefPtr(const RefPtr<U>& o)
96         : m_ptr(o.get())
97     {
98         T* ptr = m_ptr;
99         refIfNotNull(ptr);
100     }
101
102     template<typename T> inline T* PassRefPtr<T>::leakRef() const
103     {
104         T* ptr = m_ptr;
105         m_ptr = 0;
106         return ptr;
107     }
108
109     template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const PassRefPtr<U>& b) 
110     { 
111         return a.get() == b.get(); 
112     }
113
114     template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const RefPtr<U>& b) 
115     { 
116         return a.get() == b.get(); 
117     }
118
119     template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, const PassRefPtr<U>& b) 
120     { 
121         return a.get() == b.get(); 
122     }
123
124     template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, U* b) 
125     { 
126         return a.get() == b; 
127     }
128     
129     template<typename T, typename U> inline bool operator==(T* a, const PassRefPtr<U>& b) 
130     {
131         return a == b.get(); 
132     }
133     
134     template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const PassRefPtr<U>& b) 
135     { 
136         return a.get() != b.get(); 
137     }
138
139     template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const RefPtr<U>& b) 
140     { 
141         return a.get() != b.get(); 
142     }
143
144     template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const PassRefPtr<U>& b) 
145     { 
146         return a.get() != b.get(); 
147     }
148
149     template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, U* b)
150     {
151         return a.get() != b; 
152     }
153
154     template<typename T, typename U> inline bool operator!=(T* a, const PassRefPtr<U>& b) 
155     { 
156         return a != b.get(); 
157     }
158     
159     template<typename T> inline PassRefPtr<T> adoptRef(T* p)
160     {
161         adopted(p);
162         return PassRefPtr<T>(p, true);
163     }
164
165     template<typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const PassRefPtr<U>& p) 
166     { 
167         return adoptRef(static_cast<T*>(p.leakRef())); 
168     }
169
170     template<typename T, typename U> inline PassRefPtr<T> const_pointer_cast(const PassRefPtr<U>& p) 
171     { 
172         return adoptRef(const_cast<T*>(p.leakRef())); 
173     }
174
175     template<typename T> inline T* getPtr(const PassRefPtr<T>& p)
176     {
177         return p.get();
178     }
179
180 } // namespace WTF
181
182 using WTF::PassRefPtr;
183 using WTF::adoptRef;
184 using WTF::static_pointer_cast;
185 using WTF::const_pointer_cast;
186
187 #endif // WTF_PassRefPtr_h