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/. */
7 #ifndef nsISupportsUtils_h__
8 #define nsISupportsUtils_h__
10 #include <type_traits>
13 #include "nsIOutputStream.h"
14 #include "nsISupports.h"
17 #include "nsISupportsImpl.h"
18 #include "mozilla/RefPtr.h"
21 * Macro for adding a reference to an interface.
22 * @param _ptr The interface pointer.
24 #define NS_ADDREF(_ptr) (_ptr)->AddRef()
27 * Macro for adding a reference to this. This macro should be used
28 * because NS_ADDREF (when tracing) may require an ambiguous cast
29 * from the pointers primary type to nsISupports. This macro sidesteps
30 * that entire problem.
32 #define NS_ADDREF_THIS() AddRef()
34 // Making this a |inline| |template| allows |aExpr| to be evaluated only once,
35 // yet still denies you the ability to |AddRef()| an |nsCOMPtr|.
37 inline void ns_if_addref(T aExpr
) {
44 * Macro for adding a reference to an interface that checks for nullptr.
45 * @param _expr The interface pointer.
47 #define NS_IF_ADDREF(_expr) ns_if_addref(_expr)
50 * Given these declarations, it explicitly OK and efficient to end a `getter'
53 * NS_IF_ADDREF(*result = mThing);
55 * even if |mThing| is an |nsCOMPtr|. If |mThing| is an |nsCOMPtr|, however, it
56 * is still _illegal_ to say |NS_IF_ADDREF(mThing)|.
60 * Macro for releasing a reference to an interface.
61 * @param _ptr The interface pointer.
63 #define NS_RELEASE(_ptr) \
70 * Macro for releasing a reference to this interface.
72 #define NS_RELEASE_THIS() Release()
75 * Macro for releasing a reference to an interface, except that this
76 * macro preserves the return value from the underlying Release call.
77 * The interface pointer argument will only be NULLed if the reference count
80 * @param _ptr The interface pointer.
81 * @param _rc The reference count.
83 #define NS_RELEASE2(_ptr, _rc) \
85 _rc = (_ptr)->Release(); \
86 if (0 == (_rc)) (_ptr) = 0; \
90 * Macro for releasing a reference to an interface that checks for nullptr;
91 * @param _ptr The interface pointer.
93 #define NS_IF_RELEASE(_ptr) \
102 * Often you have to cast an implementation pointer, e.g., |this|, to an
103 * |nsISupports*|, but because you have multiple inheritance, a simple cast
104 * is ambiguous. One could simply say, e.g., (given a base |nsIBase|),
105 * |static_cast<nsIBase*>(this)|; but that disguises the fact that what
106 * you are really doing is disambiguating the |nsISupports|. You could make
107 * that more obvious with a double cast, e.g., |static_cast<nsISupports*>
109 static_cast<nsIBase*>(this))|, but that is bulky and harder to read...
111 * The following macro is clean, short, and obvious. In the example above,
112 * you would use it like this: |NS_ISUPPORTS_CAST(nsIBase*, this)|.
115 #define NS_ISUPPORTS_CAST(__unambiguousBase, __expr) \
116 static_cast<nsISupports*>(static_cast<__unambiguousBase>(__expr))
118 // a type-safe shortcut for calling the |QueryInterface()| member function
119 template <class T
, class DestinationType
>
120 inline nsresult
CallQueryInterface(T
* aSource
, DestinationType
** aDestination
) {
121 // We permit nsISupports-to-nsISupports here so that one can still obtain
122 // the canonical nsISupports pointer with CallQueryInterface.
124 !(std::is_same_v
<DestinationType
, T
> ||
125 std::is_base_of
<DestinationType
, T
>::value
) ||
126 std::is_same_v
<DestinationType
, nsISupports
>,
127 "don't use CallQueryInterface for compile-time-determinable casts");
129 MOZ_ASSERT(aSource
, "null parameter");
130 MOZ_ASSERT(aDestination
, "null parameter");
132 return aSource
->QueryInterface(NS_GET_TEMPLATE_IID(DestinationType
),
133 reinterpret_cast<void**>(aDestination
));
136 template <class SourceType
, class DestinationType
>
137 inline nsresult
CallQueryInterface(RefPtr
<SourceType
>& aSourcePtr
,
138 DestinationType
** aDestPtr
) {
139 return CallQueryInterface(aSourcePtr
.get(), aDestPtr
);
142 #endif /* __nsISupportsUtils_h */