OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 13 matching lines...) Expand all Loading... |
24 * | 24 * |
25 */ | 25 */ |
26 | 26 |
27 #ifndef LifecycleObserver_h | 27 #ifndef LifecycleObserver_h |
28 #define LifecycleObserver_h | 28 #define LifecycleObserver_h |
29 | 29 |
30 #include "platform/heap/Handle.h" | 30 #include "platform/heap/Handle.h" |
31 | 31 |
32 namespace blink { | 32 namespace blink { |
33 | 33 |
34 template<typename T, typename Observer> | 34 template<typename Context, typename Observer> |
35 class LifecycleObserver : public GarbageCollectedMixin { | 35 class LifecycleObserver : public GarbageCollectedMixin { |
36 public: | 36 public: |
37 using Context = T; | |
38 | |
39 DEFINE_INLINE_VIRTUAL_TRACE() | 37 DEFINE_INLINE_VIRTUAL_TRACE() |
40 { | 38 { |
41 visitor->trace(m_lifecycleContext); | 39 visitor->trace(m_lifecycleContext); |
42 } | 40 } |
43 | 41 |
44 virtual void contextDestroyed() { } | 42 virtual void contextDestroyed() { } |
45 | 43 |
46 Context* lifecycleContext() const { return m_lifecycleContext; } | 44 Context* lifecycleContext() const { return m_lifecycleContext; } |
47 | 45 |
48 protected: | 46 protected: |
49 explicit LifecycleObserver(Context* context) | 47 explicit LifecycleObserver(Context* context) |
50 : m_lifecycleContext(nullptr) | 48 : m_lifecycleContext(nullptr) |
51 { | 49 { |
52 setContext(context); | 50 setContext(context); |
53 } | 51 } |
54 | 52 |
55 void setContext(Context*); | 53 void setContext(Context*); |
56 | 54 |
57 void clearContext() | 55 void clearContext() |
58 { | 56 { |
59 setContext(nullptr); | 57 setContext(nullptr); |
60 } | 58 } |
61 | 59 |
62 private: | 60 private: |
63 WeakMember<Context> m_lifecycleContext; | 61 WeakMember<Context> m_lifecycleContext; |
64 }; | 62 }; |
65 | 63 |
66 template<typename T, typename Observer> | 64 template<typename Context, typename Observer> |
67 inline void LifecycleObserver<T, Observer>::setContext(Context* context) | 65 inline void LifecycleObserver<Context, Observer>::setContext(Context* context) |
68 { | 66 { |
69 if (m_lifecycleContext) | 67 if (m_lifecycleContext) |
70 m_lifecycleContext->removeObserver(static_cast<Observer*>(this)); | 68 m_lifecycleContext->removeObserver(static_cast<Observer*>(this)); |
71 | 69 |
72 m_lifecycleContext = context; | 70 m_lifecycleContext = context; |
73 | 71 |
74 if (m_lifecycleContext) | 72 if (m_lifecycleContext) |
75 m_lifecycleContext->addObserver(static_cast<Observer*>(this)); | 73 m_lifecycleContext->addObserver(static_cast<Observer*>(this)); |
76 } | 74 } |
77 | 75 |
78 } // namespace blink | 76 } // namespace blink |
79 | 77 |
80 #endif // LifecycleObserver_h | 78 #endif // LifecycleObserver_h |
OLD | NEW |