OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 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 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 template <typename T> class PassOwnPtr { | 41 template <typename T> class PassOwnPtr { |
42 DISALLOW_NEW(); | 42 DISALLOW_NEW(); |
43 public: | 43 public: |
44 typedef typename std::remove_extent<T>::type ValueType; | 44 typedef typename std::remove_extent<T>::type ValueType; |
45 typedef ValueType* PtrType; | 45 typedef ValueType* PtrType; |
46 | 46 |
47 PassOwnPtr() : m_ptr(nullptr) {} | 47 PassOwnPtr() : m_ptr(nullptr) {} |
48 PassOwnPtr(std::nullptr_t) : m_ptr(nullptr) {} | 48 PassOwnPtr(std::nullptr_t) : m_ptr(nullptr) {} |
49 | 49 |
50 // It somewhat breaks the type system to allow transfer of ownership out of | 50 PassOwnPtr(PassOwnPtr&& o) : m_ptr(o.leakPtr()) {} |
51 // a const PassOwnPtr. However, it makes it much easier to work with | 51 template <typename U> PassOwnPtr(PassOwnPtr<U>&&, EnsurePtrConvertibleArgDec
l(U, T)); |
52 // PassOwnPtr temporaries, and we don't have a need to use real const | |
53 // PassOwnPtrs anyway. | |
54 PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) {} | |
55 template <typename U> PassOwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleA
rgDecl(U, T)); | |
56 | 52 |
57 ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); } | 53 ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); } |
58 | 54 |
59 PtrType get() const { return m_ptr; } | 55 PtrType get() const { return m_ptr; } |
60 | 56 |
61 PtrType leakPtr() const WARN_UNUSED_RETURN; | 57 PtrType leakPtr() WARN_UNUSED_RETURN; |
62 | 58 |
63 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } | 59 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } |
64 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } | 60 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } |
65 | 61 |
66 bool operator!() const { return !m_ptr; } | 62 bool operator!() const { return !m_ptr; } |
67 explicit operator bool() const { return m_ptr; } | 63 explicit operator bool() const { return m_ptr; } |
68 | 64 |
69 template <typename U> friend PassOwnPtr<U> adoptPtr(U*); | 65 template <typename U> friend PassOwnPtr<U> adoptPtr(U*); |
70 template <typename U> friend PassOwnPtr<U[]> adoptArrayPtr(U*); | 66 template <typename U> friend PassOwnPtr<U[]> adoptArrayPtr(U*); |
71 template <typename U> friend class OwnPtr; | 67 template <typename U> friend class OwnPtr; |
72 | 68 |
73 private: | 69 private: |
74 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) {} | 70 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) {} |
75 | 71 |
| 72 PassOwnPtr(const PassOwnPtr&) = delete; |
76 PassOwnPtr& operator=(const PassOwnPtr&) = delete; | 73 PassOwnPtr& operator=(const PassOwnPtr&) = delete; |
77 | 74 |
78 // We should never have two OwnPtrs for the same underlying object | 75 // We should never have two OwnPtrs for the same underlying object |
79 // (otherwise we'll get double-destruction), so these equality operators | 76 // (otherwise we'll get double-destruction), so these equality operators |
80 // should never be needed. | 77 // should never be needed. |
81 template <typename U> bool operator==(const PassOwnPtr<U>&) const = delete; | 78 template <typename U> bool operator==(const PassOwnPtr<U>&) const = delete; |
82 template <typename U> bool operator!=(const PassOwnPtr<U>&) const = delete; | 79 template <typename U> bool operator!=(const PassOwnPtr<U>&) const = delete; |
83 template <typename U> bool operator==(const OwnPtr<U>&) const = delete; | 80 template <typename U> bool operator==(const OwnPtr<U>&) const = delete; |
84 template <typename U> bool operator!=(const OwnPtr<U>&) const = delete; | 81 template <typename U> bool operator!=(const OwnPtr<U>&) const = delete; |
85 | 82 |
86 mutable PtrType m_ptr; | 83 PtrType m_ptr; |
87 }; | 84 }; |
88 | 85 |
89 template <typename T> | 86 template <typename T> |
90 template <typename U> inline PassOwnPtr<T>::PassOwnPtr(const PassOwnPtr<U>& o, E
nsurePtrConvertibleArgDefn(U, T)) | 87 template <typename U> inline PassOwnPtr<T>::PassOwnPtr(PassOwnPtr<U>&& o, Ensure
PtrConvertibleArgDefn(U, T)) |
91 : m_ptr(o.leakPtr()) | 88 : m_ptr(o.leakPtr()) |
92 { | 89 { |
93 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 90 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); |
94 } | 91 } |
95 | 92 |
96 template <typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leak
Ptr() const | 93 template <typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leak
Ptr() |
97 { | 94 { |
98 PtrType ptr = m_ptr; | 95 PtrType ptr = m_ptr; |
99 m_ptr = nullptr; | 96 m_ptr = nullptr; |
100 return ptr; | 97 return ptr; |
101 } | 98 } |
102 | 99 |
103 template <typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a,
U* b) | 100 template <typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a,
U* b) |
104 { | 101 { |
105 return a.get() == b; | 102 return a.get() == b; |
106 } | 103 } |
(...skipping 16 matching lines...) Expand all Loading... |
123 template <typename T> inline PassOwnPtr<T> adoptPtr(T* ptr) | 120 template <typename T> inline PassOwnPtr<T> adoptPtr(T* ptr) |
124 { | 121 { |
125 return PassOwnPtr<T>(ptr); | 122 return PassOwnPtr<T>(ptr); |
126 } | 123 } |
127 | 124 |
128 template <typename T> inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr) | 125 template <typename T> inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr) |
129 { | 126 { |
130 return PassOwnPtr<T[]>(ptr); | 127 return PassOwnPtr<T[]>(ptr); |
131 } | 128 } |
132 | 129 |
133 template <typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(const
PassOwnPtr<U>& p) | 130 template <typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(PassO
wnPtr<U>&& p) |
134 { | 131 { |
135 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 132 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); |
136 return adoptPtr(static_cast<T*>(p.leakPtr())); | 133 return adoptPtr(static_cast<T*>(p.leakPtr())); |
137 } | 134 } |
138 | 135 |
139 template <typename T> inline T* getPtr(const PassOwnPtr<T>& p) | 136 template <typename T> inline T* getPtr(const PassOwnPtr<T>& p) |
140 { | 137 { |
141 return p.get(); | 138 return p.get(); |
142 } | 139 } |
143 | 140 |
144 } // namespace WTF | 141 } // namespace WTF |
145 | 142 |
146 using WTF::PassOwnPtr; | 143 using WTF::PassOwnPtr; |
147 using WTF::adoptPtr; | 144 using WTF::adoptPtr; |
148 using WTF::adoptArrayPtr; | 145 using WTF::adoptArrayPtr; |
149 using WTF::static_pointer_cast; | 146 using WTF::static_pointer_cast; |
150 | 147 |
151 #endif // WTF_PassOwnPtr_h | 148 #endif // WTF_PassOwnPtr_h |
OLD | NEW |