Chromium Code Reviews| Index: Source/platform/heap/Handle.h |
| diff --git a/Source/platform/heap/Handle.h b/Source/platform/heap/Handle.h |
| index b25f3bbc0789009772a04ecf8d53943cbcdb9614..d085230a025cd81fc901a90a0eb8eb1f2b608471 100644 |
| --- a/Source/platform/heap/Handle.h |
| +++ b/Source/platform/heap/Handle.h |
| @@ -1011,6 +1011,59 @@ private: |
| Member<T> m_object; |
| }; |
| +// SelfKeepAlive<Object> is the idiom to use for objects that have to keep |
| +// themselves temporarily alive and cannot rely on there being some |
| +// external reference in that interval: |
| +// |
| +// class Opener { |
| +// public: |
| +// ... |
| +// void open() |
| +// { |
| +// // Retain a self-reference while in an open()ed state: |
| +// m_keepAlive = this; |
| +// .... |
| +// } |
| +// |
| +// void close() |
| +// { |
| +// // Clear self-reference that ensured we were kept alive while opened. |
| +// m_keepAlive.clear(); |
| +// .... |
| +// } |
| +// |
| +// private: |
| +// ... |
| +// SelfKeepAlive m_keepAlive; |
| +// }; |
| +// |
| +// The responsibility to call clear() in a timely fashion resides with the implementation |
| +// of the object. |
| +// |
| +// |
| +template<typename Self> |
| +class SelfKeepAlive { |
|
haraken
2015/07/30 11:41:21
Can we land this (with some HeapTests) ahead of th
|
| +public: |
| + SelfKeepAlive& operator=(Self* self) |
| + { |
| + ASSERT(!m_keepAlive || m_keepAlive.get() == self); |
| + m_keepAlive = self; |
| + return *this; |
| + } |
| + |
| + void clear() |
| + { |
| + m_keepAlive = nullptr; |
| + } |
| + |
| + typedef Persistent<Self> (SelfKeepAlive::*UnspecifiedBoolType); |
| + operator UnspecifiedBoolType() const { return m_keepAlive ? &SelfKeepAlive::m_keepAlive : 0; } |
| + |
| +private: |
| + GC_PLUGIN_IGNORE("420515") |
| + Persistent<Self> m_keepAlive; |
| +}; |
| + |
| } // namespace blink |
| namespace WTF { |