PageRenderTime 123ms CodeModel.GetById 108ms RepoModel.GetById 1ms app.codeStats 0ms

/gecko_api/include/nsISupportsUtils.h

http://firefox-mac-pdf.googlecode.com/
C Header | 206 lines | 71 code | 26 blank | 109 comment | 3 complexity | 582ddf987451352329be03526a64d284 MD5 | raw file
  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 <pp@ludusdesign.com>
  24. * Scott Collins <scc@ScottCollins.net>
  25. * Dan Mosedale <dmose@mozilla.org>
  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. * |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., |static_cast<nsISupports*>
  162. (* 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. static_cast<nsISupports*>(static_cast<__unambiguousBase>(__expr))
  169. // a type-safe shortcut for calling the |QueryInterface()| member function
  170. template <class T, class DestinationType>
  171. inline
  172. nsresult
  173. CallQueryInterface( T* aSource, DestinationType** aDestination )
  174. {
  175. NS_PRECONDITION(aSource, "null parameter");
  176. NS_PRECONDITION(aDestination, "null parameter");
  177. return aSource->QueryInterface(NS_GET_TEMPLATE_IID(DestinationType),
  178. reinterpret_cast<void**>(aDestination));
  179. }
  180. #endif /* __nsISupportsUtils_h */