Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: Source/core/fetch/ResourcePtr.h

Issue 720693002: ResourcePtr refactoring (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/fetch/ResourceFetcherTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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;
Nate Chapin 2014/11/12 17:31:28 I'm not familiar with this syntax, I assume it's n
dcheng 2014/11/12 18:31:15 It's an error to call a deleted function, so this
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) : ResourcePtr(o.ge t()) { }
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) { R* r = o.get(); setResource(r); 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(); }
96 }; 94 };
97 95
98 template <class R, class RR> bool operator==(const ResourcePtr<R>& h, const RR* res) 96 template <class R, class RR> bool operator==(const ResourcePtr<R>& h, const RR* res)
99 { 97 {
100 return h.get() == res; 98 return h.get() == res;
101 } 99 }
102 template <class R, class RR> bool operator==(const RR* res, const ResourcePtr<R> & h) 100 template <class R, class RR> bool operator==(const RR* res, const ResourcePtr<R> & h)
103 { 101 {
104 return h.get() == res; 102 return h.get() == res;
105 } 103 }
106 template <class R, class RR> bool operator!=(const ResourcePtr<R>& h, const RR* res) 104 template <class R, class RR> bool operator!=(const ResourcePtr<R>& h, const RR* res)
107 { 105 {
108 return h.get() != res; 106 return h.get() != res;
109 } 107 }
110 template <class R, class RR> bool operator!=(const RR* res, const ResourcePtr<R> & h) 108 template <class R, class RR> bool operator!=(const RR* res, const ResourcePtr<R> & h)
111 { 109 {
112 return h.get() != res; 110 return h.get() != res;
113 } 111 }
114 } 112 }
115 113
116 #endif 114 #endif
OLDNEW
« no previous file with comments | « Source/core/fetch/ResourceFetcherTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698