Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 NULL) by the object, or its | 6 // and which may be invalidated (i.e. reset to NULL) 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 |
| 11 // object vanishing and e.g. tasks posted to it being silently dropped. | 11 // object vanishing and e.g. tasks posted to it being silently dropped. |
| 12 // Reference-counting such an object would complicate the ownership graph and | 12 // Reference-counting such an object would complicate the ownership graph and |
| 13 // make it harder to reason about the object's lifetime. | 13 // make it harder to reason about the object's lifetime. |
| 14 | 14 |
| 15 // EXAMPLE: | 15 // EXAMPLE: |
| 16 // | 16 // |
| 17 // class Controller { | 17 // class Controller { |
| 18 // public: | 18 // public: |
| 19 // Controller() : weak_factory_(this) {} | |
|
Nico
2015/09/11 18:41:39
It's not always `this`, is it? It's just the point
scheib
2015/09/11 19:40:29
True, but in this example the factory vends Contro
| |
| 19 // void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); } | 20 // void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); } |
| 20 // void WorkComplete(const Result& result) { ... } | 21 // void WorkComplete(const Result& result) { ... } |
| 21 // private: | 22 // private: |
| 22 // // Member variables should appear before the WeakPtrFactory, to ensure | 23 // // Member variables should appear before the WeakPtrFactory, to ensure |
| 23 // // that any WeakPtrs to Controller are invalidated before its members | 24 // // that any WeakPtrs to Controller are invalidated before its members |
| 24 // // variable's destructors are executed, rendering them invalid. | 25 // // variable's destructors are executed, rendering them invalid. |
| 25 // WeakPtrFactory<Controller> weak_factory_; | 26 // WeakPtrFactory<Controller> weak_factory_; |
| 26 // }; | 27 // }; |
| 27 // | 28 // |
| 28 // class Worker { | 29 // class Worker { |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 331 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
| 331 | 332 |
| 332 template <typename Derived> | 333 template <typename Derived> |
| 333 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 334 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
| 334 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 335 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
| 335 } | 336 } |
| 336 | 337 |
| 337 } // namespace base | 338 } // namespace base |
| 338 | 339 |
| 339 #endif // BASE_MEMORY_WEAK_PTR_H_ | 340 #endif // BASE_MEMORY_WEAK_PTR_H_ |
| OLD | NEW |