2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "core/CoreExport.h"
30 #include "core/fetch/Resource.h"
31 #include "wtf/Allocator.h"
35 class CORE_EXPORT ResourcePtrBase
{
36 ALLOW_ONLY_INLINE_ALLOCATION();
38 Resource
* get() const { return m_resource
; }
39 bool operator!() const { return !m_resource
; }
40 void clear() { setResource(nullptr); }
42 // This conversion operator allows implicit conversion to bool but not to other integer types.
43 typedef Resource
* ResourcePtrBase::*UnspecifiedBoolType
;
44 operator UnspecifiedBoolType() const { return m_resource
? &ResourcePtrBase::m_resource
: nullptr; }
47 ResourcePtrBase() : m_resource(nullptr) { }
48 explicit ResourcePtrBase(Resource
*);
49 explicit ResourcePtrBase(const ResourcePtrBase
&);
52 void setResource(Resource
*);
55 friend class Resource
;
56 ResourcePtrBase
& operator=(const ResourcePtrBase
&) = delete;
58 // The lifetime of the Resource object is explicitly managed by
59 // reference-counting.
60 GC_PLUGIN_IGNORE("503485")
64 inline ResourcePtrBase::ResourcePtrBase(Resource
* res
)
68 m_resource
->registerHandle(this);
71 inline ResourcePtrBase::~ResourcePtrBase()
74 m_resource
->unregisterHandle(this);
77 inline ResourcePtrBase::ResourcePtrBase(const ResourcePtrBase
& o
)
78 : m_resource(o
.m_resource
)
81 m_resource
->registerHandle(this);
84 template <class R
> class ResourcePtr final
: public ResourcePtrBase
{
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())) { }
91 R
* get() const { return static_cast<R
*>(ResourcePtrBase::get()); }
92 R
* operator->() const { return get(); }
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
) { setResource(cast(o
.get())); return *this; }
98 bool operator==(const ResourcePtrBase
& o
) const { return get() == o
.get(); }
99 bool operator!=(const ResourcePtrBase
& o
) const { return get() != o
.get(); }
101 template<typename U
> static R
* cast(U
* u
) { return u
; }
104 template <class R
, class RR
> bool operator==(const ResourcePtr
<R
>& h
, const RR
* res
)
106 return h
.get() == res
;
108 template <class R
, class RR
> bool operator==(const RR
* res
, const ResourcePtr
<R
>& h
)
110 return h
.get() == res
;
112 template <class R
, class RR
> bool operator!=(const ResourcePtr
<R
>& h
, const RR
* res
)
114 return h
.get() != res
;
116 template <class R
, class RR
> bool operator!=(const RR
* res
, const ResourcePtr
<R
>& h
)
118 return h
.get() != res
;