/src/mozilla-central/parser/html/nsHtml5StreamParserPtr.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #ifndef nsHtml5StreamParserPtr_h |
7 | | #define nsHtml5StreamParserPtr_h |
8 | | |
9 | | #include "nsHtml5StreamParser.h" |
10 | | #include "nsThreadUtils.h" |
11 | | #include "mozilla/dom/DocGroup.h" |
12 | | |
13 | | class nsHtml5StreamParserReleaser : public mozilla::Runnable |
14 | | { |
15 | | private: |
16 | | nsHtml5StreamParser* mPtr; |
17 | | |
18 | | public: |
19 | | explicit nsHtml5StreamParserReleaser(nsHtml5StreamParser* aPtr) |
20 | | : mozilla::Runnable("nsHtml5StreamParserReleaser") |
21 | | , mPtr(aPtr) |
22 | 0 | { |
23 | 0 | } |
24 | | NS_IMETHOD Run() override |
25 | 0 | { |
26 | 0 | mPtr->Release(); |
27 | 0 | return NS_OK; |
28 | 0 | } |
29 | | }; |
30 | | |
31 | | /** |
32 | | * Like nsRefPtr except release is proxied to the main |
33 | | * thread. Mostly copied from nsRefPtr. |
34 | | */ |
35 | | class nsHtml5StreamParserPtr |
36 | | { |
37 | | private: |
38 | | void assign_with_AddRef(nsHtml5StreamParser* rawPtr) |
39 | 0 | { |
40 | 0 | if (rawPtr) |
41 | 0 | rawPtr->AddRef(); |
42 | 0 | assign_assuming_AddRef(rawPtr); |
43 | 0 | } |
44 | | void** begin_assignment() |
45 | 0 | { |
46 | 0 | assign_assuming_AddRef(0); |
47 | 0 | return reinterpret_cast<void**>(&mRawPtr); |
48 | 0 | } |
49 | | void assign_assuming_AddRef(nsHtml5StreamParser* newPtr) |
50 | 0 | { |
51 | 0 | nsHtml5StreamParser* oldPtr = mRawPtr; |
52 | 0 | mRawPtr = newPtr; |
53 | 0 | if (oldPtr) |
54 | 0 | release(oldPtr); |
55 | 0 | } |
56 | | void release(nsHtml5StreamParser* aPtr) |
57 | 0 | { |
58 | 0 | nsCOMPtr<nsIRunnable> releaser = new nsHtml5StreamParserReleaser(aPtr); |
59 | 0 | if (NS_FAILED(aPtr->DispatchToMain(releaser.forget()))) { |
60 | 0 | NS_WARNING("Failed to dispatch releaser event."); |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | private: |
65 | | nsHtml5StreamParser* mRawPtr; |
66 | | |
67 | | public: |
68 | | ~nsHtml5StreamParserPtr() |
69 | 0 | { |
70 | 0 | if (mRawPtr) |
71 | 0 | release(mRawPtr); |
72 | 0 | } |
73 | | // Constructors |
74 | | nsHtml5StreamParserPtr() |
75 | | : mRawPtr(0) |
76 | | // default constructor |
77 | 0 | { |
78 | 0 | } |
79 | | nsHtml5StreamParserPtr(const nsHtml5StreamParserPtr& aSmartPtr) |
80 | | : mRawPtr(aSmartPtr.mRawPtr) |
81 | | // copy-constructor |
82 | 0 | { |
83 | 0 | if (mRawPtr) |
84 | 0 | mRawPtr->AddRef(); |
85 | 0 | } |
86 | | explicit nsHtml5StreamParserPtr(nsHtml5StreamParser* aRawPtr) |
87 | | : mRawPtr(aRawPtr) |
88 | | // construct from a raw pointer (of the right type) |
89 | 0 | { |
90 | 0 | if (mRawPtr) |
91 | 0 | mRawPtr->AddRef(); |
92 | 0 | } |
93 | | // Assignment operators |
94 | | nsHtml5StreamParserPtr& operator=(const nsHtml5StreamParserPtr& rhs) |
95 | | // copy assignment operator |
96 | 0 | { |
97 | 0 | assign_with_AddRef(rhs.mRawPtr); |
98 | 0 | return *this; |
99 | 0 | } |
100 | | nsHtml5StreamParserPtr& operator=(nsHtml5StreamParser* rhs) |
101 | | // assign from a raw pointer (of the right type) |
102 | 0 | { |
103 | 0 | assign_with_AddRef(rhs); |
104 | 0 | return *this; |
105 | 0 | } |
106 | | // Other pointer operators |
107 | | void swap(nsHtml5StreamParserPtr& rhs) |
108 | | // ...exchange ownership with |rhs|; can save a pair of refcount operations |
109 | 0 | { |
110 | 0 | nsHtml5StreamParser* temp = rhs.mRawPtr; |
111 | 0 | rhs.mRawPtr = mRawPtr; |
112 | 0 | mRawPtr = temp; |
113 | 0 | } |
114 | | void swap(nsHtml5StreamParser*& rhs) |
115 | | // ...exchange ownership with |rhs|; can save a pair of refcount operations |
116 | 0 | { |
117 | 0 | nsHtml5StreamParser* temp = rhs; |
118 | 0 | rhs = mRawPtr; |
119 | 0 | mRawPtr = temp; |
120 | 0 | } |
121 | | template<typename I> |
122 | | void forget(I** rhs) |
123 | | // Set the target of rhs to the value of mRawPtr and null out mRawPtr. |
124 | | // Useful to avoid unnecessary AddRef/Release pairs with "out" |
125 | | // parameters where rhs bay be a T** or an I** where I is a base class |
126 | | // of T. |
127 | | { |
128 | | NS_ASSERTION(rhs, "Null pointer passed to forget!"); |
129 | | *rhs = mRawPtr; |
130 | | mRawPtr = 0; |
131 | | } |
132 | | nsHtml5StreamParser* get() const |
133 | | /* |
134 | | Prefer the implicit conversion provided automatically by |operator |
135 | | nsHtml5StreamParser*() const|. Use |get()| to resolve ambiguity or to get a |
136 | | castable pointer. |
137 | | */ |
138 | 0 | { |
139 | 0 | return const_cast<nsHtml5StreamParser*>(mRawPtr); |
140 | 0 | } |
141 | | operator nsHtml5StreamParser*() const |
142 | | /* |
143 | | ...makes an |nsHtml5StreamParserPtr| act like its underlying raw |
144 | | pointer type whenever it is used in a context where a raw pointer is |
145 | | expected. It is this operator that makes an |nsHtml5StreamParserPtr| |
146 | | substitutable for a raw pointer. Prefer the implicit use of this operator |
147 | | to calling |get()|, except where necessary to resolve ambiguity. |
148 | | */ |
149 | 0 | { |
150 | 0 | return get(); |
151 | 0 | } |
152 | | nsHtml5StreamParser* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN |
153 | 0 | { |
154 | 0 | MOZ_ASSERT( |
155 | 0 | mRawPtr != 0, |
156 | 0 | "You can't dereference a NULL nsHtml5StreamParserPtr with operator->()."); |
157 | 0 | return get(); |
158 | 0 | } |
159 | | nsHtml5StreamParserPtr* get_address() |
160 | | // This is not intended to be used by clients. See |address_of| |
161 | | // below. |
162 | 0 | { |
163 | 0 | return this; |
164 | 0 | } |
165 | | const nsHtml5StreamParserPtr* get_address() const |
166 | | // This is not intended to be used by clients. See |address_of| |
167 | | // below. |
168 | 0 | { |
169 | 0 | return this; |
170 | 0 | } |
171 | | |
172 | | public: |
173 | | nsHtml5StreamParser& operator*() const |
174 | 0 | { |
175 | 0 | MOZ_ASSERT( |
176 | 0 | mRawPtr != 0, |
177 | 0 | "You can't dereference a NULL nsHtml5StreamParserPtr with operator*()."); |
178 | 0 | return *get(); |
179 | 0 | } |
180 | | nsHtml5StreamParser** StartAssignment() |
181 | 0 | { |
182 | 0 | #ifndef NSCAP_FEATURE_INLINE_STARTASSIGNMENT |
183 | 0 | return reinterpret_cast<nsHtml5StreamParser**>(begin_assignment()); |
184 | 0 | #else |
185 | 0 | assign_assuming_AddRef(0); |
186 | 0 | return reinterpret_cast<nsHtml5StreamParser**>(&mRawPtr); |
187 | 0 | #endif |
188 | 0 | } |
189 | | }; |
190 | | |
191 | | inline nsHtml5StreamParserPtr* |
192 | | address_of(nsHtml5StreamParserPtr& aPtr) |
193 | 0 | { |
194 | 0 | return aPtr.get_address(); |
195 | 0 | } |
196 | | |
197 | | inline const nsHtml5StreamParserPtr* |
198 | | address_of(const nsHtml5StreamParserPtr& aPtr) |
199 | 0 | { |
200 | 0 | return aPtr.get_address(); |
201 | 0 | } |
202 | | |
203 | | class nsHtml5StreamParserPtrGetterAddRefs |
204 | | /* |
205 | | ... |
206 | | This class is designed to be used for anonymous temporary objects in the |
207 | | argument list of calls that return COM interface pointers, e.g., |
208 | | nsHtml5StreamParserPtr<IFoo> fooP; |
209 | | ...->GetAddRefedPointer(getter_AddRefs(fooP)) |
210 | | DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_AddRefs()| |
211 | | instead. When initialized with a |nsHtml5StreamParserPtr|, as in the example |
212 | | above, it returns a |void**|, a |T**|, or an |nsISupports**| as needed, that |
213 | | the outer call (|GetAddRefedPointer| in this case) can fill in. This type |
214 | | should be a nested class inside |nsHtml5StreamParserPtr<T>|. |
215 | | */ |
216 | | { |
217 | | public: |
218 | | explicit nsHtml5StreamParserPtrGetterAddRefs( |
219 | | nsHtml5StreamParserPtr& aSmartPtr) |
220 | | : mTargetSmartPtr(aSmartPtr) |
221 | 0 | { |
222 | 0 | // nothing else to do |
223 | 0 | } |
224 | | operator void**() |
225 | 0 | { |
226 | 0 | return reinterpret_cast<void**>(mTargetSmartPtr.StartAssignment()); |
227 | 0 | } |
228 | 0 | operator nsHtml5StreamParser**() { return mTargetSmartPtr.StartAssignment(); } |
229 | | nsHtml5StreamParser*& operator*() |
230 | 0 | { |
231 | 0 | return *(mTargetSmartPtr.StartAssignment()); |
232 | 0 | } |
233 | | |
234 | | private: |
235 | | nsHtml5StreamParserPtr& mTargetSmartPtr; |
236 | | }; |
237 | | |
238 | | inline nsHtml5StreamParserPtrGetterAddRefs |
239 | | getter_AddRefs(nsHtml5StreamParserPtr& aSmartPtr) |
240 | | /* |
241 | | Used around a |nsHtml5StreamParserPtr| when |
242 | | ...makes the class |nsHtml5StreamParserPtrGetterAddRefs| invisible. |
243 | | */ |
244 | 0 | { |
245 | 0 | return nsHtml5StreamParserPtrGetterAddRefs(aSmartPtr); |
246 | 0 | } |
247 | | |
248 | | // Comparing an |nsHtml5StreamParserPtr| to |0| |
249 | | |
250 | | inline bool |
251 | | operator==(const nsHtml5StreamParserPtr& lhs, decltype(nullptr)) |
252 | 0 | { |
253 | 0 | return lhs.get() == nullptr; |
254 | 0 | } |
255 | | |
256 | | inline bool |
257 | | operator==(decltype(nullptr), const nsHtml5StreamParserPtr& rhs) |
258 | 0 | { |
259 | 0 | return nullptr == rhs.get(); |
260 | 0 | } |
261 | | |
262 | | inline bool |
263 | | operator!=(const nsHtml5StreamParserPtr& lhs, decltype(nullptr)) |
264 | 0 | { |
265 | 0 | return lhs.get() != nullptr; |
266 | 0 | } |
267 | | |
268 | | inline bool |
269 | | operator!=(decltype(nullptr), const nsHtml5StreamParserPtr& rhs) |
270 | 0 | { |
271 | 0 | return nullptr != rhs.get(); |
272 | 0 | } |
273 | | |
274 | | #endif // !defined(nsHtml5StreamParserPtr_h) |