Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / PassOwnPtr.h
blobc35aefec67576049de781568f118d592caf6bf6e
1 /*
2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Intel Corporation. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef WTF_PassOwnPtr_h
28 #define WTF_PassOwnPtr_h
30 #include "wtf/NullPtr.h"
31 #include "wtf/OwnPtrCommon.h"
33 namespace WTF {
35 template<typename T> class OwnPtr;
36 template<typename T> class PassOwnPtr;
37 template<typename T> PassOwnPtr<T> adoptPtr(T*);
38 template<typename T> PassOwnPtr<T[]> adoptArrayPtr(T*);
40 template<typename T> class PassOwnPtr {
41 public:
42 typedef typename RemoveExtent<T>::Type ValueType;
43 typedef ValueType* PtrType;
45 PassOwnPtr() : m_ptr(0) { }
46 PassOwnPtr(std::nullptr_t) : m_ptr(0) { }
48 // It somewhat breaks the type system to allow transfer of ownership out of
49 // a const PassOwnPtr. However, it makes it much easier to work with PassOwnPtr
50 // temporaries, and we don't have a need to use real const PassOwnPtrs anyway.
51 PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) { }
52 template<typename U> PassOwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleArgDecl(U, T));
54 ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); }
56 PtrType get() const { return m_ptr; }
58 PtrType leakPtr() const WARN_UNUSED_RETURN;
60 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
61 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
63 bool operator!() const { return !m_ptr; }
65 // This conversion operator allows implicit conversion to bool but not to other integer types.
66 typedef PtrType PassOwnPtr::*UnspecifiedBoolType;
67 operator UnspecifiedBoolType() const { return m_ptr ? &PassOwnPtr::m_ptr : 0; }
69 template<typename U> friend PassOwnPtr<U> adoptPtr(U*);
70 template<typename U> friend PassOwnPtr<U[]> adoptArrayPtr(U*);
71 template<typename U> friend class OwnPtr;
73 private:
74 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) { }
76 PassOwnPtr& operator=(const PassOwnPtr&) { static_assert(!sizeof(T*), "PassOwnPtr should never be assigned to"); return *this; }
78 // We should never have two OwnPtrs for the same underlying object (otherwise we'll get
79 // double-destruction), so these equality operators should never be needed.
80 template<typename U> bool operator==(const PassOwnPtr<U>&) const { static_assert(!sizeof(U*), "OwnPtrs should never be equal"); return false; }
81 template<typename U> bool operator!=(const PassOwnPtr<U>&) const { static_assert(!sizeof(U*), "OwnPtrs should never be equal"); return false; }
82 template<typename U> bool operator==(const OwnPtr<U>&) const { static_assert(!sizeof(U*), "OwnPtrs should never be equal"); return false; }
83 template<typename U> bool operator!=(const OwnPtr<U>&) const { static_assert(!sizeof(U*), "OwnPtrs should never be equal"); return false; }
85 mutable PtrType m_ptr;
88 template<typename T> template<typename U> inline PassOwnPtr<T>::PassOwnPtr(const PassOwnPtr<U>& o, EnsurePtrConvertibleArgDefn(U, T))
89 : m_ptr(o.leakPtr())
91 static_assert(!IsArray<T>::value, "pointers to array must never be converted");
94 template<typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leakPtr() const
96 PtrType ptr = m_ptr;
97 m_ptr = 0;
98 return ptr;
101 template<typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a, U* b)
103 return a.get() == b;
106 template<typename T, typename U> inline bool operator==(T* a, const PassOwnPtr<U>& b)
108 return a == b.get();
111 template<typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a, U* b)
113 return a.get() != b;
116 template<typename T, typename U> inline bool operator!=(T* a, const PassOwnPtr<U>& b)
118 return a != b.get();
121 template<typename T> inline PassOwnPtr<T> adoptPtr(T* ptr)
123 return PassOwnPtr<T>(ptr);
126 template<typename T> inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr)
128 return PassOwnPtr<T[]>(ptr);
131 template<typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(const PassOwnPtr<U>& p)
133 static_assert(!IsArray<T>::value, "pointers to array must never be converted");
134 return adoptPtr(static_cast<T*>(p.leakPtr()));
137 template<typename T> inline T* getPtr(const PassOwnPtr<T>& p)
139 return p.get();
142 } // namespace WTF
144 using WTF::PassOwnPtr;
145 using WTF::adoptPtr;
146 using WTF::adoptArrayPtr;
147 using WTF::static_pointer_cast;
149 #endif // WTF_PassOwnPtr_h