1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
12 #include "nsCycleCollectionNoteChild.h"
13 #include "mozilla/MemoryReporting.h"
15 /*****************************************************************************/
17 // template <class T> class nsAutoPtrGetterTransfers;
27 return reinterpret_cast<void**>(&mRawPtr
);
35 if (aNewPtr
&& aNewPtr
== oldPtr
) {
36 NS_RUNTIMEABORT("Logic flaw in the caller");
43 // |class Ptr| helps us prevent implicit "copy construction"
44 // through |operator T*() const| from a |const nsAutoPtr<T>|
45 // because two implicit conversions in a row aren't allowed.
46 // It still allows assignment from T* through implicit conversion
47 // from |T*| to |nsAutoPtr<T>::Ptr|
51 MOZ_IMPLICIT
Ptr(T
* aPtr
)
69 typedef T element_type
;
80 // default constructor
84 MOZ_IMPLICIT
nsAutoPtr(Ptr aRawPtr
)
86 // construct from a raw pointer (of the right type)
90 // This constructor shouldn't exist; we should just use the &&
92 nsAutoPtr(nsAutoPtr
<T
>& aSmartPtr
)
93 : mRawPtr(aSmartPtr
.forget())
94 // Construct by transferring ownership from another smart pointer.
98 nsAutoPtr(nsAutoPtr
<T
>&& aSmartPtr
)
99 : mRawPtr(aSmartPtr
.forget())
100 // Construct by transferring ownership from another smart pointer.
104 // Assignment operators
108 // assign from a raw pointer (of the right type)
114 nsAutoPtr
<T
>& operator=(nsAutoPtr
<T
>& aRhs
)
115 // assign by transferring ownership from another smart pointer.
117 assign(aRhs
.forget());
121 nsAutoPtr
<T
>& operator=(nsAutoPtr
<T
>&& aRhs
)
123 assign(aRhs
.forget());
127 // Other pointer operators
132 Prefer the implicit conversion provided automatically by
133 |operator T*() const|. Use |get()| _only_ to resolve
142 ...makes an |nsAutoPtr| act like its underlying raw pointer
143 type whenever it is used in a context where a raw pointer
144 is expected. It is this operator that makes an |nsAutoPtr|
145 substitutable for a raw pointer.
147 Prefer the implicit use of this operator to calling |get()|,
148 except where necessary to resolve ambiguity.
165 NS_PRECONDITION(mRawPtr
!= 0,
166 "You can't dereference a NULL nsAutoPtr with operator->().");
170 // This operator is needed for gcc <= 4.0.* and for Sun Studio; it
171 // causes internal compiler errors for some MSVC versions. (It's not
172 // clear to me whether it should be needed.)
174 template <class U
, class V
>
176 operator->*(U
V::* aMember
)
178 NS_PRECONDITION(mRawPtr
!= 0,
179 "You can't dereference a NULL nsAutoPtr with operator->*().");
180 return get()->*aMember
;
186 // This is not intended to be used by clients. See |address_of|
194 // This is not intended to be used by clients. See |address_of|
204 NS_PRECONDITION(mRawPtr
!= 0,
205 "You can't dereference a NULL nsAutoPtr with operator*().");
212 #ifndef NSCAP_FEATURE_INLINE_STARTASSIGNMENT
213 return reinterpret_cast<T
**>(begin_assignment());
216 return reinterpret_cast<T
**>(&mRawPtr
);
223 address_of(nsAutoPtr
<T
>& aPtr
)
225 return aPtr
.get_address();
229 inline const nsAutoPtr
<T
>*
230 address_of(const nsAutoPtr
<T
>& aPtr
)
232 return aPtr
.get_address();
236 class nsAutoPtrGetterTransfers
240 This class is designed to be used for anonymous temporary objects in the
241 argument list of calls that return COM interface pointers, e.g.,
243 nsAutoPtr<IFoo> fooP;
244 ...->GetTransferedPointer(getter_Transfers(fooP))
246 DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_Transfers()| instead.
248 When initialized with a |nsAutoPtr|, as in the example above, it returns
249 a |void**|, a |T**|, or an |nsISupports**| as needed, that the
250 outer call (|GetTransferedPointer| in this case) can fill in.
252 This type should be a nested class inside |nsAutoPtr<T>|.
257 nsAutoPtrGetterTransfers(nsAutoPtr
<T
>& aSmartPtr
)
258 : mTargetSmartPtr(aSmartPtr
)
260 // nothing else to do
265 return reinterpret_cast<void**>(mTargetSmartPtr
.StartAssignment());
270 return mTargetSmartPtr
.StartAssignment();
276 return *(mTargetSmartPtr
.StartAssignment());
280 nsAutoPtr
<T
>& mTargetSmartPtr
;
284 inline nsAutoPtrGetterTransfers
<T
>
285 getter_Transfers(nsAutoPtr
<T
>& aSmartPtr
)
287 Used around a |nsAutoPtr| when
288 ...makes the class |nsAutoPtrGetterTransfers<T>| invisible.
291 return nsAutoPtrGetterTransfers
<T
>(aSmartPtr
);
296 // Comparing two |nsAutoPtr|s
298 template <class T
, class U
>
300 operator==(const nsAutoPtr
<T
>& aLhs
, const nsAutoPtr
<U
>& aRhs
)
302 return static_cast<const T
*>(aLhs
.get()) == static_cast<const U
*>(aRhs
.get());
306 template <class T
, class U
>
308 operator!=(const nsAutoPtr
<T
>& aLhs
, const nsAutoPtr
<U
>& aRhs
)
310 return static_cast<const T
*>(aLhs
.get()) != static_cast<const U
*>(aRhs
.get());
314 // Comparing an |nsAutoPtr| to a raw pointer
316 template <class T
, class U
>
318 operator==(const nsAutoPtr
<T
>& aLhs
, const U
* aRhs
)
320 return static_cast<const T
*>(aLhs
.get()) == static_cast<const U
*>(aRhs
);
323 template <class T
, class U
>
325 operator==(const U
* aLhs
, const nsAutoPtr
<T
>& aRhs
)
327 return static_cast<const U
*>(aLhs
) == static_cast<const T
*>(aRhs
.get());
330 template <class T
, class U
>
332 operator!=(const nsAutoPtr
<T
>& aLhs
, const U
* aRhs
)
334 return static_cast<const T
*>(aLhs
.get()) != static_cast<const U
*>(aRhs
);
337 template <class T
, class U
>
339 operator!=(const U
* aLhs
, const nsAutoPtr
<T
>& aRhs
)
341 return static_cast<const U
*>(aLhs
) != static_cast<const T
*>(aRhs
.get());
344 template <class T
, class U
>
346 operator==(const nsAutoPtr
<T
>& aLhs
, U
* aRhs
)
348 return static_cast<const T
*>(aLhs
.get()) == const_cast<const U
*>(aRhs
);
351 template <class T
, class U
>
353 operator==(U
* aLhs
, const nsAutoPtr
<T
>& aRhs
)
355 return const_cast<const U
*>(aLhs
) == static_cast<const T
*>(aRhs
.get());
358 template <class T
, class U
>
360 operator!=(const nsAutoPtr
<T
>& aLhs
, U
* aRhs
)
362 return static_cast<const T
*>(aLhs
.get()) != const_cast<const U
*>(aRhs
);
365 template <class T
, class U
>
367 operator!=(U
* aLhs
, const nsAutoPtr
<T
>& aRhs
)
369 return const_cast<const U
*>(aLhs
) != static_cast<const T
*>(aRhs
.get());
374 // Comparing an |nsAutoPtr| to |0|
378 operator==(const nsAutoPtr
<T
>& aLhs
, NSCAP_Zero
* aRhs
)
379 // specifically to allow |smartPtr == 0|
381 return static_cast<const void*>(aLhs
.get()) == reinterpret_cast<const void*>(aRhs
);
386 operator==(NSCAP_Zero
* aLhs
, const nsAutoPtr
<T
>& aRhs
)
387 // specifically to allow |0 == smartPtr|
389 return reinterpret_cast<const void*>(aLhs
) == static_cast<const void*>(aRhs
.get());
394 operator!=(const nsAutoPtr
<T
>& aLhs
, NSCAP_Zero
* aRhs
)
395 // specifically to allow |smartPtr != 0|
397 return static_cast<const void*>(aLhs
.get()) != reinterpret_cast<const void*>(aRhs
);
402 operator!=(NSCAP_Zero
* aLhs
, const nsAutoPtr
<T
>& aRhs
)
403 // specifically to allow |0 != smartPtr|
405 return reinterpret_cast<const void*>(aLhs
) != static_cast<const void*>(aRhs
.get());
409 #ifdef HAVE_CPP_TROUBLE_COMPARING_TO_ZERO
411 // We need to explicitly define comparison operators for `int'
412 // because the compiler is lame.
416 operator==(const nsAutoPtr
<T
>& aLhs
, int aRhs
)
417 // specifically to allow |smartPtr == 0|
419 return static_cast<const void*>(aLhs
.get()) == reinterpret_cast<const void*>(aRhs
);
424 operator==(int aLhs
, const nsAutoPtr
<T
>& aRhs
)
425 // specifically to allow |0 == smartPtr|
427 return reinterpret_cast<const void*>(aLhs
) == static_cast<const void*>(aRhs
.get());
430 #endif // !defined(HAVE_CPP_TROUBLE_COMPARING_TO_ZERO)
432 /*****************************************************************************/
434 // template <class T> class nsAutoArrayPtrGetterTransfers;
444 return reinterpret_cast<void**>(&mRawPtr
);
459 typedef T element_type
;
470 // default constructor
474 MOZ_IMPLICIT
nsAutoArrayPtr(T
* aRawPtr
)
476 // construct from a raw pointer (of the right type)
480 nsAutoArrayPtr(nsAutoArrayPtr
<T
>& aSmartPtr
)
481 : mRawPtr(aSmartPtr
.forget())
482 // Construct by transferring ownership from another smart pointer.
487 // Assignment operators
491 // assign from a raw pointer (of the right type)
497 nsAutoArrayPtr
<T
>& operator=(nsAutoArrayPtr
<T
>& aRhs
)
498 // assign by transferring ownership from another smart pointer.
500 assign(aRhs
.forget());
504 // Other pointer operators
509 Prefer the implicit conversion provided automatically by
510 |operator T*() const|. Use |get()| _only_ to resolve
519 ...makes an |nsAutoArrayPtr| act like its underlying raw pointer
520 type whenever it is used in a context where a raw pointer
521 is expected. It is this operator that makes an |nsAutoArrayPtr|
522 substitutable for a raw pointer.
524 Prefer the implicit use of this operator to calling |get()|,
525 except where necessary to resolve ambiguity.
542 NS_PRECONDITION(mRawPtr
!= 0,
543 "You can't dereference a NULL nsAutoArrayPtr with operator->().");
549 // This is not intended to be used by clients. See |address_of|
555 const nsAutoArrayPtr
<T
>*
557 // This is not intended to be used by clients. See |address_of|
567 NS_PRECONDITION(mRawPtr
!= 0,
568 "You can't dereference a NULL nsAutoArrayPtr with operator*().");
575 #ifndef NSCAP_FEATURE_INLINE_STARTASSIGNMENT
576 return reinterpret_cast<T
**>(begin_assignment());
579 return reinterpret_cast<T
**>(&mRawPtr
);
584 SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const
586 return aMallocSizeOf(mRawPtr
);
590 SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const
592 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf
);
597 inline nsAutoArrayPtr
<T
>*
598 address_of(nsAutoArrayPtr
<T
>& aPtr
)
600 return aPtr
.get_address();
604 inline const nsAutoArrayPtr
<T
>*
605 address_of(const nsAutoArrayPtr
<T
>& aPtr
)
607 return aPtr
.get_address();
611 class nsAutoArrayPtrGetterTransfers
615 This class is designed to be used for anonymous temporary objects in the
616 argument list of calls that return COM interface pointers, e.g.,
618 nsAutoArrayPtr<IFoo> fooP;
619 ...->GetTransferedPointer(getter_Transfers(fooP))
621 DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_Transfers()| instead.
623 When initialized with a |nsAutoArrayPtr|, as in the example above, it returns
624 a |void**|, a |T**|, or an |nsISupports**| as needed, that the
625 outer call (|GetTransferedPointer| in this case) can fill in.
627 This type should be a nested class inside |nsAutoArrayPtr<T>|.
632 nsAutoArrayPtrGetterTransfers(nsAutoArrayPtr
<T
>& aSmartPtr
)
633 : mTargetSmartPtr(aSmartPtr
)
635 // nothing else to do
640 return reinterpret_cast<void**>(mTargetSmartPtr
.StartAssignment());
645 return mTargetSmartPtr
.StartAssignment();
651 return *(mTargetSmartPtr
.StartAssignment());
655 nsAutoArrayPtr
<T
>& mTargetSmartPtr
;
659 inline nsAutoArrayPtrGetterTransfers
<T
>
660 getter_Transfers(nsAutoArrayPtr
<T
>& aSmartPtr
)
662 Used around a |nsAutoArrayPtr| when
663 ...makes the class |nsAutoArrayPtrGetterTransfers<T>| invisible.
666 return nsAutoArrayPtrGetterTransfers
<T
>(aSmartPtr
);
671 // Comparing two |nsAutoArrayPtr|s
673 template <class T
, class U
>
675 operator==(const nsAutoArrayPtr
<T
>& aLhs
, const nsAutoArrayPtr
<U
>& aRhs
)
677 return static_cast<const T
*>(aLhs
.get()) == static_cast<const U
*>(aRhs
.get());
681 template <class T
, class U
>
683 operator!=(const nsAutoArrayPtr
<T
>& aLhs
, const nsAutoArrayPtr
<U
>& aRhs
)
685 return static_cast<const T
*>(aLhs
.get()) != static_cast<const U
*>(aRhs
.get());
689 // Comparing an |nsAutoArrayPtr| to a raw pointer
691 template <class T
, class U
>
693 operator==(const nsAutoArrayPtr
<T
>& aLhs
, const U
* aRhs
)
695 return static_cast<const T
*>(aLhs
.get()) == static_cast<const U
*>(aRhs
);
698 template <class T
, class U
>
700 operator==(const U
* aLhs
, const nsAutoArrayPtr
<T
>& aRhs
)
702 return static_cast<const U
*>(aLhs
) == static_cast<const T
*>(aRhs
.get());
705 template <class T
, class U
>
707 operator!=(const nsAutoArrayPtr
<T
>& aLhs
, const U
* aRhs
)
709 return static_cast<const T
*>(aLhs
.get()) != static_cast<const U
*>(aRhs
);
712 template <class T
, class U
>
714 operator!=(const U
* aLhs
, const nsAutoArrayPtr
<T
>& aRhs
)
716 return static_cast<const U
*>(aLhs
) != static_cast<const T
*>(aRhs
.get());
719 template <class T
, class U
>
721 operator==(const nsAutoArrayPtr
<T
>& aLhs
, U
* aRhs
)
723 return static_cast<const T
*>(aLhs
.get()) == const_cast<const U
*>(aRhs
);
726 template <class T
, class U
>
728 operator==(U
* aLhs
, const nsAutoArrayPtr
<T
>& aRhs
)
730 return const_cast<const U
*>(aLhs
) == static_cast<const T
*>(aRhs
.get());
733 template <class T
, class U
>
735 operator!=(const nsAutoArrayPtr
<T
>& aLhs
, U
* aRhs
)
737 return static_cast<const T
*>(aLhs
.get()) != const_cast<const U
*>(aRhs
);
740 template <class T
, class U
>
742 operator!=(U
* aLhs
, const nsAutoArrayPtr
<T
>& aRhs
)
744 return const_cast<const U
*>(aLhs
) != static_cast<const T
*>(aRhs
.get());
749 // Comparing an |nsAutoArrayPtr| to |0|
753 operator==(const nsAutoArrayPtr
<T
>& aLhs
, NSCAP_Zero
* aRhs
)
754 // specifically to allow |smartPtr == 0|
756 return static_cast<const void*>(aLhs
.get()) == reinterpret_cast<const void*>(aRhs
);
761 operator==(NSCAP_Zero
* aLhs
, const nsAutoArrayPtr
<T
>& aRhs
)
762 // specifically to allow |0 == smartPtr|
764 return reinterpret_cast<const void*>(aLhs
) == static_cast<const void*>(aRhs
.get());
769 operator!=(const nsAutoArrayPtr
<T
>& aLhs
, NSCAP_Zero
* aRhs
)
770 // specifically to allow |smartPtr != 0|
772 return static_cast<const void*>(aLhs
.get()) != reinterpret_cast<const void*>(aRhs
);
777 operator!=(NSCAP_Zero
* aLhs
, const nsAutoArrayPtr
<T
>& aRhs
)
778 // specifically to allow |0 != smartPtr|
780 return reinterpret_cast<const void*>(aLhs
) != static_cast<const void*>(aRhs
.get());
784 #ifdef HAVE_CPP_TROUBLE_COMPARING_TO_ZERO
786 // We need to explicitly define comparison operators for `int'
787 // because the compiler is lame.
791 operator==(const nsAutoArrayPtr
<T
>& aLhs
, int aRhs
)
792 // specifically to allow |smartPtr == 0|
794 return static_cast<const void*>(aLhs
.get()) == reinterpret_cast<const void*>(aRhs
);
799 operator==(int aLhs
, const nsAutoArrayPtr
<T
>& aRhs
)
800 // specifically to allow |0 == smartPtr|
802 return reinterpret_cast<const void*>(aLhs
) == static_cast<const void*>(aRhs
.get());
805 #endif // !defined(HAVE_CPP_TROUBLE_COMPARING_TO_ZERO)
808 /*****************************************************************************/
810 // template <class T> class nsRefPtrGetterAddRefs;
818 assign_with_AddRef(T
* aRawPtr
)
823 assign_assuming_AddRef(aRawPtr
);
829 assign_assuming_AddRef(0);
830 return reinterpret_cast<void**>(&mRawPtr
);
834 assign_assuming_AddRef(T
* aNewPtr
)
847 typedef T element_type
;
860 // default constructor
864 nsRefPtr(const nsRefPtr
<T
>& aSmartPtr
)
865 : mRawPtr(aSmartPtr
.mRawPtr
)
873 nsRefPtr(nsRefPtr
<T
>&& aRefPtr
)
874 : mRawPtr(aRefPtr
.mRawPtr
)
876 aRefPtr
.mRawPtr
= nullptr;
879 // construct from a raw pointer (of the right type)
881 MOZ_IMPLICIT
nsRefPtr(T
* aRawPtr
)
889 template <typename I
>
890 nsRefPtr(already_AddRefed
<I
>& aSmartPtr
)
891 : mRawPtr(aSmartPtr
.take())
892 // construct from |already_AddRefed|
896 template <typename I
>
897 nsRefPtr(already_AddRefed
<I
>&& aSmartPtr
)
898 : mRawPtr(aSmartPtr
.take())
899 // construct from |otherRefPtr.forget()|
903 MOZ_IMPLICIT
nsRefPtr(const nsCOMPtr_helper
& aHelper
)
906 if (NS_FAILED(aHelper(NS_GET_TEMPLATE_IID(T
), &newRawPtr
))) {
909 mRawPtr
= static_cast<T
*>(newRawPtr
);
912 // Assignment operators
915 operator=(const nsRefPtr
<T
>& aRhs
)
916 // copy assignment operator
918 assign_with_AddRef(aRhs
.mRawPtr
);
924 // assign from a raw pointer (of the right type)
926 assign_with_AddRef(aRhs
);
930 template <typename I
>
932 operator=(already_AddRefed
<I
>& aRhs
)
933 // assign from |already_AddRefed|
935 assign_assuming_AddRef(aRhs
.take());
939 template <typename I
>
941 operator=(already_AddRefed
<I
> && aRhs
)
942 // assign from |otherRefPtr.forget()|
944 assign_assuming_AddRef(aRhs
.take());
949 operator=(const nsCOMPtr_helper
& aHelper
)
952 if (NS_FAILED(aHelper(NS_GET_TEMPLATE_IID(T
), &newRawPtr
))) {
955 assign_assuming_AddRef(static_cast<T
*>(newRawPtr
));
960 operator=(nsRefPtr
<T
> && aRefPtr
)
962 assign_assuming_AddRef(aRefPtr
.mRawPtr
);
963 aRefPtr
.mRawPtr
= nullptr;
967 // Other pointer operators
970 swap(nsRefPtr
<T
>& aRhs
)
971 // ...exchange ownership with |aRhs|; can save a pair of refcount operations
973 T
* temp
= aRhs
.mRawPtr
;
974 aRhs
.mRawPtr
= mRawPtr
;
980 // ...exchange ownership with |aRhs|; can save a pair of refcount operations
989 // return the value of mRawPtr and null out mRawPtr. Useful for
990 // already_AddRefed return values.
994 return already_AddRefed
<T
>(temp
);
997 template <typename I
>
1000 // Set the target of aRhs to the value of mRawPtr and null out mRawPtr.
1001 // Useful to avoid unnecessary AddRef/Release pairs with "out"
1002 // parameters where aRhs bay be a T** or an I** where I is a base class
1005 NS_ASSERTION(aRhs
, "Null pointer passed to forget!");
1013 Prefer the implicit conversion provided automatically by |operator T*() const|.
1014 Use |get()| to resolve ambiguity or to get a castable pointer.
1017 return const_cast<T
*>(mRawPtr
);
1022 ...makes an |nsRefPtr| act like its underlying raw pointer type whenever it
1023 is used in a context where a raw pointer is expected. It is this operator
1024 that makes an |nsRefPtr| substitutable for a raw pointer.
1026 Prefer the implicit use of this operator to calling |get()|, except where
1027 necessary to resolve ambiguity.
1036 NS_PRECONDITION(mRawPtr
!= 0,
1037 "You can't dereference a NULL nsRefPtr with operator->().");
1041 // This operator is needed for gcc <= 4.0.* and for Sun Studio; it
1042 // causes internal compiler errors for some MSVC versions. (It's not
1043 // clear to me whether it should be needed.)
1045 template <class U
, class V
>
1047 operator->*(U
V::* aMember
)
1049 NS_PRECONDITION(mRawPtr
!= 0,
1050 "You can't dereference a NULL nsRefPtr with operator->*().");
1051 return get()->*aMember
;
1057 // This is not intended to be used by clients. See |address_of|
1065 // This is not intended to be used by clients. See |address_of|
1075 NS_PRECONDITION(mRawPtr
!= 0,
1076 "You can't dereference a NULL nsRefPtr with operator*().");
1083 #ifndef NSCAP_FEATURE_INLINE_STARTASSIGNMENT
1084 return reinterpret_cast<T
**>(begin_assignment());
1086 assign_assuming_AddRef(0);
1087 return reinterpret_cast<T
**>(&mRawPtr
);
1092 template <typename T
>
1094 ImplCycleCollectionUnlink(nsRefPtr
<T
>& aField
)
1099 template <typename T
>
1101 ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback
& aCallback
,
1102 nsRefPtr
<T
>& aField
,
1104 uint32_t aFlags
= 0)
1106 CycleCollectionNoteChild(aCallback
, aField
.get(), aName
, aFlags
);
1111 address_of(nsRefPtr
<T
>& aPtr
)
1113 return aPtr
.get_address();
1117 inline const nsRefPtr
<T
>*
1118 address_of(const nsRefPtr
<T
>& aPtr
)
1120 return aPtr
.get_address();
1124 class nsRefPtrGetterAddRefs
1128 This class is designed to be used for anonymous temporary objects in the
1129 argument list of calls that return COM interface pointers, e.g.,
1131 nsRefPtr<IFoo> fooP;
1132 ...->GetAddRefedPointer(getter_AddRefs(fooP))
1134 DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_AddRefs()| instead.
1136 When initialized with a |nsRefPtr|, as in the example above, it returns
1137 a |void**|, a |T**|, or an |nsISupports**| as needed, that the
1138 outer call (|GetAddRefedPointer| in this case) can fill in.
1140 This type should be a nested class inside |nsRefPtr<T>|.
1145 nsRefPtrGetterAddRefs(nsRefPtr
<T
>& aSmartPtr
)
1146 : mTargetSmartPtr(aSmartPtr
)
1148 // nothing else to do
1153 return reinterpret_cast<void**>(mTargetSmartPtr
.StartAssignment());
1158 return mTargetSmartPtr
.StartAssignment();
1164 return *(mTargetSmartPtr
.StartAssignment());
1168 nsRefPtr
<T
>& mTargetSmartPtr
;
1172 inline nsRefPtrGetterAddRefs
<T
>
1173 getter_AddRefs(nsRefPtr
<T
>& aSmartPtr
)
1175 Used around a |nsRefPtr| when
1176 ...makes the class |nsRefPtrGetterAddRefs<T>| invisible.
1179 return nsRefPtrGetterAddRefs
<T
>(aSmartPtr
);
1184 // Comparing two |nsRefPtr|s
1186 template <class T
, class U
>
1188 operator==(const nsRefPtr
<T
>& aLhs
, const nsRefPtr
<U
>& aRhs
)
1190 return static_cast<const T
*>(aLhs
.get()) == static_cast<const U
*>(aRhs
.get());
1194 template <class T
, class U
>
1196 operator!=(const nsRefPtr
<T
>& aLhs
, const nsRefPtr
<U
>& aRhs
)
1198 return static_cast<const T
*>(aLhs
.get()) != static_cast<const U
*>(aRhs
.get());
1202 // Comparing an |nsRefPtr| to a raw pointer
1204 template <class T
, class U
>
1206 operator==(const nsRefPtr
<T
>& aLhs
, const U
* aRhs
)
1208 return static_cast<const T
*>(aLhs
.get()) == static_cast<const U
*>(aRhs
);
1211 template <class T
, class U
>
1213 operator==(const U
* aLhs
, const nsRefPtr
<T
>& aRhs
)
1215 return static_cast<const U
*>(aLhs
) == static_cast<const T
*>(aRhs
.get());
1218 template <class T
, class U
>
1220 operator!=(const nsRefPtr
<T
>& aLhs
, const U
* aRhs
)
1222 return static_cast<const T
*>(aLhs
.get()) != static_cast<const U
*>(aRhs
);
1225 template <class T
, class U
>
1227 operator!=(const U
* aLhs
, const nsRefPtr
<T
>& aRhs
)
1229 return static_cast<const U
*>(aLhs
) != static_cast<const T
*>(aRhs
.get());
1232 template <class T
, class U
>
1234 operator==(const nsRefPtr
<T
>& aLhs
, U
* aRhs
)
1236 return static_cast<const T
*>(aLhs
.get()) == const_cast<const U
*>(aRhs
);
1239 template <class T
, class U
>
1241 operator==(U
* aLhs
, const nsRefPtr
<T
>& aRhs
)
1243 return const_cast<const U
*>(aLhs
) == static_cast<const T
*>(aRhs
.get());
1246 template <class T
, class U
>
1248 operator!=(const nsRefPtr
<T
>& aLhs
, U
* aRhs
)
1250 return static_cast<const T
*>(aLhs
.get()) != const_cast<const U
*>(aRhs
);
1253 template <class T
, class U
>
1255 operator!=(U
* aLhs
, const nsRefPtr
<T
>& aRhs
)
1257 return const_cast<const U
*>(aLhs
) != static_cast<const T
*>(aRhs
.get());
1262 // Comparing an |nsRefPtr| to |0|
1266 operator==(const nsRefPtr
<T
>& aLhs
, NSCAP_Zero
* aRhs
)
1267 // specifically to allow |smartPtr == 0|
1269 return static_cast<const void*>(aLhs
.get()) == reinterpret_cast<const void*>(aRhs
);
1274 operator==(NSCAP_Zero
* aLhs
, const nsRefPtr
<T
>& aRhs
)
1275 // specifically to allow |0 == smartPtr|
1277 return reinterpret_cast<const void*>(aLhs
) == static_cast<const void*>(aRhs
.get());
1282 operator!=(const nsRefPtr
<T
>& aLhs
, NSCAP_Zero
* aRhs
)
1283 // specifically to allow |smartPtr != 0|
1285 return static_cast<const void*>(aLhs
.get()) != reinterpret_cast<const void*>(aRhs
);
1290 operator!=(NSCAP_Zero
* aLhs
, const nsRefPtr
<T
>& aRhs
)
1291 // specifically to allow |0 != smartPtr|
1293 return reinterpret_cast<const void*>(aLhs
) != static_cast<const void*>(aRhs
.get());
1297 #ifdef HAVE_CPP_TROUBLE_COMPARING_TO_ZERO
1299 // We need to explicitly define comparison operators for `int'
1300 // because the compiler is lame.
1304 operator==(const nsRefPtr
<T
>& aLhs
, int aRhs
)
1305 // specifically to allow |smartPtr == 0|
1307 return static_cast<const void*>(aLhs
.get()) == reinterpret_cast<const void*>(aRhs
);
1312 operator==(int aLhs
, const nsRefPtr
<T
>& aRhs
)
1313 // specifically to allow |0 == smartPtr|
1315 return reinterpret_cast<const void*>(aLhs
) == static_cast<const void*>(aRhs
.get());
1318 #endif // !defined(HAVE_CPP_TROUBLE_COMPARING_TO_ZERO)
1320 template <class SourceType
, class DestinationType
>
1322 CallQueryInterface(nsRefPtr
<SourceType
>& aSourcePtr
, DestinationType
** aDestPtr
)
1324 return CallQueryInterface(aSourcePtr
.get(), aDestPtr
);
1327 /*****************************************************************************/
1330 class nsQueryObject
: public nsCOMPtr_helper
1333 explicit nsQueryObject(T
* aRawPtr
)
1338 virtual nsresult NS_FASTCALL
operator()(const nsIID
& aIID
,
1339 void** aResult
) const
1341 nsresult status
= mRawPtr
? mRawPtr
->QueryInterface(aIID
, aResult
)
1342 : NS_ERROR_NULL_POINTER
;
1350 class nsQueryObjectWithError
: public nsCOMPtr_helper
1353 nsQueryObjectWithError(T
* aRawPtr
, nsresult
* aErrorPtr
)
1354 : mRawPtr(aRawPtr
), mErrorPtr(aErrorPtr
)
1358 virtual nsresult NS_FASTCALL
operator()(const nsIID
& aIID
,
1359 void** aResult
) const
1361 nsresult status
= mRawPtr
? mRawPtr
->QueryInterface(aIID
, aResult
)
1362 : NS_ERROR_NULL_POINTER
;
1364 *mErrorPtr
= status
;
1370 nsresult
* mErrorPtr
;
1374 inline nsQueryObject
<T
>
1375 do_QueryObject(T
* aRawPtr
)
1377 return nsQueryObject
<T
>(aRawPtr
);
1381 inline nsQueryObject
<T
>
1382 do_QueryObject(nsCOMPtr
<T
>& aRawPtr
)
1384 return nsQueryObject
<T
>(aRawPtr
);
1388 inline nsQueryObject
<T
>
1389 do_QueryObject(nsRefPtr
<T
>& aRawPtr
)
1391 return nsQueryObject
<T
>(aRawPtr
);
1395 inline nsQueryObjectWithError
<T
>
1396 do_QueryObject(T
* aRawPtr
, nsresult
* aErrorPtr
)
1398 return nsQueryObjectWithError
<T
>(aRawPtr
, aErrorPtr
);
1402 inline nsQueryObjectWithError
<T
>
1403 do_QueryObject(nsCOMPtr
<T
>& aRawPtr
, nsresult
* aErrorPtr
)
1405 return nsQueryObjectWithError
<T
>(aRawPtr
, aErrorPtr
);
1409 inline nsQueryObjectWithError
<T
>
1410 do_QueryObject(nsRefPtr
<T
>& aRawPtr
, nsresult
* aErrorPtr
)
1412 return nsQueryObjectWithError
<T
>(aRawPtr
, aErrorPtr
);
1415 /*****************************************************************************/
1417 #endif // !defined(nsAutoPtr_h___)