parser/html/nsHtml5RefPtr.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef nsHtml5RefPtr_h
michael@0 7 #define nsHtml5RefPtr_h
michael@0 8
michael@0 9 #include "nsThreadUtils.h"
michael@0 10
michael@0 11 template <class T>
michael@0 12 class nsHtml5RefPtrReleaser : public nsRunnable
michael@0 13 {
michael@0 14 private:
michael@0 15 T* mPtr;
michael@0 16 public:
michael@0 17 nsHtml5RefPtrReleaser(T* aPtr)
michael@0 18 : mPtr(aPtr)
michael@0 19 {}
michael@0 20 NS_IMETHODIMP Run()
michael@0 21 {
michael@0 22 mPtr->Release();
michael@0 23 return NS_OK;
michael@0 24 }
michael@0 25 };
michael@0 26
michael@0 27 // template <class T> class nsHtml5RefPtrGetterAddRefs;
michael@0 28
michael@0 29 /**
michael@0 30 * Like nsRefPtr except release is proxied to the main thread. Mostly copied
michael@0 31 * from nsRefPtr.
michael@0 32 */
michael@0 33 template <class T>
michael@0 34 class nsHtml5RefPtr
michael@0 35 {
michael@0 36 private:
michael@0 37
michael@0 38 void
michael@0 39 assign_with_AddRef( T* rawPtr )
michael@0 40 {
michael@0 41 if ( rawPtr )
michael@0 42 rawPtr->AddRef();
michael@0 43 assign_assuming_AddRef(rawPtr);
michael@0 44 }
michael@0 45
michael@0 46 void**
michael@0 47 begin_assignment()
michael@0 48 {
michael@0 49 assign_assuming_AddRef(0);
michael@0 50 return reinterpret_cast<void**>(&mRawPtr);
michael@0 51 }
michael@0 52
michael@0 53 void
michael@0 54 assign_assuming_AddRef( T* newPtr )
michael@0 55 {
michael@0 56 T* oldPtr = mRawPtr;
michael@0 57 mRawPtr = newPtr;
michael@0 58 if ( oldPtr )
michael@0 59 release(oldPtr);
michael@0 60 }
michael@0 61
michael@0 62 void
michael@0 63 release( T* aPtr )
michael@0 64 {
michael@0 65 nsCOMPtr<nsIRunnable> releaser = new nsHtml5RefPtrReleaser<T>(aPtr);
michael@0 66 if (NS_FAILED(NS_DispatchToMainThread(releaser)))
michael@0 67 {
michael@0 68 NS_WARNING("Failed to dispatch releaser event.");
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 private:
michael@0 73 T* mRawPtr;
michael@0 74
michael@0 75 public:
michael@0 76 typedef T element_type;
michael@0 77
michael@0 78 ~nsHtml5RefPtr()
michael@0 79 {
michael@0 80 if ( mRawPtr )
michael@0 81 release(mRawPtr);
michael@0 82 }
michael@0 83
michael@0 84 // Constructors
michael@0 85
michael@0 86 nsHtml5RefPtr()
michael@0 87 : mRawPtr(0)
michael@0 88 // default constructor
michael@0 89 {
michael@0 90 }
michael@0 91
michael@0 92 nsHtml5RefPtr( const nsHtml5RefPtr<T>& aSmartPtr )
michael@0 93 : mRawPtr(aSmartPtr.mRawPtr)
michael@0 94 // copy-constructor
michael@0 95 {
michael@0 96 if ( mRawPtr )
michael@0 97 mRawPtr->AddRef();
michael@0 98 }
michael@0 99
michael@0 100 nsHtml5RefPtr( T* aRawPtr )
michael@0 101 : mRawPtr(aRawPtr)
michael@0 102 // construct from a raw pointer (of the right type)
michael@0 103 {
michael@0 104 if ( mRawPtr )
michael@0 105 mRawPtr->AddRef();
michael@0 106 }
michael@0 107
michael@0 108 nsHtml5RefPtr( const already_AddRefed<T>& aSmartPtr )
michael@0 109 : mRawPtr(aSmartPtr.mRawPtr)
michael@0 110 // construct from |dont_AddRef(expr)|
michael@0 111 {
michael@0 112 }
michael@0 113
michael@0 114 // Assignment operators
michael@0 115
michael@0 116 nsHtml5RefPtr<T>&
michael@0 117 operator=( const nsHtml5RefPtr<T>& rhs )
michael@0 118 // copy assignment operator
michael@0 119 {
michael@0 120 assign_with_AddRef(rhs.mRawPtr);
michael@0 121 return *this;
michael@0 122 }
michael@0 123
michael@0 124 nsHtml5RefPtr<T>&
michael@0 125 operator=( T* rhs )
michael@0 126 // assign from a raw pointer (of the right type)
michael@0 127 {
michael@0 128 assign_with_AddRef(rhs);
michael@0 129 return *this;
michael@0 130 }
michael@0 131
michael@0 132 nsHtml5RefPtr<T>&
michael@0 133 operator=( const already_AddRefed<T>& rhs )
michael@0 134 // assign from |dont_AddRef(expr)|
michael@0 135 {
michael@0 136 assign_assuming_AddRef(rhs.mRawPtr);
michael@0 137 return *this;
michael@0 138 }
michael@0 139
michael@0 140 // Other pointer operators
michael@0 141
michael@0 142 void
michael@0 143 swap( nsHtml5RefPtr<T>& rhs )
michael@0 144 // ...exchange ownership with |rhs|; can save a pair of refcount operations
michael@0 145 {
michael@0 146 T* temp = rhs.mRawPtr;
michael@0 147 rhs.mRawPtr = mRawPtr;
michael@0 148 mRawPtr = temp;
michael@0 149 }
michael@0 150
michael@0 151 void
michael@0 152 swap( T*& rhs )
michael@0 153 // ...exchange ownership with |rhs|; can save a pair of refcount operations
michael@0 154 {
michael@0 155 T* temp = rhs;
michael@0 156 rhs = mRawPtr;
michael@0 157 mRawPtr = temp;
michael@0 158 }
michael@0 159
michael@0 160 already_AddRefed<T>
michael@0 161 forget()
michael@0 162 // return the value of mRawPtr and null out mRawPtr. Useful for
michael@0 163 // already_AddRefed return values.
michael@0 164 {
michael@0 165 T* temp = 0;
michael@0 166 swap(temp);
michael@0 167 return temp;
michael@0 168 }
michael@0 169
michael@0 170 template <typename I>
michael@0 171 void
michael@0 172 forget( I** rhs)
michael@0 173 // Set the target of rhs to the value of mRawPtr and null out mRawPtr.
michael@0 174 // Useful to avoid unnecessary AddRef/Release pairs with "out"
michael@0 175 // parameters where rhs bay be a T** or an I** where I is a base class
michael@0 176 // of T.
michael@0 177 {
michael@0 178 NS_ASSERTION(rhs, "Null pointer passed to forget!");
michael@0 179 *rhs = mRawPtr;
michael@0 180 mRawPtr = 0;
michael@0 181 }
michael@0 182
michael@0 183 T*
michael@0 184 get() const
michael@0 185 /*
michael@0 186 Prefer the implicit conversion provided automatically by |operator T*() const|.
michael@0 187 Use |get()| to resolve ambiguity or to get a castable pointer.
michael@0 188 */
michael@0 189 {
michael@0 190 return const_cast<T*>(mRawPtr);
michael@0 191 }
michael@0 192
michael@0 193 operator T*() const
michael@0 194 /*
michael@0 195 ...makes an |nsHtml5RefPtr| act like its underlying raw pointer type whenever it
michael@0 196 is used in a context where a raw pointer is expected. It is this operator
michael@0 197 that makes an |nsHtml5RefPtr| substitutable for a raw pointer.
michael@0 198
michael@0 199 Prefer the implicit use of this operator to calling |get()|, except where
michael@0 200 necessary to resolve ambiguity.
michael@0 201 */
michael@0 202 {
michael@0 203 return get();
michael@0 204 }
michael@0 205
michael@0 206 T*
michael@0 207 operator->() const
michael@0 208 {
michael@0 209 NS_PRECONDITION(mRawPtr != 0, "You can't dereference a NULL nsHtml5RefPtr with operator->().");
michael@0 210 return get();
michael@0 211 }
michael@0 212
michael@0 213 nsHtml5RefPtr<T>*
michael@0 214 get_address()
michael@0 215 // This is not intended to be used by clients. See |address_of|
michael@0 216 // below.
michael@0 217 {
michael@0 218 return this;
michael@0 219 }
michael@0 220
michael@0 221 const nsHtml5RefPtr<T>*
michael@0 222 get_address() const
michael@0 223 // This is not intended to be used by clients. See |address_of|
michael@0 224 // below.
michael@0 225 {
michael@0 226 return this;
michael@0 227 }
michael@0 228
michael@0 229 public:
michael@0 230 T&
michael@0 231 operator*() const
michael@0 232 {
michael@0 233 NS_PRECONDITION(mRawPtr != 0, "You can't dereference a NULL nsHtml5RefPtr with operator*().");
michael@0 234 return *get();
michael@0 235 }
michael@0 236
michael@0 237 T**
michael@0 238 StartAssignment()
michael@0 239 {
michael@0 240 #ifndef NSCAP_FEATURE_INLINE_STARTASSIGNMENT
michael@0 241 return reinterpret_cast<T**>(begin_assignment());
michael@0 242 #else
michael@0 243 assign_assuming_AddRef(0);
michael@0 244 return reinterpret_cast<T**>(&mRawPtr);
michael@0 245 #endif
michael@0 246 }
michael@0 247 };
michael@0 248
michael@0 249 template <class T>
michael@0 250 inline
michael@0 251 nsHtml5RefPtr<T>*
michael@0 252 address_of( nsHtml5RefPtr<T>& aPtr )
michael@0 253 {
michael@0 254 return aPtr.get_address();
michael@0 255 }
michael@0 256
michael@0 257 template <class T>
michael@0 258 inline
michael@0 259 const nsHtml5RefPtr<T>*
michael@0 260 address_of( const nsHtml5RefPtr<T>& aPtr )
michael@0 261 {
michael@0 262 return aPtr.get_address();
michael@0 263 }
michael@0 264
michael@0 265 template <class T>
michael@0 266 class nsHtml5RefPtrGetterAddRefs
michael@0 267 /*
michael@0 268 ...
michael@0 269
michael@0 270 This class is designed to be used for anonymous temporary objects in the
michael@0 271 argument list of calls that return COM interface pointers, e.g.,
michael@0 272
michael@0 273 nsHtml5RefPtr<IFoo> fooP;
michael@0 274 ...->GetAddRefedPointer(getter_AddRefs(fooP))
michael@0 275
michael@0 276 DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_AddRefs()| instead.
michael@0 277
michael@0 278 When initialized with a |nsHtml5RefPtr|, as in the example above, it returns
michael@0 279 a |void**|, a |T**|, or an |nsISupports**| as needed, that the
michael@0 280 outer call (|GetAddRefedPointer| in this case) can fill in.
michael@0 281
michael@0 282 This type should be a nested class inside |nsHtml5RefPtr<T>|.
michael@0 283 */
michael@0 284 {
michael@0 285 public:
michael@0 286 explicit
michael@0 287 nsHtml5RefPtrGetterAddRefs( nsHtml5RefPtr<T>& aSmartPtr )
michael@0 288 : mTargetSmartPtr(aSmartPtr)
michael@0 289 {
michael@0 290 // nothing else to do
michael@0 291 }
michael@0 292
michael@0 293 operator void**()
michael@0 294 {
michael@0 295 return reinterpret_cast<void**>(mTargetSmartPtr.StartAssignment());
michael@0 296 }
michael@0 297
michael@0 298 operator T**()
michael@0 299 {
michael@0 300 return mTargetSmartPtr.StartAssignment();
michael@0 301 }
michael@0 302
michael@0 303 T*&
michael@0 304 operator*()
michael@0 305 {
michael@0 306 return *(mTargetSmartPtr.StartAssignment());
michael@0 307 }
michael@0 308
michael@0 309 private:
michael@0 310 nsHtml5RefPtr<T>& mTargetSmartPtr;
michael@0 311 };
michael@0 312
michael@0 313 template <class T>
michael@0 314 inline
michael@0 315 nsHtml5RefPtrGetterAddRefs<T>
michael@0 316 getter_AddRefs( nsHtml5RefPtr<T>& aSmartPtr )
michael@0 317 /*
michael@0 318 Used around a |nsHtml5RefPtr| when
michael@0 319 ...makes the class |nsHtml5RefPtrGetterAddRefs<T>| invisible.
michael@0 320 */
michael@0 321 {
michael@0 322 return nsHtml5RefPtrGetterAddRefs<T>(aSmartPtr);
michael@0 323 }
michael@0 324
michael@0 325
michael@0 326
michael@0 327 // Comparing two |nsHtml5RefPtr|s
michael@0 328
michael@0 329 template <class T, class U>
michael@0 330 inline
michael@0 331 bool
michael@0 332 operator==( const nsHtml5RefPtr<T>& lhs, const nsHtml5RefPtr<U>& rhs )
michael@0 333 {
michael@0 334 return static_cast<const T*>(lhs.get()) == static_cast<const U*>(rhs.get());
michael@0 335 }
michael@0 336
michael@0 337
michael@0 338 template <class T, class U>
michael@0 339 inline
michael@0 340 bool
michael@0 341 operator!=( const nsHtml5RefPtr<T>& lhs, const nsHtml5RefPtr<U>& rhs )
michael@0 342 {
michael@0 343 return static_cast<const T*>(lhs.get()) != static_cast<const U*>(rhs.get());
michael@0 344 }
michael@0 345
michael@0 346
michael@0 347 // Comparing an |nsHtml5RefPtr| to a raw pointer
michael@0 348
michael@0 349 template <class T, class U>
michael@0 350 inline
michael@0 351 bool
michael@0 352 operator==( const nsHtml5RefPtr<T>& lhs, const U* rhs )
michael@0 353 {
michael@0 354 return static_cast<const T*>(lhs.get()) == static_cast<const U*>(rhs);
michael@0 355 }
michael@0 356
michael@0 357 template <class T, class U>
michael@0 358 inline
michael@0 359 bool
michael@0 360 operator==( const U* lhs, const nsHtml5RefPtr<T>& rhs )
michael@0 361 {
michael@0 362 return static_cast<const U*>(lhs) == static_cast<const T*>(rhs.get());
michael@0 363 }
michael@0 364
michael@0 365 template <class T, class U>
michael@0 366 inline
michael@0 367 bool
michael@0 368 operator!=( const nsHtml5RefPtr<T>& lhs, const U* rhs )
michael@0 369 {
michael@0 370 return static_cast<const T*>(lhs.get()) != static_cast<const U*>(rhs);
michael@0 371 }
michael@0 372
michael@0 373 template <class T, class U>
michael@0 374 inline
michael@0 375 bool
michael@0 376 operator!=( const U* lhs, const nsHtml5RefPtr<T>& rhs )
michael@0 377 {
michael@0 378 return static_cast<const U*>(lhs) != static_cast<const T*>(rhs.get());
michael@0 379 }
michael@0 380
michael@0 381 // To avoid ambiguities caused by the presence of builtin |operator==|s
michael@0 382 // creating a situation where one of the |operator==| defined above
michael@0 383 // has a better conversion for one argument and the builtin has a
michael@0 384 // better conversion for the other argument, define additional
michael@0 385 // |operator==| without the |const| on the raw pointer.
michael@0 386 // See bug 65664 for details.
michael@0 387
michael@0 388 #ifndef NSCAP_DONT_PROVIDE_NONCONST_OPEQ
michael@0 389 template <class T, class U>
michael@0 390 inline
michael@0 391 bool
michael@0 392 operator==( const nsHtml5RefPtr<T>& lhs, U* rhs )
michael@0 393 {
michael@0 394 return static_cast<const T*>(lhs.get()) == const_cast<const U*>(rhs);
michael@0 395 }
michael@0 396
michael@0 397 template <class T, class U>
michael@0 398 inline
michael@0 399 bool
michael@0 400 operator==( U* lhs, const nsHtml5RefPtr<T>& rhs )
michael@0 401 {
michael@0 402 return const_cast<const U*>(lhs) == static_cast<const T*>(rhs.get());
michael@0 403 }
michael@0 404
michael@0 405 template <class T, class U>
michael@0 406 inline
michael@0 407 bool
michael@0 408 operator!=( const nsHtml5RefPtr<T>& lhs, U* rhs )
michael@0 409 {
michael@0 410 return static_cast<const T*>(lhs.get()) != const_cast<const U*>(rhs);
michael@0 411 }
michael@0 412
michael@0 413 template <class T, class U>
michael@0 414 inline
michael@0 415 bool
michael@0 416 operator!=( U* lhs, const nsHtml5RefPtr<T>& rhs )
michael@0 417 {
michael@0 418 return const_cast<const U*>(lhs) != static_cast<const T*>(rhs.get());
michael@0 419 }
michael@0 420 #endif
michael@0 421
michael@0 422
michael@0 423
michael@0 424 // Comparing an |nsHtml5RefPtr| to |0|
michael@0 425
michael@0 426 template <class T>
michael@0 427 inline
michael@0 428 bool
michael@0 429 operator==( const nsHtml5RefPtr<T>& lhs, NSCAP_Zero* rhs )
michael@0 430 // specifically to allow |smartPtr == 0|
michael@0 431 {
michael@0 432 return static_cast<const void*>(lhs.get()) == reinterpret_cast<const void*>(rhs);
michael@0 433 }
michael@0 434
michael@0 435 template <class T>
michael@0 436 inline
michael@0 437 bool
michael@0 438 operator==( NSCAP_Zero* lhs, const nsHtml5RefPtr<T>& rhs )
michael@0 439 // specifically to allow |0 == smartPtr|
michael@0 440 {
michael@0 441 return reinterpret_cast<const void*>(lhs) == static_cast<const void*>(rhs.get());
michael@0 442 }
michael@0 443
michael@0 444 template <class T>
michael@0 445 inline
michael@0 446 bool
michael@0 447 operator!=( const nsHtml5RefPtr<T>& lhs, NSCAP_Zero* rhs )
michael@0 448 // specifically to allow |smartPtr != 0|
michael@0 449 {
michael@0 450 return static_cast<const void*>(lhs.get()) != reinterpret_cast<const void*>(rhs);
michael@0 451 }
michael@0 452
michael@0 453 template <class T>
michael@0 454 inline
michael@0 455 bool
michael@0 456 operator!=( NSCAP_Zero* lhs, const nsHtml5RefPtr<T>& rhs )
michael@0 457 // specifically to allow |0 != smartPtr|
michael@0 458 {
michael@0 459 return reinterpret_cast<const void*>(lhs) != static_cast<const void*>(rhs.get());
michael@0 460 }
michael@0 461
michael@0 462
michael@0 463 #ifdef HAVE_CPP_TROUBLE_COMPARING_TO_ZERO
michael@0 464
michael@0 465 // We need to explicitly define comparison operators for `int'
michael@0 466 // because the compiler is lame.
michael@0 467
michael@0 468 template <class T>
michael@0 469 inline
michael@0 470 bool
michael@0 471 operator==( const nsHtml5RefPtr<T>& lhs, int rhs )
michael@0 472 // specifically to allow |smartPtr == 0|
michael@0 473 {
michael@0 474 return static_cast<const void*>(lhs.get()) == reinterpret_cast<const void*>(rhs);
michael@0 475 }
michael@0 476
michael@0 477 template <class T>
michael@0 478 inline
michael@0 479 bool
michael@0 480 operator==( int lhs, const nsHtml5RefPtr<T>& rhs )
michael@0 481 // specifically to allow |0 == smartPtr|
michael@0 482 {
michael@0 483 return reinterpret_cast<const void*>(lhs) == static_cast<const void*>(rhs.get());
michael@0 484 }
michael@0 485
michael@0 486 #endif // !defined(HAVE_CPP_TROUBLE_COMPARING_TO_ZERO)
michael@0 487
michael@0 488 #endif // !defined(nsHtml5RefPtr_h)

mercurial