OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google 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 are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef ResourceOwner_h | 31 #ifndef ResourceOwner_h |
32 #define ResourceOwner_h | 32 #define ResourceOwner_h |
33 | 33 |
34 #include "core/fetch/Resource.h" | 34 #include "core/fetch/Resource.h" |
35 #include <type_traits> | |
35 | 36 |
36 namespace blink { | 37 namespace blink { |
37 | 38 |
39 template <typename R, typename C, bool isClientGarbageCollectedMixin> | |
hiroshige
2016/05/27 08:18:38
|R| is not necessary?
nit optional: How about ren
yhirano
2016/05/27 16:37:28
Done.
| |
40 class ResourceOwnerBase; | |
41 | |
42 template <typename R, typename C> | |
43 class ResourceOwnerBase<R, C, true> : public C { | |
44 public: | |
45 DEFINE_INLINE_VIRTUAL_TRACE() | |
46 { | |
47 C::trace(visitor); | |
48 } | |
49 }; | |
50 | |
51 // TODO(yhirano): Remove this template once all ResourceClients become | |
52 // GarbageCollectedMixin. | |
53 template <typename R, typename C> | |
54 class ResourceOwnerBase<R, C, false> : public GarbageCollectedMixin, public C { | |
55 public: | |
56 DEFINE_INLINE_VIRTUAL_TRACE() {} | |
57 }; | |
58 | |
38 template<class R, class C = typename R::ClientType> | 59 template<class R, class C = typename R::ClientType> |
39 class ResourceOwner : public GarbageCollectedMixin, public C { | 60 class ResourceOwner : public ResourceOwnerBase<R, C, std::is_base_of<GarbageColl ectedMixin, C>::value> { |
40 USING_PRE_FINALIZER(ResourceOwner, clearResource); | 61 USING_PRE_FINALIZER(ResourceOwner, clearResource); |
41 public: | 62 public: |
42 using ResourceType = R; | 63 using ResourceType = R; |
64 ~ResourceOwner() override {} | |
65 ResourceType* resource() const { return m_resource; } | |
43 | 66 |
44 virtual ~ResourceOwner(); | 67 DEFINE_INLINE_TRACE() |
45 ResourceType* resource() const { return m_resource.get(); } | 68 { |
46 | 69 visitor->trace(m_resource); |
47 DEFINE_INLINE_VIRTUAL_TRACE() { visitor->trace(m_resource); } | 70 ResourceOwnerBase<R, C, std::is_base_of<GarbageCollectedMixin, C>::value >::trace(visitor); |
71 } | |
48 | 72 |
49 protected: | 73 protected: |
50 ResourceOwner(); | 74 ResourceOwner() |
75 { | |
76 ThreadState::current()->registerPreFinalizer(this); | |
77 } | |
51 | 78 |
52 void setResource(ResourceType*); | 79 void setResource(ResourceType*); |
53 void clearResource() { setResource(nullptr); } | 80 void clearResource() { setResource(nullptr); } |
54 | 81 |
55 private: | 82 private: |
56 Member<ResourceType> m_resource; | 83 Member<ResourceType> m_resource; |
57 }; | 84 }; |
58 | 85 |
59 template<class R, class C> | 86 template<class R, class C> |
60 inline ResourceOwner<R, C>::ResourceOwner() | |
61 { | |
62 ThreadState::current()->registerPreFinalizer(this); | |
63 } | |
64 | |
65 template<class R, class C> | |
66 inline ResourceOwner<R, C>::~ResourceOwner() | |
67 { | |
68 } | |
69 | |
70 template<class R, class C> | |
71 inline void ResourceOwner<R, C>::setResource(R* newResource) | 87 inline void ResourceOwner<R, C>::setResource(R* newResource) |
72 { | 88 { |
73 if (newResource == m_resource) | 89 if (newResource == m_resource) |
74 return; | 90 return; |
75 | 91 |
76 // Some ResourceClient implementations reenter this so | 92 // Some ResourceClient implementations reenter this so |
77 // we need to prevent double removal. | 93 // we need to prevent double removal. |
78 if (ResourceType* oldResource = m_resource.release()) | 94 if (ResourceType* oldResource = m_resource.release()) |
79 oldResource->removeClient(this); | 95 oldResource->removeClient(this); |
80 | 96 |
81 if (newResource) { | 97 if (newResource) { |
82 m_resource = newResource; | 98 m_resource = newResource; |
83 m_resource->addClient(this); | 99 m_resource->addClient(this); |
84 } | 100 } |
85 } | 101 } |
86 | 102 |
87 } // namespace blink | 103 } // namespace blink |
88 | 104 |
89 #endif | 105 #endif |
OLD | NEW |