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

Side by Side Diff: base/memory/weak_ptr.h

Issue 2094873003: Make base::WeakPtr moveable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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 | « no previous file | base/memory/weak_ptr.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Weak pointers are pointers to an object that do not affect its lifetime, 5 // Weak pointers are pointers to an object that do not affect its lifetime,
6 // and which may be invalidated (i.e. reset to nullptr) by the object, or its 6 // and which may be invalidated (i.e. reset to nullptr) by the object, or its
7 // owner, at any time, most commonly when the object is about to be deleted. 7 // owner, at any time, most commonly when the object is about to be deleted.
8 8
9 // Weak pointers are useful when an object needs to be accessed safely by one 9 // Weak pointers are useful when an object needs to be accessed safely by one
10 // or more objects other than its owner, and those callers can cope with the 10 // or more objects other than its owner, and those callers can cope with the
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 private: 102 private:
103 friend class base::RefCountedThreadSafe<Flag>; 103 friend class base::RefCountedThreadSafe<Flag>;
104 104
105 ~Flag(); 105 ~Flag();
106 106
107 SequenceChecker sequence_checker_; 107 SequenceChecker sequence_checker_;
108 bool is_valid_; 108 bool is_valid_;
109 }; 109 };
110 110
111 WeakReference(); 111 WeakReference();
112 WeakReference(const WeakReference& other);
113 explicit WeakReference(const Flag* flag); 112 explicit WeakReference(const Flag* flag);
114 ~WeakReference(); 113 ~WeakReference();
115 114
115 WeakReference(WeakReference&& other);
116 WeakReference(const WeakReference& other);
117 WeakReference& operator=(WeakReference&& other) = default;
118 WeakReference& operator=(const WeakReference& other) = default;
119
116 bool is_valid() const; 120 bool is_valid() const;
117 121
118 private: 122 private:
119 scoped_refptr<const Flag> flag_; 123 scoped_refptr<const Flag> flag_;
120 }; 124 };
121 125
122 class BASE_EXPORT WeakReferenceOwner { 126 class BASE_EXPORT WeakReferenceOwner {
123 public: 127 public:
124 WeakReferenceOwner(); 128 WeakReferenceOwner();
125 ~WeakReferenceOwner(); 129 ~WeakReferenceOwner();
(...skipping 12 matching lines...) Expand all
138 142
139 // This class simplifies the implementation of WeakPtr's type conversion 143 // This class simplifies the implementation of WeakPtr's type conversion
140 // constructor by avoiding the need for a public accessor for ref_. A 144 // constructor by avoiding the need for a public accessor for ref_. A
141 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this 145 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this
142 // base class gives us a way to access ref_ in a protected fashion. 146 // base class gives us a way to access ref_ in a protected fashion.
143 class BASE_EXPORT WeakPtrBase { 147 class BASE_EXPORT WeakPtrBase {
144 public: 148 public:
145 WeakPtrBase(); 149 WeakPtrBase();
146 ~WeakPtrBase(); 150 ~WeakPtrBase();
147 151
152 WeakPtrBase(const WeakPtrBase& other) = default;
153 WeakPtrBase(WeakPtrBase&& other) = default;
154 WeakPtrBase& operator=(const WeakPtrBase& other) = default;
155 WeakPtrBase& operator=(WeakPtrBase&& other) = default;
156
148 protected: 157 protected:
149 explicit WeakPtrBase(const WeakReference& ref); 158 explicit WeakPtrBase(const WeakReference& ref);
150 159
151 WeakReference ref_; 160 WeakReference ref_;
152 }; 161 };
153 162
154 // This class provides a common implementation of common functions that would 163 // This class provides a common implementation of common functions that would
155 // otherwise get instantiated separately for each distinct instantiation of 164 // otherwise get instantiated separately for each distinct instantiation of
156 // SupportsWeakPtr<>. 165 // SupportsWeakPtr<>.
157 class SupportsWeakPtrBase { 166 class SupportsWeakPtrBase {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 // foo->method(); 207 // foo->method();
199 // 208 //
200 template <typename T> 209 template <typename T>
201 class WeakPtr : public internal::WeakPtrBase { 210 class WeakPtr : public internal::WeakPtrBase {
202 public: 211 public:
203 WeakPtr() : ptr_(nullptr) {} 212 WeakPtr() : ptr_(nullptr) {}
204 213
205 WeakPtr(std::nullptr_t) : ptr_(nullptr) {} 214 WeakPtr(std::nullptr_t) : ptr_(nullptr) {}
206 215
207 // Allow conversion from U to T provided U "is a" T. Note that this 216 // Allow conversion from U to T provided U "is a" T. Note that this
208 // is separate from the (implicit) copy constructor. 217 // is separate from the (implicit) copy and move constructors.
209 template <typename U> 218 template <typename U>
210 WeakPtr(const WeakPtr<U>& other) : WeakPtrBase(other), ptr_(other.ptr_) { 219 WeakPtr(const WeakPtr<U>& other) : WeakPtrBase(other), ptr_(other.ptr_) {
211 } 220 }
221 template <typename U>
222 WeakPtr(WeakPtr<U>&& other) : WeakPtrBase(other), ptr_(other.ptr_) {
vmpstr 2016/06/24 00:17:39 WeakPtrBase(std::move(other))
Marijn Kruisselbrink 2016/06/24 00:50:15 Done
223 }
212 224
213 T* get() const { return ref_.is_valid() ? ptr_ : nullptr; } 225 T* get() const { return ref_.is_valid() ? ptr_ : nullptr; }
214 226
215 T& operator*() const { 227 T& operator*() const {
216 DCHECK(get() != nullptr); 228 DCHECK(get() != nullptr);
217 return *get(); 229 return *get();
218 } 230 }
219 T* operator->() const { 231 T* operator->() const {
220 DCHECK(get() != nullptr); 232 DCHECK(get() != nullptr);
221 return get(); 233 return get();
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. 352 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails.
341 353
342 template <typename Derived> 354 template <typename Derived>
343 WeakPtr<Derived> AsWeakPtr(Derived* t) { 355 WeakPtr<Derived> AsWeakPtr(Derived* t) {
344 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); 356 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t);
345 } 357 }
346 358
347 } // namespace base 359 } // namespace base
348 360
349 #endif // BASE_MEMORY_WEAK_PTR_H_ 361 #endif // BASE_MEMORY_WEAK_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/weak_ptr.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698