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

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

Issue 169343002: [WIP]Add WebPrivateHeapPtr and use it for speech platform layer. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: 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 /*
2 * Copyright (c) 2014, Opera Software ASA. All rights reserved.
haraken 2014/02/17 09:04:57 Can you use the 4-line copyright?
3 *
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.
12 * 3. Neither the name of Opera Software ASA nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef WebPrivateHeapPtr_h
31 #define WebPrivateHeapPtr_h
32
33 #include "WebCommon.h"
34
35 #if INSIDE_BLINK
haraken 2014/02/17 09:04:57 What's the difference between INSIDE_BLINK and BLI
sof 2014/02/17 10:22:26 Good question; I can't discern a pattern within th
tkent 2014/02/17 23:42:46 INSIDE_BLINK means that it's in either of webkit.d
sof 2014/02/18 10:14:17 Excellent clarification & info, many thanks :) On
36 #include "heap/Handle.h"
37 #include "wtf/PassRefPtr.h"
38 #endif
39
40 namespace blink {
41
42 // A version of WebPrivatePtr that holds a heap-residing object,
43 // but without exposing how that is done within Blink. If no
44 // heap support (i.e., no Oilpan), WebPrivateHeapPtr is equal
45 // to WebPrivatePtr and the object will be reference counted.
haraken 2014/02/17 09:04:57 "heap" is ambiguous. We want to use "garbage-colle
46 //
47 // See WebPrivatePtr.h comment for how to make use of WebPrivateHeapPtr;
48 // same pattern applies, but for one detail. Instead of declaring & defining,
49 //
50 // #if INSIDE_BLINK
51 // WebFoo(const WTF::PassRefPtr<WebCore::Foo>&);
52 // #endif
53 //
54 // provide
55 //
56 // #if INSIDE_BLINK
57 // WebFoo(const WTF::PassRefPtrWillBeRawPtr<WebCore::Foo>&);
58 // #endif
59 //
60 template <typename T>
61 class WebPrivateHeapPtr {
62 public:
63 WebPrivateHeapPtr() : m_ptr(0) { }
64 ~WebPrivateHeapPtr()
65 {
66 // We don't destruct the object pointed by m_ptr here because we don't
67 // want to expose destructors of core classes to embedders. We should
68 // call reset() manually in destructors of classes with WebPrivateHeapPt r
69 // members.
70 BLINK_ASSERT(!m_ptr);
71 }
72
73 bool isNull() const { return !m_ptr; }
74
75 #if INSIDE_BLINK
76 WebPrivateHeapPtr(const PassRefPtrWillBeRawPtr<T>& p)
77 # if !ENABLE(OILPAN)
78 : m_ptr(p.leakRef())
79 # endif
80 {
81 # if ENABLE(OILPAN)
82 assign(new WebCore::Persistent<T>(p));
haraken 2014/02/17 09:04:57 How about calling 'new Persistent' in assign? assi
sof 2014/02/17 09:23:21 Yes, let's do that.
83 # endif
84 }
85
86 void reset()
87 {
88 # if ENABLE(OILPAN)
89 if (this->m_ptr) {
90 delete reinterpret_cast<WebCore::Persistent<T>*>(this->m_ptr);
91 this->m_ptr = 0;
92 }
93 # else
94 assign(0);
95 # endif
96 }
97
98 WebPrivateHeapPtr<T>& operator=(const WebPrivateHeapPtr<T>& other)
99 {
100 # if ENABLE(OILPAN)
101 assign(new WebCore::Persistent<T>(other.get()));
102 # else
103 T* p = other.m_ptr;
104 if (p)
105 p->ref();
106 assign(p);
107 # endif
108 return *this;
109 }
110
111 WebPrivateHeapPtr<T>& operator=(WebPrivateHeapPtr<T>& other)
112 {
113 assign(new WebCore::Persistent<T>(other.get()));
114 return *this;
115 }
116
117 WebPrivateHeapPtr<T>& operator=(const PassRefPtrWillBeRawPtr<T>& p)
118 {
119 # if ENABLE(OILPAN)
120 assign(new WebCore::Persistent<T>(p));
121 # else
122 assign(p.leakRef());
123 # endif
124 return *this;
125 }
126
127 T* get() const
128 {
129 # if ENABLE(OILPAN)
130 if (!m_ptr)
131 return 0;
132 return (reinterpret_cast<WebCore::Persistent<T> * >(m_ptr))->get();
133 # else
134 return m_ptr;
135 # endif
136 }
137
138 T* operator->() const
139 {
140 ASSERT(m_ptr);
141 # if ENABLE(OILPAN)
142 return (reinterpret_cast<WebCore::Persistent<T> * >(m_ptr))->get();
143 # else
144 return m_ptr;
145 # endif
146 }
147 #endif
148
149 private:
150 #if INSIDE_BLINK
151 # if ENABLE(OILPAN)
152 void assign(WebCore::Persistent<T>* p)
153 {
154 reset();
155 m_ptr = reinterpret_cast<T*>(p);
156 }
157 # else
158 void assign(T* p)
159 {
160 // p is already ref'd for us by the caller
161 if (m_ptr)
162 m_ptr->deref();
163 m_ptr = p;
164 }
165 # endif
166 #else
167 // Disable the assignment operator; we define it above for when
168 // INSIDE_BLINK is set, but we need to make sure that it is not
169 // used outside there; the compiler-provided version won't handle reference
170 // counting properly.
171 WebPrivateHeapPtr<T>& operator=(const WebPrivateHeapPtr<T>& other);
172 #endif
173 // Disable the copy constructor; classes that contain a WebPrivateHeapPtr
174 // should implement their copy constructor using assign().
175 WebPrivateHeapPtr(const WebPrivateHeapPtr<T>&);
176
177 T* m_ptr;
178 };
179
180 } // namespace blink
181
182 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698