1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
24 * Scott Collins <scc@ScottCollins.net>
25 * Dan Mosedale <dmose@mozilla.org>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef nsISupportsUtils_h__
42 #define nsISupportsUtils_h__
48 #ifndef nsISupportsBase_h__
49 #include "nsISupportsBase.h"
60 #ifndef nsISupportsImpl_h__
61 #include "nsISupportsImpl.h"
65 * Macro for instantiating a new object that implements nsISupports.
66 * Note that you can only use this if you adhere to the no arguments
67 * constructor com policy (which you really should!).
68 * @param _result Where the new instance pointer is stored
69 * @param _type The type of object to call "new" with.
71 #define NS_NEWXPCOM(_result,_type) \
73 _result = new _type(); \
77 * Macro for deleting an object that implements nsISupports.
78 * @param _ptr The object to delete.
80 #define NS_DELETEXPCOM(_ptr) \
86 * Macro for adding a reference to an interface.
87 * @param _ptr The interface pointer.
89 #define NS_ADDREF(_ptr) \
93 * Macro for adding a reference to this. This macro should be used
94 * because NS_ADDREF (when tracing) may require an ambiguous cast
95 * from the pointers primary type to nsISupports. This macro sidesteps
96 * that entire problem.
98 #define NS_ADDREF_THIS() \
103 // ...because some one is accidentally including this file inside
107 // Making this a |inline| |template| allows |expr| to be evaluated only once,
108 // yet still denies you the ability to |AddRef()| an |nsCOMPtr|.
112 ns_if_addref( T expr
)
114 return expr
? expr
->AddRef() : 0;
120 * Macro for adding a reference to an interface that checks for NULL.
121 * @param _expr The interface pointer.
123 #define NS_IF_ADDREF(_expr) ns_if_addref(_expr)
126 * Given these declarations, it explicitly OK and efficient to end a `getter' with:
128 * NS_IF_ADDREF(*result = mThing);
130 * even if |mThing| is an |nsCOMPtr|. If |mThing| is an |nsCOMPtr|, however, it is still
131 * _illegal_ to say |NS_IF_ADDREF(mThing)|.
135 * Macro for releasing a reference to an interface.
136 * @param _ptr The interface pointer.
138 #define NS_RELEASE(_ptr) \
145 * Macro for releasing a reference to an interface.
146 * @param _ptr The interface pointer.
148 #define NS_RELEASE_THIS() \
152 * Macro for releasing a reference to an interface, except that this
153 * macro preserves the return value from the underlying Release call.
154 * The interface pointer argument will only be NULLed if the reference count
157 * @param _ptr The interface pointer.
159 #define NS_RELEASE2(_ptr,_rv) \
161 _rv = (_ptr)->Release(); \
162 if (0 == (_rv)) (_ptr) = 0; \
166 * Macro for releasing a reference to an interface that checks for NULL;
167 * @param _ptr The interface pointer.
169 #define NS_IF_RELEASE(_ptr) \
178 * Often you have to cast an implementation pointer, e.g., |this|, to an
179 * |nsISupports*|, but because you have multiple inheritance, a simple cast
180 * is ambiguous. One could simply say, e.g., (given a base |nsIBase|),
181 * |static_cast<nsIBase*>(this)|; but that disguises the fact that what
182 * you are really doing is disambiguating the |nsISupports|. You could make
183 * that more obvious with a double cast, e.g., |static_cast<nsISupports*>
184 (* static_cast<nsIBase*>(this))|, but that is bulky and harder to read...
186 * The following macro is clean, short, and obvious. In the example above,
187 * you would use it like this: |NS_ISUPPORTS_CAST(nsIBase*, this)|.
190 #define NS_ISUPPORTS_CAST(__unambiguousBase, __expr) \
191 static_cast<nsISupports*>(static_cast<__unambiguousBase>(__expr))
193 // a type-safe shortcut for calling the |QueryInterface()| member function
194 template <class T
, class DestinationType
>
197 CallQueryInterface( T
* aSource
, DestinationType
** aDestination
)
199 NS_PRECONDITION(aSource
, "null parameter");
200 NS_PRECONDITION(aDestination
, "null parameter");
202 return aSource
->QueryInterface(NS_GET_TEMPLATE_IID(DestinationType
),
203 reinterpret_cast<void**>(aDestination
));
206 #endif /* __nsISupportsUtils_h */