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

Side by Side Diff: public/platform/WebPrivatePtr.h

Issue 169653006: [not for commit] (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: replace = 0 with = nullptr Created 6 years, 10 months 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 | Annotate | Revision Log
« no previous file with comments | « Source/web/WebIDBKeyRange.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) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 16 matching lines...) Expand all
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #ifndef WebPrivatePtr_h 31 #ifndef WebPrivatePtr_h
32 #define WebPrivatePtr_h 32 #define WebPrivatePtr_h
33 33
34 #include "WebCommon.h" 34 #include "WebCommon.h"
35 35
36 #if INSIDE_BLINK 36 #if INSIDE_BLINK
37 #include "heap/Handle.h"
37 #include "wtf/PassRefPtr.h" 38 #include "wtf/PassRefPtr.h"
39 #include "wtf/TypeTraits.h"
38 #endif 40 #endif
39 41
40 namespace blink { 42 namespace blink {
41 43
44 #if INSIDE_BLINK
45 enum LifetimeManagementType {
46 RefCountedLifetime,
47 GarbageCollectedLifetime,
48 RefCountedGarbageCollectedLifetime
49 };
50
51 template<typename T>
52 class LifetimeOf {
53 static const bool isGarbageCollected = WTF::IsSubclassOfTemplate<T, WebCore: :GarbageCollected>::value;
54 static const bool isRefCountedGarbageCollected = WTF::IsSubclassOfTemplate<T , WebCore::RefCountedGarbageCollected>::value;
55 public:
56 static const LifetimeManagementType value =
57 !isGarbageCollected ? RefCountedLifetime :
58 isRefCountedGarbageCollected ? RefCountedGarbageCollectedLifetime : Garb ageCollectedLifetime;
59 };
60
61 template<typename T, LifetimeManagementType lifetime>
62 class PtrStorageImpl;
63
64 template<typename T>
65 class PtrStorageImpl<T, RefCountedLifetime> {
66 public:
67 typedef PassRefPtr<T> BlinkPtrType;
68
69 void assign(const BlinkPtrType& val)
70 {
71 release();
72 m_ptr = val.leakRef();
73 }
74
75 void assign(const PtrStorageImpl& other)
76 {
77 release();
78 T* val = other.get();
79 if (val)
80 val->ref();
81 m_ptr = val;
82 }
83
84 T* get() const { return m_ptr; }
85
86 void release()
87 {
88 if (m_ptr)
89 m_ptr->deref();
90 m_ptr = 0;
91 }
92
93 private:
94 T* m_ptr;
95 };
96
97 template<typename T>
98 class PtrStorageImpl<T, GarbageCollectedLifetime> {
99 public:
100 void assign(const RawPtr<T>& val)
101 {
102 if (!val) {
103 release();
104 return;
105 }
106
107 if (!m_handle)
108 m_handle = new WebCore::Persistent<T>();
109
110 (*m_handle) = val;
111 }
112
113 void assign(T* ptr) { assign(RawPtr<T>(ptr)); }
114
115 void assign(const PtrStorageImpl& other) { assign(other.get()); }
116
117 T* get() const { return m_handle ? m_handle->get() : 0; }
118
119 void release()
120 {
121 delete m_handle;
122 m_handle = 0;
123 }
124
125 private:
126 WebCore::Persistent<T>* m_handle;
127 };
128
129 template<typename T>
130 class PtrStorageImpl<T, RefCountedGarbageCollectedLifetime> : public PtrStorageI mpl<T, GarbageCollectedLifetime> {
131 public:
132 void assign(const PassRefPtr<T>& val) { assign(val.get()); }
sof 2014/02/18 11:24:20 I'm seeing this cause infinite recursion when runn
Vyacheslav Egorov (Chromium) 2014/02/18 12:02:33 Fixed & verified.
sof 2014/02/18 12:58:57 It still crashes due to infinite regress for me, I
133
134 void assign(const PtrStorageImpl& other) { assign(other.get()); }
135 };
136
137 template<typename T>
138 class PtrStorage : public PtrStorageImpl<T, LifetimeOf<T>::value> {
139 public:
140 static PtrStorage& fromSlot(void** slot)
141 {
142 COMPILE_ASSERT(sizeof(PtrStorage) == sizeof(void*), PtrStorage_must_be_p ointer_size);
143 return *reinterpret_cast<PtrStorage*>(slot);
144 }
145
146 static const PtrStorage& fromSlot(void* const* slot)
147 {
148 COMPILE_ASSERT(sizeof(PtrStorage) == sizeof(void*), PtrStorage_must_be_p ointer_size);
149 return *reinterpret_cast<const PtrStorage*>(slot);
150 }
151
152 private:
153 // Prevent construction via normal means.
154 PtrStorage();
155 PtrStorage(const PtrStorage&);
156 };
157 #endif
158
42 // This class is an implementation detail of the Blink API. It exists to help 159 // This class is an implementation detail of the Blink API. It exists to help
43 // simplify the implementation of Blink interfaces that merely wrap a reference 160 // simplify the implementation of Blink interfaces that merely wrap a reference
44 // counted WebCore class. 161 // counted WebCore class.
45 // 162 //
46 // A typical implementation of a class which uses WebPrivatePtr might look like 163 // A typical implementation of a class which uses WebPrivatePtr might look like
47 // this: 164 // this:
48 // class WebFoo { 165 // class WebFoo {
49 // public: 166 // public:
50 // BLINK_EXPORT ~WebFoo(); 167 // BLINK_EXPORT ~WebFoo();
51 // WebFoo() { } 168 // WebFoo() { }
(...skipping 19 matching lines...) Expand all
71 // WebPrivatePtr<WebCore::Foo> m_private; 188 // WebPrivatePtr<WebCore::Foo> m_private;
72 // }; 189 // };
73 // 190 //
74 // // WebFoo.cpp 191 // // WebFoo.cpp
75 // WebFoo::~WebFoo() { m_private.reset(); } 192 // WebFoo::~WebFoo() { m_private.reset(); }
76 // void WebFoo::assign(const WebFoo& other) { ... } 193 // void WebFoo::assign(const WebFoo& other) { ... }
77 // 194 //
78 template <typename T> 195 template <typename T>
79 class WebPrivatePtr { 196 class WebPrivatePtr {
80 public: 197 public:
81 WebPrivatePtr() : m_ptr(0) { } 198 WebPrivatePtr() : m_storage(0) { }
82 ~WebPrivatePtr() 199 ~WebPrivatePtr()
83 { 200 {
84 // We don't destruct the object pointed by m_ptr here because we don't 201 // We don't destruct the object pointed by m_ptr here because we don't
85 // want to expose destructors of core classes to embedders. We should 202 // want to expose destructors of core classes to embedders. We should
86 // call reset() manually in destructors of classes with WebPrivatePtr 203 // call reset() manually in destructors of classes with WebPrivatePtr
87 // members. 204 // members.
88 BLINK_ASSERT(!m_ptr); 205 BLINK_ASSERT(!m_storage);
89 } 206 }
90 207
91 bool isNull() const { return !m_ptr; } 208 bool isNull() const { return !m_storage; }
92 209
93 #if INSIDE_BLINK 210 #if INSIDE_BLINK
94 WebPrivatePtr(const PassRefPtr<T>& prp) 211 template<typename U>
95 : m_ptr(prp.leakRef()) 212 WebPrivatePtr(const U& ptr)
213 : m_storage(0)
96 { 214 {
215 storage().assign(ptr);
97 } 216 }
98 217
99 void reset() 218 void reset() { storage().release(); }
100 {
101 assign(0);
102 }
103 219
104 WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other) 220 WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other)
105 { 221 {
106 T* p = other.m_ptr; 222 storage().assign(other.storage());
107 if (p)
108 p->ref();
109 assign(p);
110 return *this; 223 return *this;
111 } 224 }
112 225
113 WebPrivatePtr<T>& operator=(const PassRefPtr<T>& prp) 226 WebPrivatePtr<T>& operator=(std::nullptr_t&)
114 { 227 {
115 assign(prp.leakRef()); 228 storage().reset();
sof 2014/02/18 10:36:56 Shouldn't this be reset() (or storage.release()) ?
Vyacheslav Egorov (Chromium) 2014/02/18 12:02:33 Killed this overload. It is not actually selected
sof 2014/02/18 12:07:40 I don't understand. Aren't we making use of nullpt
Vyacheslav Egorov (Chromium) 2014/02/18 12:18:17 It can just hit the normal assignment operator and
116 return *this; 229 return *this;
117 } 230 }
118 231
119 T* get() const 232 template<typename U>
233 WebPrivatePtr<T>& operator=(const U& ptr)
120 { 234 {
121 return m_ptr; 235 storage().assign(ptr);
236 return *this;
122 } 237 }
123 238
239 T* get() const { return storage().get(); }
240
124 T* operator->() const 241 T* operator->() const
125 { 242 {
126 ASSERT(m_ptr); 243 ASSERT(m_storage);
127 return m_ptr; 244 return get();
128 } 245 }
129 #endif 246 #endif
130 247
131 private: 248 private:
132 #if INSIDE_BLINK 249 #if INSIDE_BLINK
133 void assign(T* p) 250 PtrStorage<T>& storage() { return PtrStorage<T>::fromSlot(&m_storage); }
134 { 251 const PtrStorage<T>& storage() const { return PtrStorage<T>::fromSlot(&m_sto rage); }
135 // p is already ref'd for us by the caller 252 #endif
136 if (m_ptr) 253
137 m_ptr->deref(); 254 #if !INSIDE_BLINK
138 m_ptr = p;
139 }
140 #else
141 // Disable the assignment operator; we define it above for when 255 // Disable the assignment operator; we define it above for when
142 // INSIDE_BLINK is set, but we need to make sure that it is not 256 // INSIDE_BLINK is set, but we need to make sure that it is not
143 // used outside there; the compiler-provided version won't handle reference 257 // used outside there; the compiler-provided version won't handle reference
144 // counting properly. 258 // counting properly.
145 WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other); 259 WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other);
146 #endif 260 #endif
147 // Disable the copy constructor; classes that contain a WebPrivatePtr 261 // Disable the copy constructor; classes that contain a WebPrivatePtr
148 // should implement their copy constructor using assign(). 262 // should implement their copy constructor using assign().
149 WebPrivatePtr(const WebPrivatePtr<T>&); 263 WebPrivatePtr(const WebPrivatePtr<T>&);
150 264
151 T* m_ptr; 265 void* m_storage;
152 }; 266 };
153 267
154 } // namespace blink 268 } // namespace blink
155 269
156 #endif 270 #endif
OLDNEW
« no previous file with comments | « Source/web/WebIDBKeyRange.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698