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 14 matching lines...) Expand all Loading... |
25 | 25 |
26 #ifndef ResourcePtr_h | 26 #ifndef ResourcePtr_h |
27 #define ResourcePtr_h | 27 #define ResourcePtr_h |
28 | 28 |
29 #include "core/fetch/Resource.h" | 29 #include "core/fetch/Resource.h" |
30 | 30 |
31 namespace blink { | 31 namespace blink { |
32 | 32 |
33 class ResourcePtrBase { | 33 class ResourcePtrBase { |
34 public: | 34 public: |
35 ~ResourcePtrBase(); | |
36 | |
37 Resource* get() const { return m_resource; } | 35 Resource* get() const { return m_resource; } |
38 bool operator!() const { return !m_resource; } | 36 bool operator!() const { return !m_resource; } |
39 void clear() { setResource(0); } | 37 void clear() { setResource(nullptr); } |
40 | 38 |
41 // This conversion operator allows implicit conversion to bool but not to ot
her integer types. | 39 // This conversion operator allows implicit conversion to bool but not to ot
her integer types. |
42 typedef Resource* ResourcePtrBase::*UnspecifiedBoolType; | 40 typedef Resource* ResourcePtrBase::*UnspecifiedBoolType; |
43 operator UnspecifiedBoolType() const { return m_resource ? &ResourcePtrBase:
:m_resource : 0; } | 41 operator UnspecifiedBoolType() const { return m_resource ? &ResourcePtrBase:
:m_resource : nullptr; } |
44 | 42 |
45 protected: | 43 protected: |
46 ResourcePtrBase() : m_resource(0) { } | 44 ResourcePtrBase() : m_resource(nullptr) { } |
47 ResourcePtrBase(Resource*); | 45 explicit ResourcePtrBase(Resource*); |
48 ResourcePtrBase(const ResourcePtrBase&); | 46 explicit ResourcePtrBase(const ResourcePtrBase&); |
| 47 ~ResourcePtrBase(); |
49 | 48 |
50 void setResource(Resource*); | 49 void setResource(Resource*); |
51 | 50 |
52 private: | 51 private: |
53 ResourcePtrBase& operator=(const ResourcePtrBase&) { return *this; } | |
54 | |
55 friend class Resource; | 52 friend class Resource; |
| 53 ResourcePtrBase& operator=(const ResourcePtrBase&) = delete; |
56 | 54 |
57 Resource* m_resource; | 55 Resource* m_resource; |
58 }; | 56 }; |
59 | 57 |
60 inline ResourcePtrBase::ResourcePtrBase(Resource* res) | 58 inline ResourcePtrBase::ResourcePtrBase(Resource* res) |
61 : m_resource(res) | 59 : m_resource(res) |
62 { | 60 { |
63 if (m_resource) | 61 if (m_resource) |
64 m_resource->registerHandle(this); | 62 m_resource->registerHandle(this); |
65 } | 63 } |
66 | 64 |
67 inline ResourcePtrBase::~ResourcePtrBase() | 65 inline ResourcePtrBase::~ResourcePtrBase() |
68 { | 66 { |
69 if (m_resource) | 67 if (m_resource) |
70 m_resource->unregisterHandle(this); | 68 m_resource->unregisterHandle(this); |
71 } | 69 } |
72 | 70 |
73 inline ResourcePtrBase::ResourcePtrBase(const ResourcePtrBase& o) | 71 inline ResourcePtrBase::ResourcePtrBase(const ResourcePtrBase& o) |
74 : m_resource(o.m_resource) | 72 : m_resource(o.m_resource) |
75 { | 73 { |
76 if (m_resource) | 74 if (m_resource) |
77 m_resource->registerHandle(this); | 75 m_resource->registerHandle(this); |
78 } | 76 } |
79 | 77 |
80 template <class R> class ResourcePtr : public ResourcePtrBase { | 78 template <class R> class ResourcePtr final : public ResourcePtrBase { |
81 public: | 79 public: |
82 ResourcePtr() { } | 80 ResourcePtr() { } |
83 ResourcePtr(R* res) : ResourcePtrBase(res) { } | 81 ResourcePtr(R* res) : ResourcePtrBase(res) { } |
84 ResourcePtr(const ResourcePtr<R>& o) : ResourcePtrBase(o) { } | 82 ResourcePtr(const ResourcePtr<R>& o) : ResourcePtrBase(o) { } |
85 template<typename U> ResourcePtr(const ResourcePtr<U>& o) : ResourcePtrBase(
o.get()) { } | 83 template<typename U> ResourcePtr(const ResourcePtr<U>& o) : ResourcePtrBase(
cast(o.get())) { } |
86 | 84 |
87 R* get() const { return reinterpret_cast<R*>(ResourcePtrBase::get()); } | 85 R* get() const { return static_cast<R*>(ResourcePtrBase::get()); } |
88 R* operator->() const { return get(); } | 86 R* operator->() const { return get(); } |
89 | 87 |
90 ResourcePtr& operator=(R* res) { setResource(res); return *this; } | 88 ResourcePtr& operator=(R* res) { setResource(res); return *this; } |
91 ResourcePtr& operator=(const ResourcePtr& o) { setResource(o.get()); return
*this; } | 89 ResourcePtr& operator=(const ResourcePtr& o) { setResource(o.get()); return
*this; } |
92 template<typename U> ResourcePtr& operator=(const ResourcePtr<U>& o) { setRe
source(o.get()); return *this; } | 90 template<typename U> ResourcePtr& operator=(const ResourcePtr<U>& o) { setRe
source(cast(o.get())); return *this; } |
93 | 91 |
94 bool operator==(const ResourcePtrBase& o) const { return get() == o.get(); } | 92 bool operator==(const ResourcePtrBase& o) const { return get() == o.get(); } |
95 bool operator!=(const ResourcePtrBase& o) const { return get() != o.get(); } | 93 bool operator!=(const ResourcePtrBase& o) const { return get() != o.get(); } |
| 94 private: |
| 95 template<typename U> static R* cast(U* u) { return u; } |
96 }; | 96 }; |
97 | 97 |
98 template <class R, class RR> bool operator==(const ResourcePtr<R>& h, const RR*
res) | 98 template <class R, class RR> bool operator==(const ResourcePtr<R>& h, const RR*
res) |
99 { | 99 { |
100 return h.get() == res; | 100 return h.get() == res; |
101 } | 101 } |
102 template <class R, class RR> bool operator==(const RR* res, const ResourcePtr<R>
& h) | 102 template <class R, class RR> bool operator==(const RR* res, const ResourcePtr<R>
& h) |
103 { | 103 { |
104 return h.get() == res; | 104 return h.get() == res; |
105 } | 105 } |
106 template <class R, class RR> bool operator!=(const ResourcePtr<R>& h, const RR*
res) | 106 template <class R, class RR> bool operator!=(const ResourcePtr<R>& h, const RR*
res) |
107 { | 107 { |
108 return h.get() != res; | 108 return h.get() != res; |
109 } | 109 } |
110 template <class R, class RR> bool operator!=(const RR* res, const ResourcePtr<R>
& h) | 110 template <class R, class RR> bool operator!=(const RR* res, const ResourcePtr<R>
& h) |
111 { | 111 { |
112 return h.get() != res; | 112 return h.get() != res; |
113 } | 113 } |
114 } | 114 } |
115 | 115 |
116 #endif | 116 #endif |
OLD | NEW |