Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / PassRefPtr.h
blob9ae81bf1cc91b40aea48c7f606c977a40f6f7359
1 /*
2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
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.
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.
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.
21 #ifndef WTF_PassRefPtr_h
22 #define WTF_PassRefPtr_h
24 #include "wtf/Assertions.h"
25 #include "wtf/NullPtr.h"
26 #include "wtf/RawPtr.h"
27 #include "wtf/TypeTraits.h"
29 namespace WTF {
31 template<typename T> class RefPtr;
32 template<typename T> class PassRefPtr;
33 template<typename T> PassRefPtr<T> adoptRef(T*);
35 inline void adopted(const void*) { }
37 // requireAdoption() is not overloaded for WTF::RefCounted, which has a
38 // built-in assumption that adoption is required. requireAdoption() is
39 // for bootstrapping alternate reference count classes that are compatible
40 // with ReftPtr/PassRefPtr but cannot have adoption checks enabled
41 // by default, such as skia's SkRefCnt. The purpose of requireAdoption()
42 // is to enable adoption checks only once it is known that the object will
43 // be used with RefPtr/PassRefPtr.
44 inline void requireAdoption(const void*) { }
46 template<typename T> ALWAYS_INLINE void refIfNotNull(T* ptr)
48 if (LIKELY(ptr != 0)) {
49 requireAdoption(ptr);
50 ptr->ref();
54 template<typename T> ALWAYS_INLINE void derefIfNotNull(T* ptr)
56 if (LIKELY(ptr != 0))
57 ptr->deref();
60 template<typename T> class PassRefPtr {
61 public:
62 PassRefPtr() : m_ptr(0) { }
63 PassRefPtr(std::nullptr_t) : m_ptr(0) { }
64 PassRefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); }
65 template<typename U> PassRefPtr(const RawPtr<U>& ptr, EnsurePtrConvertibleArgDecl(U, T)) : m_ptr(ptr.get()) { refIfNotNull(m_ptr); }
66 explicit PassRefPtr(T& ptr) : m_ptr(&ptr) { m_ptr->ref(); }
67 // It somewhat breaks the type system to allow transfer of ownership out of
68 // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
69 // temporaries, and we don't have a need to use real const PassRefPtrs anyway.
70 PassRefPtr(const PassRefPtr& o) : m_ptr(o.leakRef()) { }
71 template<typename U> PassRefPtr(const PassRefPtr<U>& o, EnsurePtrConvertibleArgDecl(U, T)) : m_ptr(o.leakRef()) { }
73 ALWAYS_INLINE ~PassRefPtr() { derefIfNotNull(m_ptr); }
75 template<typename U> PassRefPtr(const RefPtr<U>&, EnsurePtrConvertibleArgDecl(U, T));
77 T* get() const { return m_ptr; }
79 T* leakRef() const WARN_UNUSED_RETURN;
81 T& operator*() const { return *m_ptr; }
82 T* operator->() const { return m_ptr; }
84 bool operator!() const { return !m_ptr; }
86 // This conversion operator allows implicit conversion to bool but not to other integer types.
87 typedef T* (PassRefPtr::*UnspecifiedBoolType);
88 operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::m_ptr : 0; }
90 friend PassRefPtr adoptRef<T>(T*);
92 private:
93 enum AdoptRefTag { AdoptRef };
94 PassRefPtr(T* ptr, AdoptRefTag) : m_ptr(ptr) { }
96 PassRefPtr& operator=(const PassRefPtr&) { static_assert(!sizeof(T*), "PassRefPtr should never be assigned to"); return *this; }
98 mutable T* m_ptr;
101 template<typename T> template<typename U> inline PassRefPtr<T>::PassRefPtr(const RefPtr<U>& o, EnsurePtrConvertibleArgDefn(U, T))
102 : m_ptr(o.get())
104 T* ptr = m_ptr;
105 refIfNotNull(ptr);
108 template<typename T> inline T* PassRefPtr<T>::leakRef() const
110 T* ptr = m_ptr;
111 m_ptr = 0;
112 return ptr;
115 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const PassRefPtr<U>& b)
117 return a.get() == b.get();
120 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const RefPtr<U>& b)
122 return a.get() == b.get();
125 template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, const PassRefPtr<U>& b)
127 return a.get() == b.get();
130 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, U* b)
132 return a.get() == b;
135 template<typename T, typename U> inline bool operator==(T* a, const PassRefPtr<U>& b)
137 return a == b.get();
140 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const RawPtr<U>& b)
142 return a.get() == b.get();
145 template<typename T, typename U> inline bool operator==(const RawPtr<T>& a, const PassRefPtr<U>& b)
147 return a.get() == b.get();
150 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const PassRefPtr<U>& b)
152 return a.get() != b.get();
155 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const RefPtr<U>& b)
157 return a.get() != b.get();
160 template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const PassRefPtr<U>& b)
162 return a.get() != b.get();
165 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, U* b)
167 return a.get() != b;
170 template<typename T, typename U> inline bool operator!=(T* a, const PassRefPtr<U>& b)
172 return a != b.get();
175 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const RawPtr<U>& b)
177 return a.get() != b.get();
180 template<typename T, typename U> inline bool operator!=(const RawPtr<T>& a, const PassRefPtr<U>& b)
182 return a.get() != b.get();
185 template<typename T> PassRefPtr<T> adoptRef(T* p)
187 adopted(p);
188 return PassRefPtr<T>(p, PassRefPtr<T>::AdoptRef);
191 template<typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const PassRefPtr<U>& p)
193 return adoptRef(static_cast<T*>(p.leakRef()));
196 template<typename T> inline T* getPtr(const PassRefPtr<T>& p)
198 return p.get();
201 } // namespace WTF
203 using WTF::PassRefPtr;
204 using WTF::adoptRef;
205 using WTF::static_pointer_cast;
207 #endif // WTF_PassRefPtr_h