OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WTF_UnretainedPtr_h |
| 6 #define WTF_UnretainedPtr_h |
| 7 |
| 8 // UnretainedPtr should be used when a pointer of a GarbageCollected object |
| 9 // is passed to a thread (via postTask) that is not attached to Blink GC. |
| 10 // |
| 11 // postTask(new Task(WTF::bind(&function, UnretainedPtr<GarbageCollectedObject
>(this)))); |
| 12 // |
| 13 // Without UnretainedPtr specified, the target thread keeps |this| pointer alive |
| 14 // by creating a CrossThreadPersistent handle for the pointer. With |
| 15 // UnretainedPtr specified, the target thread doesn't keep |this| pointer alive. |
| 16 // |
| 17 // If the target thread is not attached to Blink GC, the thread is not allowed |
| 18 // to create a CrossThreadPersistent handle, so you need to specify |
| 19 // UnretainedPtr. In this case, you need to write code so that |this| pointer |
| 20 // is kept alive until the target thread finishes processing the task. |
| 21 |
| 22 namespace WTF { |
| 23 |
| 24 template<typename T> |
| 25 class UnretainedPtr { |
| 26 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(UnretainedPtr); |
| 27 WTF_DISALLOW_ZERO_ASSIGNMENT(UnretainedPtr); |
| 28 public: |
| 29 UnretainedPtr(std::nullptr_t) : m_ptr(0) { } |
| 30 UnretainedPtr(T* ptr) : m_ptr(ptr) { } |
| 31 operator T*() const { return m_ptr; } |
| 32 template<typename U> |
| 33 operator RawPtr<U>() const { return m_ptr; } |
| 34 |
| 35 private: |
| 36 T* m_ptr; |
| 37 }; |
| 38 |
| 39 } // namespace WTF |
| 40 |
| 41 using WTF::UnretainedPtr; |
| 42 |
| 43 #endif |
OLD | NEW |