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

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

Issue 168963003: Make WebPrivatePtr capable of wrapping garbage collected objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Removed unused (and possibly overlapping) operator= overload. 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
« no previous file with comments | « Source/web/WebSpeechGrammar.cpp ('k') | public/web/WebSpeechGrammar.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 WebPrivateGarbageCollectedPtr_h
Mads Ager (chromium) 2014/02/17 12:26:29 We had a chat about this just now. We can probably
sof 2014/02/17 12:45:03 There's a slight problem with that: uses of WebPri
Mads Ager (chromium) 2014/02/17 12:54:30 Yeah, I see that now. Hmm. I'll give it a bit more
sof 2014/02/17 14:09:00 I think we get out ahead by doing so, switched ove
7 #define WebPrivateGarbageCollectedPtr_h
8
9 #include "WebCommon.h"
10
11 #if INSIDE_BLINK
12 #include "wtf/PassRefPtr.h"
13
14 namespace WebCore {
Mads Ager (chromium) 2014/02/17 13:31:31 We should be able to remove all of the forward dec
sof 2014/02/17 14:09:00 That would be cleaner, the reason why it's like th
15 template<typename T> class Persistent;
16 }
17
18 namespace WTF {
19 template<typename T> class RawPtr;
20 }
21
22 # if ENABLE(OILPAN)
Mads Ager (chromium) 2014/02/17 12:26:29 Please remove the spaces between # and if/define/e
sof 2014/02/17 14:09:00 Done.
23 # define PassRefPtrWillBeRawPtr RawPtr
24 # else
25 # define PassRefPtrWillBeRawPtr PassRefPtr
26 # endif
27 #endif
28
29 namespace blink {
30
31 // A variant of WebPrivatePtr that holds a Blink object that's garbage
32 // collected, without exposing the details of how (and with what
33 // types.) If no support for having such objects (i.e., no Oilpan),
34 // WebPrivateGarbageCollectedPtr is equal to WebPrivatePtr and the
35 // object will be reference counted.
36 //
37 // See WebPrivatePtr.h comment for how to make use of
38 // WebPrivateGarbageCollectedPtr; same pattern applies, but for one
39 // detail. Instead of declaring & defining,
40 //
41 // #if INSIDE_BLINK
42 // WebFoo(const WTF::PassRefPtr<WebCore::Foo>&);
43 // #endif
44 //
45 // provide
46 //
47 // #if INSIDE_BLINK
48 // WebFoo(const WTF::PassRefPtrWillBeRawPtr<WebCore::Foo>&);
49 // #endif
50 //
51 // FIXME: oilpan: when transition types are no more, remove the
52 // unseemly #if ENABLE(OILPAN) blocks.
53 template <typename T>
54 class WebPrivateGarbageCollectedPtr {
55 public:
56 WebPrivateGarbageCollectedPtr() : m_ptr(0) { }
57 ~WebPrivateGarbageCollectedPtr()
58 {
59 // We don't destruct the object pointed by m_ptr here because we don't
60 // want to expose destructors of core classes to embedders. We should
61 // call reset() manually in destructors of classes with WebPrivateGarbag eCollectedPtr
62 // members.
63 BLINK_ASSERT(!m_ptr);
64 }
65
66 bool isNull() const { return !m_ptr; }
67
68 #if INSIDE_BLINK
69 WebPrivateGarbageCollectedPtr(const WTF::PassRefPtrWillBeRawPtr<T>& p)
70 {
71 # if ENABLE(OILPAN)
72 assign(p);
73 # else
74 m_ptr = p.leakRef();
75 # endif
76 }
77
78 void reset()
79 {
80 # if ENABLE(OILPAN)
81 if (m_ptr) {
82 delete reinterpret_cast<WebCore::Persistent<T>*>(m_ptr);
83 m_ptr = 0;
84 }
85 # else
86 assign(0);
87 # endif
88 }
89
90 WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollected Ptr<T>& other)
91 {
92 # if ENABLE(OILPAN)
93 assign(other.get());
94 # else
95 T* p = other.m_ptr;
96 if (p)
97 p->ref();
98 assign(p);
99 # endif
100 return *this;
101 }
102
103 WebPrivateGarbageCollectedPtr<T>& operator=(const WTF::PassRefPtrWillBeRawPt r<T>& p)
104 {
105 # if ENABLE(OILPAN)
106 assign(p);
107 # else
108 assign(p.leakRef());
109 # endif
110 return *this;
111 }
112
113 T* get() const
114 {
115 # if ENABLE(OILPAN)
116 if (!m_ptr)
117 return 0;
118 return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get();
119 # else
120 return m_ptr;
121 # endif
122 }
123
124 T* operator->() const
125 {
126 ASSERT(m_ptr);
127 # if ENABLE(OILPAN)
128 return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get();
129 # else
130 return m_ptr;
131 # endif
132 }
133 #endif
134
135 private:
136 #if INSIDE_BLINK
137 void assign(T* p)
138 {
139 # if ENABLE(OILPAN)
140 reset();
141 // Keep an opaque pointer object pointer to avoid exposing
142 // representation details. (Relatively speaking, it's
143 // acceptable to have this be as a T* rather than a
144 // generic void*.
145 m_ptr = reinterpret_cast<T*>(new WebCore::Persistent<T>(p));
Mads Ager (chromium) 2014/02/17 12:26:29 We really shouldn't have to do this. We should all
Mads Ager (chromium) 2014/02/17 13:31:31 In addition to reusing the persistent we should be
sof 2014/02/17 14:09:00 Oh yes, it completely failed to consider the null
146 # else
147 // p is already ref'd for us by the caller
148 if (m_ptr)
149 m_ptr->deref();
150 m_ptr = p;
151 # endif
152 }
153 #else
154 // Disable the assignment operator; we define it above for when
155 // INSIDE_BLINK is set, but we need to make sure that it is not
156 // used outside there; the compiler-provided version won't handle reference
157 // counting properly.
158 WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollected Ptr<T>& other);
159 #endif
160 // Disable the copy constructor; classes that contain a
161 // WebPrivateGarbageCollectedPtr should implement their copy
162 // constructor using assign().
163 WebPrivateGarbageCollectedPtr(const WebPrivateGarbageCollectedPtr<T>&);
164
165 T* m_ptr;
166 };
167
168 } // namespace blink
169
170 #endif
OLDNEW
« no previous file with comments | « Source/web/WebSpeechGrammar.cpp ('k') | public/web/WebSpeechGrammar.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698