Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / fetch / ResourcePtr.h
blob2f21fb6ad1725fdfda00ff403f0d0e78bf681f1e
1 /*
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
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.
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.
26 #ifndef ResourcePtr_h
27 #define ResourcePtr_h
29 #include "core/CoreExport.h"
30 #include "core/fetch/Resource.h"
31 #include "wtf/Allocator.h"
33 namespace blink {
35 class CORE_EXPORT ResourcePtrBase {
36 ALLOW_ONLY_INLINE_ALLOCATION();
37 public:
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; }
46 protected:
47 ResourcePtrBase() : m_resource(nullptr) { }
48 explicit ResourcePtrBase(Resource*);
49 explicit ResourcePtrBase(const ResourcePtrBase&);
50 ~ResourcePtrBase();
52 void setResource(Resource*);
54 private:
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")
61 Resource* m_resource;
64 inline ResourcePtrBase::ResourcePtrBase(Resource* res)
65 : m_resource(res)
67 if (m_resource)
68 m_resource->registerHandle(this);
71 inline ResourcePtrBase::~ResourcePtrBase()
73 if (m_resource)
74 m_resource->unregisterHandle(this);
77 inline ResourcePtrBase::ResourcePtrBase(const ResourcePtrBase& o)
78 : m_resource(o.m_resource)
80 if (m_resource)
81 m_resource->registerHandle(this);
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())) { }
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(); }
100 private:
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;
122 #endif