| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2013 Intel Corporation. All rights reserved. | 3 * Copyright (C) 2013 Intel Corporation. All rights reserved. |
| 4 * | 4 * |
| 5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
| 7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
| 9 * | 9 * |
| 10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 * Library General Public License for more details. | 13 * Library General Public License for more details. |
| 14 * | 14 * |
| 15 * You should have received a copy of the GNU Library General Public License | 15 * You should have received a copy of the GNU Library General Public License |
| 16 * along with this library; see the file COPYING.LIB. If not, write to | 16 * along with this library; see the file COPYING.LIB. If not, write to |
| 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 * Boston, MA 02110-1301, USA. | 18 * Boston, MA 02110-1301, USA. |
| 19 * | 19 * |
| 20 */ | 20 */ |
| 21 | 21 |
| 22 #ifndef WTF_OwnPtr_h | 22 #ifndef WTF_OwnPtr_h |
| 23 #define WTF_OwnPtr_h | 23 #define WTF_OwnPtr_h |
| 24 | 24 |
| 25 #include "wtf/HashTableDeletedValueType.h" |
| 25 #include "wtf/Noncopyable.h" | 26 #include "wtf/Noncopyable.h" |
| 26 #include "wtf/NullPtr.h" | 27 #include "wtf/NullPtr.h" |
| 27 #include "wtf/OwnPtrCommon.h" | 28 #include "wtf/OwnPtrCommon.h" |
| 28 #include <algorithm> | 29 #include <algorithm> |
| 29 | 30 |
| 30 namespace WTF { | 31 namespace WTF { |
| 31 | 32 |
| 32 template<typename T> class PassOwnPtr; | 33 template<typename T> class PassOwnPtr; |
| 33 | 34 |
| 34 template<typename T> class OwnPtr { | 35 template<typename T> class OwnPtr { |
| 35 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | 36 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 36 // If rvalue references are not supported, the copy constructor is | 37 // If rvalue references are not supported, the copy constructor is |
| 37 // public so OwnPtr cannot be marked noncopyable. See note below. | 38 // public so OwnPtr cannot be marked noncopyable. See note below. |
| 38 WTF_MAKE_NONCOPYABLE(OwnPtr); | 39 WTF_MAKE_NONCOPYABLE(OwnPtr); |
| 39 #endif | 40 #endif |
| 40 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(OwnPtr); | 41 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(OwnPtr); |
| 41 public: | 42 public: |
| 42 typedef typename RemoveExtent<T>::Type ValueType; | 43 typedef typename RemoveExtent<T>::Type ValueType; |
| 43 typedef ValueType* PtrType; | 44 typedef ValueType* PtrType; |
| 44 | 45 |
| 45 OwnPtr() : m_ptr(0) { } | 46 OwnPtr() : m_ptr(0) { } |
| 46 OwnPtr(std::nullptr_t) : m_ptr(0) { } | 47 OwnPtr(std::nullptr_t) : m_ptr(0) { } |
| 47 | 48 |
| 48 // See comment in PassOwnPtr.h for why this takes a const reference. | 49 // See comment in PassOwnPtr.h for why this takes a const reference. |
| 49 OwnPtr(const PassOwnPtr<T>&); | 50 OwnPtr(const PassOwnPtr<T>&); |
| 50 template<typename U> OwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleAr
gDecl(U, T)); | 51 template<typename U> OwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleAr
gDecl(U, T)); |
| 51 | 52 |
| 53 // Hash table deleted values, which are only constructed and never copie
d or destroyed. |
| 54 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } |
| 55 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedV
alue(); } |
| 56 |
| 52 #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | 57 #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 53 // This copy constructor is used implicitly by gcc when it generates | 58 // This copy constructor is used implicitly by gcc when it generates |
| 54 // transients for assigning a PassOwnPtr<T> object to a stack-allocated | 59 // transients for assigning a PassOwnPtr<T> object to a stack-allocated |
| 55 // OwnPtr<T> object. It should never be called explicitly and gcc | 60 // OwnPtr<T> object. It should never be called explicitly and gcc |
| 56 // should optimize away the constructor when generating code. | 61 // should optimize away the constructor when generating code. |
| 57 OwnPtr(const OwnPtr&); | 62 OwnPtr(const OwnPtr&); |
| 58 #endif | 63 #endif |
| 59 | 64 |
| 60 ~OwnPtr() | 65 ~OwnPtr() |
| 61 { | 66 { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 87 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | 92 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 88 OwnPtr(OwnPtr&&); | 93 OwnPtr(OwnPtr&&); |
| 89 template<typename U> OwnPtr(OwnPtr<U>&&); | 94 template<typename U> OwnPtr(OwnPtr<U>&&); |
| 90 | 95 |
| 91 OwnPtr& operator=(OwnPtr&&); | 96 OwnPtr& operator=(OwnPtr&&); |
| 92 template<typename U> OwnPtr& operator=(OwnPtr<U>&&); | 97 template<typename U> OwnPtr& operator=(OwnPtr<U>&&); |
| 93 #endif | 98 #endif |
| 94 | 99 |
| 95 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } | 100 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } |
| 96 | 101 |
| 102 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
| 103 |
| 97 private: | 104 private: |
| 98 #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | 105 #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 99 // If rvalue references are supported, noncopyable takes care of this. | 106 // If rvalue references are supported, noncopyable takes care of this. |
| 100 OwnPtr& operator=(const OwnPtr&); | 107 OwnPtr& operator=(const OwnPtr&); |
| 101 #endif | 108 #endif |
| 102 | 109 |
| 103 // We should never have two OwnPtrs for the same underlying object (othe
rwise we'll get | 110 // We should never have two OwnPtrs for the same underlying object (othe
rwise we'll get |
| 104 // double-destruction), so these equality operators should never be need
ed. | 111 // double-destruction), so these equality operators should never be need
ed. |
| 105 template<typename U> bool operator==(const OwnPtr<U>&) const { COMPILE_A
SSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | 112 template<typename U> bool operator==(const OwnPtr<U>&) const { COMPILE_A
SSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } |
| 106 template<typename U> bool operator!=(const OwnPtr<U>&) const { COMPILE_A
SSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | 113 template<typename U> bool operator!=(const OwnPtr<U>&) const { COMPILE_A
SSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<
T>& p) | 238 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<
T>& p) |
| 232 { | 239 { |
| 233 return p.get(); | 240 return p.get(); |
| 234 } | 241 } |
| 235 | 242 |
| 236 } // namespace WTF | 243 } // namespace WTF |
| 237 | 244 |
| 238 using WTF::OwnPtr; | 245 using WTF::OwnPtr; |
| 239 | 246 |
| 240 #endif // WTF_OwnPtr_h | 247 #endif // WTF_OwnPtr_h |
| OLD | NEW |