| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #ifndef ResourcePtr_h | |
| 27 #define ResourcePtr_h | |
| 28 | |
| 29 #include "core/CoreExport.h" | |
| 30 #include "core/fetch/Resource.h" | |
| 31 #include "wtf/Allocator.h" | |
| 32 | |
| 33 namespace blink { | |
| 34 | |
| 35 class CORE_EXPORT ResourcePtrBase { | |
| 36 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | |
| 37 public: | |
| 38 Resource* get() const { return m_resource; } | |
| 39 bool operator!() const { return !m_resource; } | |
| 40 void clear() { setResource(nullptr); } | |
| 41 | |
| 42 // This conversion operator allows implicit conversion to bool but not to ot
her integer types. | |
| 43 typedef Resource* ResourcePtrBase::*UnspecifiedBoolType; | |
| 44 operator UnspecifiedBoolType() const { return m_resource ? &ResourcePtrBase:
:m_resource : nullptr; } | |
| 45 | |
| 46 protected: | |
| 47 ResourcePtrBase() : m_resource(nullptr) { } | |
| 48 explicit ResourcePtrBase(Resource*); | |
| 49 explicit ResourcePtrBase(const ResourcePtrBase&); | |
| 50 ~ResourcePtrBase(); | |
| 51 | |
| 52 void setResource(Resource*); | |
| 53 | |
| 54 private: | |
| 55 friend class Resource; | |
| 56 ResourcePtrBase& operator=(const ResourcePtrBase&) = delete; | |
| 57 | |
| 58 // The lifetime of the Resource object is explicitly managed by | |
| 59 // reference-counting. | |
| 60 GC_PLUGIN_IGNORE("503485") | |
| 61 Resource* m_resource; | |
| 62 }; | |
| 63 | |
| 64 inline ResourcePtrBase::ResourcePtrBase(Resource* res) | |
| 65 : m_resource(res) | |
| 66 { | |
| 67 if (m_resource) | |
| 68 m_resource->registerHandle(this); | |
| 69 } | |
| 70 | |
| 71 inline ResourcePtrBase::~ResourcePtrBase() | |
| 72 { | |
| 73 if (m_resource) | |
| 74 m_resource->unregisterHandle(this); | |
| 75 } | |
| 76 | |
| 77 inline ResourcePtrBase::ResourcePtrBase(const ResourcePtrBase& o) | |
| 78 : m_resource(o.m_resource) | |
| 79 { | |
| 80 if (m_resource) | |
| 81 m_resource->registerHandle(this); | |
| 82 } | |
| 83 | |
| 84 template <class R> class ResourcePtr final : public ResourcePtrBase { | |
| 85 public: | |
| 86 ResourcePtr() { } | |
| 87 ResourcePtr(R* res) : ResourcePtrBase(res) { } | |
| 88 ResourcePtr(const ResourcePtr<R>& o) : ResourcePtrBase(o) { } | |
| 89 template<typename U> ResourcePtr(const ResourcePtr<U>& o) : ResourcePtrBase(
cast(o.get())) { } | |
| 90 | |
| 91 R* get() const { return static_cast<R*>(ResourcePtrBase::get()); } | |
| 92 R* operator->() const { return get(); } | |
| 93 | |
| 94 ResourcePtr& operator=(R* res) { setResource(res); return *this; } | |
| 95 ResourcePtr& operator=(const ResourcePtr& o) { setResource(o.get()); return
*this; } | |
| 96 template<typename U> ResourcePtr& operator=(const ResourcePtr<U>& o) { setRe
source(cast(o.get())); return *this; } | |
| 97 | |
| 98 bool operator==(const ResourcePtrBase& o) const { return get() == o.get(); } | |
| 99 bool operator!=(const ResourcePtrBase& o) const { return get() != o.get(); } | |
| 100 private: | |
| 101 template<typename U> static R* cast(U* u) { return u; } | |
| 102 }; | |
| 103 | |
| 104 template <class R, class RR> bool operator==(const ResourcePtr<R>& h, const RR*
res) | |
| 105 { | |
| 106 return h.get() == res; | |
| 107 } | |
| 108 template <class R, class RR> bool operator==(const RR* res, const ResourcePtr<R>
& h) | |
| 109 { | |
| 110 return h.get() == res; | |
| 111 } | |
| 112 template <class R, class RR> bool operator!=(const ResourcePtr<R>& h, const RR*
res) | |
| 113 { | |
| 114 return h.get() != res; | |
| 115 } | |
| 116 template <class R, class RR> bool operator!=(const RR* res, const ResourcePtr<R>
& h) | |
| 117 { | |
| 118 return h.get() != res; | |
| 119 } | |
| 120 } // namespace blink | |
| 121 | |
| 122 #endif | |
| OLD | NEW |