Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: third_party/WebKit/Source/wtf/PassOwnPtr.h

Issue 1917193006: WTF: Make PassOwnPtr<T> move-only. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/OwnPtr.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // It somewhat breaks the type system to allow transfer of ownership out of
Mikhail 2016/04/27 09:57:10 Now this comment looks outdated.
Yuta Kitamura 2016/04/27 10:10:35 You are right; removed.
51 // a const PassOwnPtr. However, it makes it much easier to work with 51 // a const PassOwnPtr. However, it makes it much easier to work with
52 // PassOwnPtr temporaries, and we don't have a need to use real const 52 // PassOwnPtr temporaries, and we don't have a need to use real const
53 // PassOwnPtrs anyway. 53 // PassOwnPtrs anyway.
54 PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) {} 54 PassOwnPtr(PassOwnPtr&& o) : m_ptr(o.leakPtr()) {}
55 template <typename U> PassOwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleA rgDecl(U, T)); 55 template <typename U> PassOwnPtr(PassOwnPtr<U>&&, EnsurePtrConvertibleArgDec l(U, T));
56 56
57 ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); } 57 ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); }
58 58
59 PtrType get() const { return m_ptr; } 59 PtrType get() const { return m_ptr; }
60 60
61 PtrType leakPtr() const WARN_UNUSED_RETURN; 61 PtrType leakPtr() const WARN_UNUSED_RETURN;
62 62
63 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } 63 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
64 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } 64 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
65 65
66 bool operator!() const { return !m_ptr; } 66 bool operator!() const { return !m_ptr; }
67 explicit operator bool() const { return m_ptr; } 67 explicit operator bool() const { return m_ptr; }
68 68
69 template <typename U> friend PassOwnPtr<U> adoptPtr(U*); 69 template <typename U> friend PassOwnPtr<U> adoptPtr(U*);
70 template <typename U> friend PassOwnPtr<U[]> adoptArrayPtr(U*); 70 template <typename U> friend PassOwnPtr<U[]> adoptArrayPtr(U*);
71 template <typename U> friend class OwnPtr; 71 template <typename U> friend class OwnPtr;
72 72
73 private: 73 private:
74 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) {} 74 explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) {}
75 75
76 PassOwnPtr(const PassOwnPtr&) = delete;
76 PassOwnPtr& operator=(const PassOwnPtr&) = delete; 77 PassOwnPtr& operator=(const PassOwnPtr&) = delete;
77 78
78 // We should never have two OwnPtrs for the same underlying object 79 // We should never have two OwnPtrs for the same underlying object
79 // (otherwise we'll get double-destruction), so these equality operators 80 // (otherwise we'll get double-destruction), so these equality operators
80 // should never be needed. 81 // should never be needed.
81 template <typename U> bool operator==(const PassOwnPtr<U>&) const = delete; 82 template <typename U> bool operator==(const PassOwnPtr<U>&) const = delete;
82 template <typename U> bool operator!=(const PassOwnPtr<U>&) const = delete; 83 template <typename U> bool operator!=(const PassOwnPtr<U>&) const = delete;
83 template <typename U> bool operator==(const OwnPtr<U>&) const = delete; 84 template <typename U> bool operator==(const OwnPtr<U>&) const = delete;
84 template <typename U> bool operator!=(const OwnPtr<U>&) const = delete; 85 template <typename U> bool operator!=(const OwnPtr<U>&) const = delete;
85 86
86 mutable PtrType m_ptr; 87 mutable PtrType m_ptr;
tzik 2016/04/27 09:40:14 Can we remove this "mutable" now?
Yuta Kitamura 2016/04/27 10:10:35 Yep, but removing this "mutable" has caused an err
87 }; 88 };
88 89
89 template <typename T> 90 template <typename T>
90 template <typename U> inline PassOwnPtr<T>::PassOwnPtr(const PassOwnPtr<U>& o, E nsurePtrConvertibleArgDefn(U, T)) 91 template <typename U> inline PassOwnPtr<T>::PassOwnPtr(PassOwnPtr<U>&& o, Ensure PtrConvertibleArgDefn(U, T))
91 : m_ptr(o.leakPtr()) 92 : m_ptr(o.leakPtr())
92 { 93 {
93 static_assert(!std::is_array<T>::value, "pointers to array must never be con verted"); 94 static_assert(!std::is_array<T>::value, "pointers to array must never be con verted");
94 } 95 }
95 96
96 template <typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leak Ptr() const 97 template <typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leak Ptr() const
97 { 98 {
98 PtrType ptr = m_ptr; 99 PtrType ptr = m_ptr;
99 m_ptr = nullptr; 100 m_ptr = nullptr;
100 return ptr; 101 return ptr;
(...skipping 22 matching lines...) Expand all
123 template <typename T> inline PassOwnPtr<T> adoptPtr(T* ptr) 124 template <typename T> inline PassOwnPtr<T> adoptPtr(T* ptr)
124 { 125 {
125 return PassOwnPtr<T>(ptr); 126 return PassOwnPtr<T>(ptr);
126 } 127 }
127 128
128 template <typename T> inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr) 129 template <typename T> inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr)
129 { 130 {
130 return PassOwnPtr<T[]>(ptr); 131 return PassOwnPtr<T[]>(ptr);
131 } 132 }
132 133
133 template <typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(const PassOwnPtr<U>& p) 134 template <typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(PassO wnPtr<U>&& p)
134 { 135 {
135 static_assert(!std::is_array<T>::value, "pointers to array must never be con verted"); 136 static_assert(!std::is_array<T>::value, "pointers to array must never be con verted");
136 return adoptPtr(static_cast<T*>(p.leakPtr())); 137 return adoptPtr(static_cast<T*>(p.leakPtr()));
137 } 138 }
138 139
139 template <typename T> inline T* getPtr(const PassOwnPtr<T>& p) 140 template <typename T> inline T* getPtr(const PassOwnPtr<T>& p)
140 { 141 {
141 return p.get(); 142 return p.get();
142 } 143 }
143 144
144 } // namespace WTF 145 } // namespace WTF
145 146
146 using WTF::PassOwnPtr; 147 using WTF::PassOwnPtr;
147 using WTF::adoptPtr; 148 using WTF::adoptPtr;
148 using WTF::adoptArrayPtr; 149 using WTF::adoptArrayPtr;
149 using WTF::static_pointer_cast; 150 using WTF::static_pointer_cast;
150 151
151 #endif // WTF_PassOwnPtr_h 152 #endif // WTF_PassOwnPtr_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/OwnPtr.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698