Merge "Fix build break by removing TIZEN_RECORDING_SURFACE_SET" into tizen_2.1
[framework/web/webkit-efl.git] / Source / WTF / wtf / RetainPtr.h
1 /*
2  *  Copyright (C) 2005, 2006, 2007, 2008, 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 RetainPtr_h
22 #define RetainPtr_h
23
24 #include <wtf/HashTraits.h>
25 #include <wtf/NullPtr.h>
26 #include <wtf/TypeTraits.h>
27 #include <algorithm>
28
29 #if USE(CF)
30 #include <CoreFoundation/CoreFoundation.h>
31 #endif
32
33 #ifdef __OBJC__
34 #import <Foundation/Foundation.h>
35 #endif
36
37 namespace WTF {
38
39     // Unlike most most of our smart pointers, RetainPtr can take either the pointer type or the pointed-to type,
40     // so both RetainPtr<NSDictionary> and RetainPtr<CFDictionaryRef> will work.
41
42     enum AdoptCFTag { AdoptCF };
43     enum AdoptNSTag { AdoptNS };
44     
45 #ifdef __OBJC__
46     inline void adoptNSReference(id ptr)
47     {
48         if (ptr) {
49             CFRetain(ptr);
50             [ptr release];
51         }
52     }
53 #endif
54
55     template<typename T> class RetainPtr {
56     public:
57         typedef typename RemovePointer<T>::Type ValueType;
58         typedef ValueType* PtrType;
59
60         RetainPtr() : m_ptr(0) {}
61         RetainPtr(PtrType ptr) : m_ptr(ptr) { if (ptr) CFRetain(ptr); }
62
63         RetainPtr(AdoptCFTag, PtrType ptr) : m_ptr(ptr) { }
64         RetainPtr(AdoptNSTag, PtrType ptr) : m_ptr(ptr) { adoptNSReference(ptr); }
65         
66         RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
67
68 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
69         RetainPtr(RetainPtr&& o) : m_ptr(o.leakRef()) { }
70 #endif
71
72         // Hash table deleted values, which are only constructed and never copied or destroyed.
73         RetainPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
74         bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
75         
76         ~RetainPtr() { if (PtrType ptr = m_ptr) CFRelease(ptr); }
77         
78         template<typename U> RetainPtr(const RetainPtr<U>&);
79         
80         PtrType get() const { return m_ptr; }
81
82         void clear();
83         PtrType leakRef() WARN_UNUSED_RETURN;
84
85         PtrType operator->() const { return m_ptr; }
86         
87         bool operator!() const { return !m_ptr; }
88     
89         // This conversion operator allows implicit conversion to bool but not to other integer types.
90         typedef PtrType RetainPtr::*UnspecifiedBoolType;
91         operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::m_ptr : 0; }
92         
93         RetainPtr& operator=(const RetainPtr&);
94         template<typename U> RetainPtr& operator=(const RetainPtr<U>&);
95         RetainPtr& operator=(PtrType);
96         template<typename U> RetainPtr& operator=(U*);
97
98 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
99         RetainPtr& operator=(RetainPtr&&);
100         template<typename U> RetainPtr& operator=(RetainPtr<U>&&);
101 #endif
102
103 #if !COMPILER_SUPPORTS(CXX_NULLPTR)
104         RetainPtr& operator=(std::nullptr_t) { clear(); return *this; }
105 #endif
106
107         void adoptCF(PtrType);
108         void adoptNS(PtrType);
109         
110         void swap(RetainPtr&);
111
112     private:
113         static PtrType hashTableDeletedValue() { return reinterpret_cast<PtrType>(-1); }
114
115         PtrType m_ptr;
116     };
117     
118     template<typename T> template<typename U> inline RetainPtr<T>::RetainPtr(const RetainPtr<U>& o)
119         : m_ptr(o.get())
120     {
121         if (PtrType ptr = m_ptr)
122             CFRetain(ptr);
123     }
124
125     template<typename T> inline void RetainPtr<T>::clear()
126     {
127         if (PtrType ptr = m_ptr) {
128             m_ptr = 0;
129             CFRelease(ptr);
130         }
131     }
132
133     template<typename T> inline typename RetainPtr<T>::PtrType RetainPtr<T>::leakRef()
134     {
135         PtrType ptr = m_ptr;
136         m_ptr = 0;
137         return ptr;
138     }
139
140     template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<T>& o)
141     {
142         PtrType optr = o.get();
143         if (optr)
144             CFRetain(optr);
145         PtrType ptr = m_ptr;
146         m_ptr = optr;
147         if (ptr)
148             CFRelease(ptr);
149         return *this;
150     }
151     
152     template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<U>& o)
153     {
154         PtrType optr = o.get();
155         if (optr)
156             CFRetain(optr);
157         PtrType ptr = m_ptr;
158         m_ptr = optr;
159         if (ptr)
160             CFRelease(ptr);
161         return *this;
162     }
163
164     template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType optr)
165     {
166         if (optr)
167             CFRetain(optr);
168         PtrType ptr = m_ptr;
169         m_ptr = optr;
170         if (ptr)
171             CFRelease(ptr);
172         return *this;
173     }
174
175     template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
176     {
177         if (optr)
178             CFRetain(optr);
179         PtrType ptr = m_ptr;
180         m_ptr = optr;
181         if (ptr)
182             CFRelease(ptr);
183         return *this;
184     }
185
186 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
187     template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr<T>&& o)
188     {
189         adoptCF(o.leakRef());
190         return *this;
191     }
192     
193     template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr<U>&& o)
194     {
195         adoptCF(o.leakRef());
196         return *this;
197     }
198 #endif
199
200     template<typename T> inline void RetainPtr<T>::adoptCF(PtrType optr)
201     {
202         PtrType ptr = m_ptr;
203         m_ptr = optr;
204         if (ptr)
205             CFRelease(ptr);
206     }
207
208     template<typename T> inline void RetainPtr<T>::adoptNS(PtrType optr)
209     {
210         adoptNSReference(optr);
211         
212         PtrType ptr = m_ptr;
213         m_ptr = optr;
214         if (ptr)
215             CFRelease(ptr);
216     }
217
218     template<typename T> inline void RetainPtr<T>::swap(RetainPtr<T>& o)
219     {
220         std::swap(m_ptr, o.m_ptr);
221     }
222
223     template<typename T> inline void swap(RetainPtr<T>& a, RetainPtr<T>& b)
224     {
225         a.swap(b);
226     }
227
228     template<typename T, typename U> inline bool operator==(const RetainPtr<T>& a, const RetainPtr<U>& b)
229     { 
230         return a.get() == b.get(); 
231     }
232
233     template<typename T, typename U> inline bool operator==(const RetainPtr<T>& a, U* b)
234     { 
235         return a.get() == b; 
236     }
237     
238     template<typename T, typename U> inline bool operator==(T* a, const RetainPtr<U>& b) 
239     {
240         return a == b.get(); 
241     }
242     
243     template<typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, const RetainPtr<U>& b)
244     { 
245         return a.get() != b.get(); 
246     }
247
248     template<typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, U* b)
249     {
250         return a.get() != b; 
251     }
252
253     template<typename T, typename U> inline bool operator!=(T* a, const RetainPtr<U>& b)
254     { 
255         return a != b.get(); 
256     }
257
258     template<typename T> inline RetainPtr<T> adoptCF(T) WARN_UNUSED_RETURN;
259     template<typename T> inline RetainPtr<T> adoptCF(T o)
260     {
261         return RetainPtr<T>(AdoptCF, o);
262     }
263
264     template<typename T> inline RetainPtr<T> adoptNS(T) WARN_UNUSED_RETURN;
265     template<typename T> inline RetainPtr<T> adoptNS(T o)
266     {
267         return RetainPtr<T>(AdoptNS, o);
268     }
269
270     // Helper function for creating a RetainPtr using template argument deduction.
271     template<typename T> inline RetainPtr<T> retainPtr(T) WARN_UNUSED_RETURN;
272     template<typename T> inline RetainPtr<T> retainPtr(T o)
273     {
274         return RetainPtr<T>(o);
275     }
276
277     template<typename P> struct HashTraits<RetainPtr<P> > : SimpleClassHashTraits<RetainPtr<P> > { };
278     
279     template<typename P> struct PtrHash<RetainPtr<P> > : PtrHash<typename RetainPtr<P>::PtrType> {
280         using PtrHash<typename RetainPtr<P>::PtrType>::hash;
281         static unsigned hash(const RetainPtr<P>& key) { return hash(key.get()); }
282         using PtrHash<typename RetainPtr<P>::PtrType>::equal;
283         static bool equal(const RetainPtr<P>& a, const RetainPtr<P>& b) { return a == b; }
284         static bool equal(typename RetainPtr<P>::PtrType a, const RetainPtr<P>& b) { return a == b; }
285         static bool equal(const RetainPtr<P>& a, typename RetainPtr<P>::PtrType b) { return a == b; }
286     };
287     
288     template<typename P> struct DefaultHash<RetainPtr<P> > { typedef PtrHash<RetainPtr<P> > Hash; };
289
290 } // namespace WTF
291
292 using WTF::AdoptCF;
293 using WTF::AdoptNS;
294 using WTF::adoptCF;
295 using WTF::adoptNS;
296 using WTF::RetainPtr;
297 using WTF::retainPtr;
298
299 #endif // WTF_RetainPtr_h