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

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

Issue 168963003: Make WebPrivatePtr capable of wrapping garbage collected objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Update transition type comment 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
OLDNEW
(Empty)
1 /* Copyright (c) 2014 The Chromium Authors. All rihts reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #ifndef WebWillBePrivateGarbageCollectedPtr_h
7 #define WebWillBePrivateGarbageCollectedPtr_h
8
9 #include "WebCommon.h"
10
11 #if BLINK_IMPLEMENTATION
12 #include "heap/Handle.h"
13 #include "wtf/PassRefPtr.h"
14 #endif
15
16 namespace blink {
17
18 // A variant of WebPrivatePtr that holds a Blink object that's garbage
19 // collected, without exposing the details of how (and with what
20 // types.) If no support for having such objects (i.e., Oilpan not
21 // enabled), WebWillBePrivateGarbageCollectedPtr is equal to
22 // WebPrivatePtr and the object will be reference counted.
23 //
24 // See WebPrivatePtr.h comment for how to make use of
25 // WebWillBePrivateGarbageCollectedPtr; same pattern applies, but for
26 // one detail. Instead of declaring & defining,
27 //
28 // #if BLINK_IMPLEMENTATION
29 // WebFoo(const WTF::PassRefPtr<WebCore::Foo>&);
30 // #endif
31 //
32 // provide
33 //
34 // #if BLINK_IMPLEMENTATION
35 // WebFoo(const PassRefPtrWillBeRawPtr<WebCore::Foo>&);
36 // #endif
37 //
38 template <typename T>
39 class WebWillBePrivateGarbageCollectedPtr {
40 public:
41 WebWillBePrivateGarbageCollectedPtr() : m_ptr(0) { }
42 ~WebWillBePrivateGarbageCollectedPtr()
43 {
44 // We don't destruct the object pointed by m_ptr here because we don't
45 // want to expose destructors of core classes to embedders. We should
46 // call reset() manually in destructors of classes with WebWillBePrivate GarbageCollectedPtr
47 // members.
48 BLINK_ASSERT(!m_ptr);
49 }
50
51 bool isNull() const { return !m_ptr; }
52
53 #if BLINK_IMPLEMENTATION
54 #if ENABLE(OILPAN)
55 WebWillBePrivateGarbageCollectedPtr(const PassRefPtrWillBeRawPtr<T>& p)
56 {
57 assign(p);
58 }
59
60 void reset()
61 {
62 if (m_ptr) {
63 delete reinterpret_cast<WebCore::Persistent<T>*>(m_ptr);
64 m_ptr = 0;
65 }
66 }
67
68 WebWillBePrivateGarbageCollectedPtr<T>& operator=(const WebWillBePrivateGarb ageCollectedPtr<T>& other)
69 {
70 assign(other.get());
71 return *this;
72 }
73
74 WebWillBePrivateGarbageCollectedPtr<T>& operator=(const PassRefPtrWillBeRawP tr<T>& p)
75 {
76 assign(p);
77 return *this;
78 }
79
80 T* get() const
81 {
82 if (!m_ptr)
83 return 0;
84 return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get();
85 }
86
87 T* operator->() const
88 {
89 ASSERT(m_ptr);
90 return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get();
91 }
92 #else
93 WebWillBePrivateGarbageCollectedPtr(const PassRefPtrWillBeRawPtr<T>& p)
94 {
95 m_ptr = p.leakRef();
96 }
97
98 void reset()
99 {
100 assign(0);
101 }
102
103 WebWillBePrivateGarbageCollectedPtr<T>& operator=(const WebWillBePrivateGarb ageCollectedPtr<T>& other)
104 {
105 T* p = other.m_ptr;
106 if (p)
107 p->ref();
108 assign(p);
109 return *this;
110 }
111
112 WebWillBePrivateGarbageCollectedPtr<T>& operator=(const PassRefPtrWillBeRawP tr<T>& p)
113 {
114 assign(p.leakRef());
115 return *this;
116 }
117
118 T* get() const
119 {
120 return m_ptr;
121 }
122
123 T* operator->() const
124 {
125 ASSERT(m_ptr);
126 return m_ptr;
127 }
128 #endif
129 #endif
130
131 private:
132 #if BLINK_IMPLEMENTATION
133 #if ENABLE(OILPAN)
134 void assign(T* p)
135 {
136 if (!p) {
137 reset();
138 return;
139 }
140 // Keep an opaque object pointer to avoid exposing
141 // representation details.
142 if (!m_ptr)
143 m_ptr = reinterpret_cast<T*>(new WebCore::Persistent<T>(p));
144 else
145 *reinterpret_cast<WebCore::Persistent<T>*>(m_ptr) = p;
146 }
147 #else
148 void assign(T* p)
149 {
150 // p is already ref'd for us by the caller
151 if (m_ptr)
152 m_ptr->deref();
153 m_ptr = p;
154 }
155 #endif
156 #else
157 // Disable the assignment operator; we define it above for when
158 // BLINK_IMPLEMENTATION is set, but we need to make sure that it is not
159 // used outside there; the compiler-provided version won't handle reference
160 // counting properly.
161 WebWillBePrivateGarbageCollectedPtr<T>& operator=(const WebWillBePrivateGarb ageCollectedPtr<T>& other);
162 #endif
163 // Disable the copy constructor; classes that contain a
164 // WebPrivateGarbageCollectedPtr should implement their copy
165 // constructor using assign().
166 WebWillBePrivateGarbageCollectedPtr(const WebWillBePrivateGarbageCollectedPt r<T>&);
167
168 T* m_ptr;
169 };
170
171 } // namespace blink
172
173 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698