/* * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. * Copyright (C) 2013 Intel Corporation. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #ifndef WTF_OwnPtr_h #define WTF_OwnPtr_h #include "wtf/Noncopyable.h" #include "wtf/NullPtr.h" #include "wtf/OwnPtrCommon.h" #include namespace WTF { template class PassOwnPtr; template class OwnPtr { #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) // If rvalue references are not supported, the copy constructor is // public so OwnPtr cannot be marked noncopyable. See note below. WTF_MAKE_NONCOPYABLE(OwnPtr); #endif WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(OwnPtr); public: typedef typename RemoveExtent::Type ValueType; typedef ValueType* PtrType; OwnPtr() : m_ptr(0) { } OwnPtr(std::nullptr_t) : m_ptr(0) { } // See comment in PassOwnPtr.h for why this takes a const reference. OwnPtr(const PassOwnPtr&); template OwnPtr(const PassOwnPtr&, EnsurePtrConvertibleArgDecl(U, T)); #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) // This copy constructor is used implicitly by gcc when it generates // transients for assigning a PassOwnPtr object to a stack-allocated // OwnPtr object. It should never be called explicitly and gcc // should optimize away the constructor when generating code. OwnPtr(const OwnPtr&); #endif ~OwnPtr() { OwnedPtrDeleter::deletePtr(m_ptr); m_ptr = 0; } PtrType get() const { return m_ptr; } void clear(); PassOwnPtr release(); PtrType leakPtr() WARN_UNUSED_RETURN; ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } ValueType& operator[](std::ptrdiff_t i) const; bool operator!() const { return !m_ptr; } // This conversion operator allows implicit conversion to bool but not to other integer types. typedef PtrType OwnPtr::*UnspecifiedBoolType; operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; } OwnPtr& operator=(const PassOwnPtr&); OwnPtr& operator=(std::nullptr_t) { clear(); return *this; } template OwnPtr& operator=(const PassOwnPtr&); #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) OwnPtr(OwnPtr&&); template OwnPtr(OwnPtr&&); OwnPtr& operator=(OwnPtr&&); template OwnPtr& operator=(OwnPtr&&); #endif void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } private: #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) // If rvalue references are supported, noncopyable takes care of this. OwnPtr& operator=(const OwnPtr&); #endif // We should never have two OwnPtrs for the same underlying object (otherwise we'll get // double-destruction), so these equality operators should never be needed. template bool operator==(const OwnPtr&) const { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } template bool operator!=(const OwnPtr&) const { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } template bool operator==(const PassOwnPtr&) const { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } template bool operator!=(const PassOwnPtr&) const { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } PtrType m_ptr; }; template inline OwnPtr::OwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) { } template template inline OwnPtr::OwnPtr(const PassOwnPtr& o, EnsurePtrConvertibleArgDefn(U, T)) : m_ptr(o.leakPtr()) { COMPILE_ASSERT(!IsArray::value, Pointers_to_array_must_never_be_converted); } template inline void OwnPtr::clear() { PtrType ptr = m_ptr; m_ptr = 0; OwnedPtrDeleter::deletePtr(ptr); } template inline PassOwnPtr OwnPtr::release() { PtrType ptr = m_ptr; m_ptr = 0; return PassOwnPtr(ptr); } template inline typename OwnPtr::PtrType OwnPtr::leakPtr() { PtrType ptr = m_ptr; m_ptr = 0; return ptr; } template inline typename OwnPtr::ValueType& OwnPtr::operator[](std::ptrdiff_t i) const { COMPILE_ASSERT(IsArray::value, Elements_access_is_possible_for_arrays_only); ASSERT(m_ptr); ASSERT(i >= 0); return m_ptr[i]; } template inline OwnPtr& OwnPtr::operator=(const PassOwnPtr& o) { PtrType ptr = m_ptr; m_ptr = o.leakPtr(); ASSERT(!ptr || m_ptr != ptr); OwnedPtrDeleter::deletePtr(ptr); return *this; } template template inline OwnPtr& OwnPtr::operator=(const PassOwnPtr& o) { COMPILE_ASSERT(!IsArray::value, Pointers_to_array_must_never_be_converted); PtrType ptr = m_ptr; m_ptr = o.leakPtr(); ASSERT(!ptr || m_ptr != ptr); OwnedPtrDeleter::deletePtr(ptr); return *this; } #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) template inline OwnPtr::OwnPtr(OwnPtr&& o) : m_ptr(o.leakPtr()) { } template template inline OwnPtr::OwnPtr(OwnPtr&& o) : m_ptr(o.leakPtr()) { COMPILE_ASSERT(!IsArray::value, Pointers_to_array_must_never_be_converted); } template inline OwnPtr& OwnPtr::operator=(OwnPtr&& o) { PtrType ptr = m_ptr; m_ptr = o.leakPtr(); ASSERT(!ptr || m_ptr != ptr); OwnedPtrDeleter::deletePtr(ptr); return *this; } template template inline OwnPtr& OwnPtr::operator=(OwnPtr&& o) { COMPILE_ASSERT(!IsArray::value, Pointers_to_array_must_never_be_converted); PtrType ptr = m_ptr; m_ptr = o.leakPtr(); ASSERT(!ptr || m_ptr != ptr); OwnedPtrDeleter::deletePtr(ptr); return *this; } #endif template inline void swap(OwnPtr& a, OwnPtr& b) { a.swap(b); } template inline bool operator==(const OwnPtr& a, U* b) { return a.get() == b; } template inline bool operator==(T* a, const OwnPtr& b) { return a == b.get(); } template inline bool operator!=(const OwnPtr& a, U* b) { return a.get() != b; } template inline bool operator!=(T* a, const OwnPtr& b) { return a != b.get(); } template inline typename OwnPtr::PtrType getPtr(const OwnPtr& p) { return p.get(); } } // namespace WTF using WTF::OwnPtr; #endif // WTF_OwnPtr_h