2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2014 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.
24 #include <wtf/Assertions.h>
25 #include <wtf/Atomics.h>
26 #include <wtf/GetPtr.h>
27 #include <wtf/Noncopyable.h>
28 #include <wtf/OwnPtrCommon.h>
35 template<typename T> class PassOwnPtr;
36 template<typename T> PassOwnPtr<T> adoptPtr(T*);
38 template<typename T> class OwnPtr {
41 typedef ValueType* PtrType;
43 OwnPtr() : m_ptr(0) { }
44 OwnPtr(std::nullptr_t) : m_ptr(0) { }
46 // See comment in PassOwnPtr.h for why this takes a const reference.
47 template<typename U> OwnPtr(const PassOwnPtr<U>& o);
49 ~OwnPtr() { deleteOwnedPtr(m_ptr); }
51 PtrType get() const { return m_ptr; }
54 PassOwnPtr<T> release();
55 PtrType leakPtr() WARN_UNUSED_RETURN;
57 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
58 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
60 bool operator!() const { return !m_ptr; }
62 // This conversion operator allows implicit conversion to bool but not to other integer types.
63 typedef PtrType OwnPtr::*UnspecifiedBoolType;
64 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; }
66 OwnPtr& operator=(const PassOwnPtr<T>&);
67 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; }
68 template<typename U> OwnPtr& operator=(const PassOwnPtr<U>&);
71 template<typename U> OwnPtr(OwnPtr<U>&&);
73 OwnPtr& operator=(OwnPtr&&);
74 template<typename U> OwnPtr& operator=(OwnPtr<U>&&);
76 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
78 // Construct an object to store into this OwnPtr, but only so long as this OwnPtr
79 // doesn't already point to an object. This will ensure that after you call this,
80 // the OwnPtr will point to an instance of T, even if called concurrently. This
81 // instance may or may not have been created by this call. Moreover, this call uses
82 // an opportunistic transaction, in that we may create an instance of T and then
83 // immediately throw it away, if in the process of creating that instance some
84 // other thread was doing the same thing and stored its instance into this pointer
85 // before we had a chance to do so.
86 template<typename... Args>
87 void createTransactionally(Args...);
90 explicit OwnPtr(PtrType ptr) : m_ptr(ptr) { }
92 // We should never have two OwnPtrs for the same underlying object (otherwise we'll get
93 // double-destruction), so these equality operators should never be needed.
94 template<typename U> bool operator==(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
95 template<typename U> bool operator!=(const OwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
96 template<typename U> bool operator==(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
97 template<typename U> bool operator!=(const PassOwnPtr<U>&) { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
102 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<U>& o)
107 template<typename T> inline void OwnPtr<T>::clear()
114 template<typename T> inline PassOwnPtr<T> OwnPtr<T>::release()
118 return adoptPtr(ptr);
121 template<typename T> inline typename OwnPtr<T>::PtrType OwnPtr<T>::leakPtr()
128 template<typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<T>& o)
132 ASSERT(!ptr || m_ptr != ptr);
137 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<U>& o)
141 ASSERT(!ptr || m_ptr != ptr);
146 template<typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o)
151 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(OwnPtr<U>&& o)
156 template<typename T> inline auto OwnPtr<T>::operator=(OwnPtr&& o) -> OwnPtr&
158 ASSERT(!o || o != m_ptr);
159 OwnPtr ptr = WTF::move(o);
164 template<typename T> template<typename U> inline auto OwnPtr<T>::operator=(OwnPtr<U>&& o) -> OwnPtr&
166 ASSERT(!o || o != m_ptr);
167 OwnPtr ptr = WTF::move(o);
172 template<typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b)
177 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U* b)
182 template<typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>& b)
187 template<typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U* b)
192 template<typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>& b)
197 template <typename T> struct IsSmartPtr<OwnPtr<T>> {
198 static const bool value = true;
201 template<typename T> template<typename... Args> inline void OwnPtr<T>::createTransactionally(Args... args)
204 WTF::loadLoadFence();
208 T* newObject = new T(args...);
209 WTF::storeStoreFence();
210 #if ENABLE(COMPARE_AND_SWAP)
214 WTF::loadLoadFence();
217 } while (!WTF::weakCompareAndSwap(bitwise_cast<void*volatile*>(&m_ptr), nullptr, newObject));
227 #endif // WTF_OwnPtr_h