nsISupportsUtils.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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
  4. *
  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/
  9. *
  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
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  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.
  21. *
  22. * Contributor(s):
  23. * Pierre Phaneuf <[email protected]>
  24. * Scott Collins <[email protected]>
  25. * Dan Mosedale <[email protected]>
  26. *
  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.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. #ifndef nsISupportsUtils_h__
  41. #define nsISupportsUtils_h__
  42. #ifndef nscore_h___
  43. #include "nscore.h"
  44. #endif
  45. #ifndef nsISupportsBase_h__
  46. #include "nsISupportsBase.h"
  47. #endif
  48. #ifndef nsError_h__
  49. #include "nsError.h"
  50. #endif
  51. #ifndef nsDebug_h___
  52. #include "nsDebug.h"
  53. #endif
  54. #ifndef nsISupportsImpl_h__
  55. #include "nsISupportsImpl.h"
  56. #endif
  57. /**
  58. * Macro for instantiating a new object that implements nsISupports.
  59. * Note that you can only use this if you adhere to the no arguments
  60. * constructor com policy (which you really should!).
  61. * @param _result Where the new instance pointer is stored
  62. * @param _type The type of object to call "new" with.
  63. */
  64. #define NS_NEWXPCOM(_result,_type) \
  65. PR_BEGIN_MACRO \
  66. _result = new _type(); \
  67. PR_END_MACRO
  68. /**
  69. * Macro for deleting an object that implements nsISupports.
  70. * @param _ptr The object to delete.
  71. */
  72. #define NS_DELETEXPCOM(_ptr) \
  73. PR_BEGIN_MACRO \
  74. delete (_ptr); \
  75. PR_END_MACRO
  76. /**
  77. * Macro for adding a reference to an interface.
  78. * @param _ptr The interface pointer.
  79. */
  80. #define NS_ADDREF(_ptr) \
  81. (_ptr)->AddRef()
  82. /**
  83. * Macro for adding a reference to this. This macro should be used
  84. * because NS_ADDREF (when tracing) may require an ambiguous cast
  85. * from the pointers primary type to nsISupports. This macro sidesteps
  86. * that entire problem.
  87. */
  88. #define NS_ADDREF_THIS() \
  89. AddRef()
  90. extern "C++" {
  91. // ...because some one is accidentally including this file inside
  92. // an |extern "C"|
  93. // Making this a |inline| |template| allows |expr| to be evaluated only once,
  94. // yet still denies you the ability to |AddRef()| an |nsCOMPtr|.
  95. template <class T>
  96. inline
  97. nsrefcnt
  98. ns_if_addref( T expr )
  99. {
  100. return expr ? expr->AddRef() : 0;
  101. }
  102. } /* extern "C++" */
  103. /**
  104. * Macro for adding a reference to an interface that checks for NULL.
  105. * @param _expr The interface pointer.
  106. */
  107. #define NS_IF_ADDREF(_expr) ns_if_addref(_expr)
  108. /*
  109. * Given these declarations, it explicitly OK and efficient to end a `getter' with:
  110. *
  111. * NS_IF_ADDREF(*result = mThing);
  112. *
  113. * even if |mThing| is an |nsCOMPtr|. If |mThing| is an |nsCOMPtr|, however, it is still
  114. * _illegal_ to say |NS_IF_ADDREF(mThing)|.
  115. */
  116. /**
  117. * Macro for releasing a reference to an interface.
  118. * @param _ptr The interface pointer.
  119. */
  120. #define NS_RELEASE(_ptr) \
  121. PR_BEGIN_MACRO \
  122. (_ptr)->Release(); \
  123. (_ptr) = 0; \
  124. PR_END_MACRO
  125. /**
  126. * Macro for releasing a reference to an interface.
  127. * @param _ptr The interface pointer.
  128. */
  129. #define NS_RELEASE_THIS() \
  130. Release()
  131. /**
  132. * Macro for releasing a reference to an interface, except that this
  133. * macro preserves the return value from the underlying Release call.
  134. * The interface pointer argument will only be NULLed if the reference count
  135. * goes to zero.
  136. *
  137. * @param _ptr The interface pointer.
  138. */
  139. #define NS_RELEASE2(_ptr,_rv) \
  140. PR_BEGIN_MACRO \
  141. _rv = (_ptr)->Release(); \
  142. if (0 == (_rv)) (_ptr) = 0; \
  143. PR_END_MACRO
  144. /**
  145. * Macro for releasing a reference to an interface that checks for NULL;
  146. * @param _ptr The interface pointer.
  147. */
  148. #define NS_IF_RELEASE(_ptr) \
  149. PR_BEGIN_MACRO \
  150. if (_ptr) { \
  151. (_ptr)->Release(); \
  152. (_ptr) = 0; \
  153. } \
  154. PR_END_MACRO
  155. /*
  156. * Often you have to cast an implementation pointer, e.g., |this|, to an
  157. * |nsISupports*|, but because you have multiple inheritance, a simple cast
  158. * is ambiguous. One could simply say, e.g., (given a base |nsIBase|),
  159. * |NS_STATIC_CAST(nsIBase*, this)|; but that disguises the fact that what
  160. * you are really doing is disambiguating the |nsISupports|. You could make
  161. * that more obvious with a double cast, e.g., |NS_STATIC_CAST(nsISupports*,
  162. * NS_STATIC_CAST(nsIBase*, this))|, but that is bulky and harder to read...
  163. *
  164. * The following macro is clean, short, and obvious. In the example above,
  165. * you would use it like this: |NS_ISUPPORTS_CAST(nsIBase*, this)|.
  166. */
  167. #define NS_ISUPPORTS_CAST(__unambiguousBase, __expr) \
  168. NS_STATIC_CAST(nsISupports*, NS_STATIC_CAST(__unambiguousBase, __expr))
  169. extern "C++" {
  170. // ...because some one is accidentally including this file inside
  171. // an |extern "C"|
  172. class nsISupports;
  173. template <class T>
  174. struct nsCOMTypeInfo
  175. {
  176. static const nsIID& GetIID() { return T::GetIID(); }
  177. };
  178. NS_SPECIALIZE_TEMPLATE
  179. struct nsCOMTypeInfo<nsISupports>
  180. {
  181. static const nsIID& GetIID() {
  182. static const nsIID iid_NS_ISUPPORTS_IID = NS_ISUPPORTS_IID; return iid_NS_ISUPPORTS_IID;
  183. }
  184. };
  185. #define NS_GET_IID(T) nsCOMTypeInfo<T>::GetIID()
  186. // a type-safe shortcut for calling the |QueryInterface()| member function
  187. template <class T, class DestinationType>
  188. inline
  189. nsresult
  190. CallQueryInterface( T* aSource, DestinationType** aDestination )
  191. {
  192. NS_PRECONDITION(aSource, "null parameter");
  193. NS_PRECONDITION(aDestination, "null parameter");
  194. return aSource->QueryInterface(NS_GET_IID(DestinationType),
  195. NS_REINTERPRET_CAST(void**, aDestination));
  196. }
  197. } // extern "C++"
  198. #endif /* __nsISupportsUtils_h */