Bug 1700051: part 36) Reduce accessibility of `SoftText::mBegin` to `private`. r...
[gecko.git] / parser / html / nsHtml5StreamParserPtr.h
blob863f77d476ad160690578d5a789b5bf73cc4a007
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/. */
6 #ifndef nsHtml5StreamParserPtr_h
7 #define nsHtml5StreamParserPtr_h
9 #include "nsHtml5StreamParser.h"
10 #include "nsThreadUtils.h"
11 #include "mozilla/dom/DocGroup.h"
13 class nsHtml5StreamParserReleaser : public mozilla::Runnable {
14 private:
15 nsHtml5StreamParser* mPtr;
17 public:
18 explicit nsHtml5StreamParserReleaser(nsHtml5StreamParser* aPtr)
19 : mozilla::Runnable("nsHtml5StreamParserReleaser"), mPtr(aPtr) {}
20 NS_IMETHOD Run() override {
21 mPtr->Release();
22 return NS_OK;
26 /**
27 * Like nsRefPtr except release is proxied to the main
28 * thread. Mostly copied from nsRefPtr.
30 class nsHtml5StreamParserPtr {
31 private:
32 void assign_with_AddRef(nsHtml5StreamParser* rawPtr) {
33 if (rawPtr) rawPtr->AddRef();
34 assign_assuming_AddRef(rawPtr);
36 void** begin_assignment() {
37 assign_assuming_AddRef(0);
38 return reinterpret_cast<void**>(&mRawPtr);
40 void assign_assuming_AddRef(nsHtml5StreamParser* newPtr) {
41 nsHtml5StreamParser* oldPtr = mRawPtr;
42 mRawPtr = newPtr;
43 if (oldPtr) release(oldPtr);
45 void release(nsHtml5StreamParser* aPtr) {
46 nsCOMPtr<nsIRunnable> releaser = new nsHtml5StreamParserReleaser(aPtr);
47 if (NS_FAILED(aPtr->DispatchToMain(releaser.forget()))) {
48 NS_WARNING("Failed to dispatch releaser event.");
52 private:
53 nsHtml5StreamParser* mRawPtr;
55 public:
56 ~nsHtml5StreamParserPtr() {
57 if (mRawPtr) release(mRawPtr);
59 // Constructors
60 nsHtml5StreamParserPtr()
61 : mRawPtr(0)
62 // default constructor
64 nsHtml5StreamParserPtr(const nsHtml5StreamParserPtr& aSmartPtr)
65 : mRawPtr(aSmartPtr.mRawPtr)
66 // copy-constructor
68 if (mRawPtr) mRawPtr->AddRef();
70 explicit nsHtml5StreamParserPtr(nsHtml5StreamParser* aRawPtr)
71 : mRawPtr(aRawPtr)
72 // construct from a raw pointer (of the right type)
74 if (mRawPtr) mRawPtr->AddRef();
76 // Assignment operators
77 nsHtml5StreamParserPtr& operator=(const nsHtml5StreamParserPtr& rhs)
78 // copy assignment operator
80 assign_with_AddRef(rhs.mRawPtr);
81 return *this;
83 nsHtml5StreamParserPtr& operator=(nsHtml5StreamParser* rhs)
84 // assign from a raw pointer (of the right type)
86 assign_with_AddRef(rhs);
87 return *this;
89 // Other pointer operators
90 void swap(nsHtml5StreamParserPtr& rhs)
91 // ...exchange ownership with |rhs|; can save a pair of refcount operations
93 nsHtml5StreamParser* temp = rhs.mRawPtr;
94 rhs.mRawPtr = mRawPtr;
95 mRawPtr = temp;
97 void swap(nsHtml5StreamParser*& rhs)
98 // ...exchange ownership with |rhs|; can save a pair of refcount operations
100 nsHtml5StreamParser* temp = rhs;
101 rhs = mRawPtr;
102 mRawPtr = temp;
104 template <typename I>
105 void forget(I** rhs)
106 // Set the target of rhs to the value of mRawPtr and null out mRawPtr.
107 // Useful to avoid unnecessary AddRef/Release pairs with "out"
108 // parameters where rhs bay be a T** or an I** where I is a base class
109 // of T.
111 NS_ASSERTION(rhs, "Null pointer passed to forget!");
112 *rhs = mRawPtr;
113 mRawPtr = 0;
115 nsHtml5StreamParser* get() const
117 Prefer the implicit conversion provided automatically by |operator
118 nsHtml5StreamParser*() const|. Use |get()| to resolve ambiguity or to get a
119 castable pointer.
122 return const_cast<nsHtml5StreamParser*>(mRawPtr);
124 operator nsHtml5StreamParser*() const
126 ...makes an |nsHtml5StreamParserPtr| act like its underlying raw
127 pointer type whenever it is used in a context where a raw pointer is
128 expected. It is this operator that makes an |nsHtml5StreamParserPtr|
129 substitutable for a raw pointer. Prefer the implicit use of this operator
130 to calling |get()|, except where necessary to resolve ambiguity.
133 return get();
135 nsHtml5StreamParser* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN {
136 MOZ_ASSERT(mRawPtr != 0,
137 "You can't dereference a NULL nsHtml5StreamParserPtr with "
138 "operator->().");
139 return get();
141 nsHtml5StreamParserPtr* get_address()
142 // This is not intended to be used by clients. See |address_of|
143 // below.
145 return this;
147 const nsHtml5StreamParserPtr* get_address() const
148 // This is not intended to be used by clients. See |address_of|
149 // below.
151 return this;
154 public:
155 nsHtml5StreamParser& operator*() const {
156 MOZ_ASSERT(mRawPtr != 0,
157 "You can't dereference a NULL nsHtml5StreamParserPtr with "
158 "operator*().");
159 return *get();
161 nsHtml5StreamParser** StartAssignment() {
162 #ifndef NSCAP_FEATURE_INLINE_STARTASSIGNMENT
163 return reinterpret_cast<nsHtml5StreamParser**>(begin_assignment());
164 #else
165 assign_assuming_AddRef(0);
166 return reinterpret_cast<nsHtml5StreamParser**>(&mRawPtr);
167 #endif
171 inline nsHtml5StreamParserPtr* address_of(nsHtml5StreamParserPtr& aPtr) {
172 return aPtr.get_address();
175 inline const nsHtml5StreamParserPtr* address_of(
176 const nsHtml5StreamParserPtr& aPtr) {
177 return aPtr.get_address();
180 class nsHtml5StreamParserPtrGetterAddRefs
183 This class is designed to be used for anonymous temporary objects in the
184 argument list of calls that return COM interface pointers, e.g.,
185 nsHtml5StreamParserPtr<IFoo> fooP;
186 ...->GetAddRefedPointer(getter_AddRefs(fooP))
187 DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_AddRefs()|
188 instead. When initialized with a |nsHtml5StreamParserPtr|, as in the example
189 above, it returns a |void**|, a |T**|, or an |nsISupports**| as needed, that
190 the outer call (|GetAddRefedPointer| in this case) can fill in. This type
191 should be a nested class inside |nsHtml5StreamParserPtr<T>|.
194 public:
195 explicit nsHtml5StreamParserPtrGetterAddRefs(
196 nsHtml5StreamParserPtr& aSmartPtr)
197 : mTargetSmartPtr(aSmartPtr) {
198 // nothing else to do
200 operator void**() {
201 return reinterpret_cast<void**>(mTargetSmartPtr.StartAssignment());
203 operator nsHtml5StreamParser**() { return mTargetSmartPtr.StartAssignment(); }
204 nsHtml5StreamParser*& operator*() {
205 return *(mTargetSmartPtr.StartAssignment());
208 private:
209 nsHtml5StreamParserPtr& mTargetSmartPtr;
212 inline nsHtml5StreamParserPtrGetterAddRefs getter_AddRefs(
213 nsHtml5StreamParserPtr& aSmartPtr)
215 Used around a |nsHtml5StreamParserPtr| when
216 ...makes the class |nsHtml5StreamParserPtrGetterAddRefs| invisible.
219 return nsHtml5StreamParserPtrGetterAddRefs(aSmartPtr);
222 // Comparing an |nsHtml5StreamParserPtr| to |0|
224 inline bool operator==(const nsHtml5StreamParserPtr& lhs, decltype(nullptr)) {
225 return lhs.get() == nullptr;
228 inline bool operator==(decltype(nullptr), const nsHtml5StreamParserPtr& rhs) {
229 return nullptr == rhs.get();
232 inline bool operator!=(const nsHtml5StreamParserPtr& lhs, decltype(nullptr)) {
233 return lhs.get() != nullptr;
236 inline bool operator!=(decltype(nullptr), const nsHtml5StreamParserPtr& rhs) {
237 return nullptr != rhs.get();
240 #endif // !defined(nsHtml5StreamParserPtr_h)