nsComponentManagerUtils.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either of the GNU General Public License Version 2 or later (the "GPL"),
  26. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef nsComponentManagerUtils_h__
  38. #define nsComponentManagerUtils_h__
  39. #ifndef nscore_h__
  40. #include "nscore.h"
  41. #endif
  42. #ifndef nsCOMPtr_h__
  43. #include "nsCOMPtr.h"
  44. #endif
  45. #include "nsIFactory.h"
  46. NS_COM_GLUE nsresult
  47. CallCreateInstance
  48. (const nsCID &aClass, nsISupports *aDelegate, const nsIID &aIID,
  49. void **aResult);
  50. NS_COM_GLUE nsresult
  51. CallCreateInstance
  52. (const char *aContractID, nsISupports *aDelegate, const nsIID &aIID,
  53. void **aResult);
  54. NS_COM_GLUE nsresult
  55. CallGetClassObject
  56. (const nsCID &aClass, const nsIID &aIID, void **aResult);
  57. NS_COM_GLUE nsresult
  58. CallGetClassObject
  59. (const char *aContractID, const nsIID &aIID, void **aResult);
  60. class NS_COM_GLUE nsCreateInstanceByCID : public nsCOMPtr_helper
  61. {
  62. public:
  63. nsCreateInstanceByCID( const nsCID& aCID, nsISupports* aOuter, nsresult* aErrorPtr )
  64. : mCID(aCID),
  65. mOuter(aOuter),
  66. mErrorPtr(aErrorPtr)
  67. {
  68. // nothing else to do here
  69. }
  70. virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
  71. private:
  72. const nsCID& mCID;
  73. nsISupports* mOuter;
  74. nsresult* mErrorPtr;
  75. };
  76. class NS_COM_GLUE nsCreateInstanceByContractID : public nsCOMPtr_helper
  77. {
  78. public:
  79. nsCreateInstanceByContractID( const char* aContractID, nsISupports* aOuter, nsresult* aErrorPtr )
  80. : mContractID(aContractID),
  81. mOuter(aOuter),
  82. mErrorPtr(aErrorPtr)
  83. {
  84. // nothing else to do here
  85. }
  86. virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
  87. private:
  88. const char* mContractID;
  89. nsISupports* mOuter;
  90. nsresult* mErrorPtr;
  91. };
  92. class NS_COM_GLUE nsCreateInstanceFromFactory : public nsCOMPtr_helper
  93. {
  94. public:
  95. nsCreateInstanceFromFactory( nsIFactory* aFactory, nsISupports* aOuter, nsresult* aErrorPtr )
  96. : mFactory(aFactory),
  97. mOuter(aOuter),
  98. mErrorPtr(aErrorPtr)
  99. {
  100. // nothing else to do here
  101. }
  102. virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
  103. private:
  104. nsIFactory* mFactory;
  105. nsISupports* mOuter;
  106. nsresult* mErrorPtr;
  107. };
  108. inline
  109. const nsCreateInstanceByCID
  110. do_CreateInstance( const nsCID& aCID, nsresult* error = 0 )
  111. {
  112. return nsCreateInstanceByCID(aCID, 0, error);
  113. }
  114. inline
  115. const nsCreateInstanceByCID
  116. do_CreateInstance( const nsCID& aCID, nsISupports* aOuter, nsresult* error = 0 )
  117. {
  118. return nsCreateInstanceByCID(aCID, aOuter, error);
  119. }
  120. inline
  121. const nsCreateInstanceByContractID
  122. do_CreateInstance( const char* aContractID, nsresult* error = 0 )
  123. {
  124. return nsCreateInstanceByContractID(aContractID, 0, error);
  125. }
  126. inline
  127. const nsCreateInstanceByContractID
  128. do_CreateInstance( const char* aContractID, nsISupports* aOuter, nsresult* error = 0 )
  129. {
  130. return nsCreateInstanceByContractID(aContractID, aOuter, error);
  131. }
  132. inline
  133. const nsCreateInstanceFromFactory
  134. do_CreateInstance( nsIFactory* aFactory, nsresult* error = 0 )
  135. {
  136. return nsCreateInstanceFromFactory(aFactory, 0, error);
  137. }
  138. inline
  139. const nsCreateInstanceFromFactory
  140. do_CreateInstance( nsIFactory* aFactory, nsISupports* aOuter, nsresult* error = 0 )
  141. {
  142. return nsCreateInstanceFromFactory(aFactory, aOuter, error);
  143. }
  144. class NS_COM_GLUE nsGetClassObjectByCID : public nsCOMPtr_helper
  145. {
  146. public:
  147. nsGetClassObjectByCID( const nsCID& aCID, nsresult* aErrorPtr )
  148. : mCID(aCID),
  149. mErrorPtr(aErrorPtr)
  150. {
  151. // nothing else to do here
  152. }
  153. virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
  154. private:
  155. const nsCID& mCID;
  156. nsresult* mErrorPtr;
  157. };
  158. class NS_COM_GLUE nsGetClassObjectByContractID : public nsCOMPtr_helper
  159. {
  160. public:
  161. nsGetClassObjectByContractID( const char* aContractID, nsresult* aErrorPtr )
  162. : mContractID(aContractID),
  163. mErrorPtr(aErrorPtr)
  164. {
  165. // nothing else to do here
  166. }
  167. virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
  168. private:
  169. const char* mContractID;
  170. nsresult* mErrorPtr;
  171. };
  172. /**
  173. * do_GetClassObject can be used to improve performance of callers
  174. * that call |CreateInstance| many times. They can cache the factory
  175. * and call do_CreateInstance or CallCreateInstance with the cached
  176. * factory rather than having the component manager retrieve it every
  177. * time.
  178. */
  179. inline const nsGetClassObjectByCID
  180. do_GetClassObject( const nsCID& aCID, nsresult* error = 0 )
  181. {
  182. return nsGetClassObjectByCID(aCID, error);
  183. }
  184. inline const nsGetClassObjectByContractID
  185. do_GetClassObject( const char* aContractID, nsresult* error = 0 )
  186. {
  187. return nsGetClassObjectByContractID(aContractID, error);
  188. }
  189. // type-safe shortcuts for calling |CreateInstance|
  190. template <class DestinationType>
  191. inline
  192. nsresult
  193. CallCreateInstance( const nsCID &aClass,
  194. nsISupports *aDelegate,
  195. DestinationType** aDestination )
  196. {
  197. NS_PRECONDITION(aDestination, "null parameter");
  198. return CallCreateInstance(aClass, aDelegate,
  199. NS_GET_IID(DestinationType),
  200. NS_REINTERPRET_CAST(void**, aDestination));
  201. }
  202. template <class DestinationType>
  203. inline
  204. nsresult
  205. CallCreateInstance( const nsCID &aClass,
  206. DestinationType** aDestination )
  207. {
  208. NS_PRECONDITION(aDestination, "null parameter");
  209. return CallCreateInstance(aClass, nsnull,
  210. NS_GET_IID(DestinationType),
  211. NS_REINTERPRET_CAST(void**, aDestination));
  212. }
  213. template <class DestinationType>
  214. inline
  215. nsresult
  216. CallCreateInstance( const char *aContractID,
  217. nsISupports *aDelegate,
  218. DestinationType** aDestination )
  219. {
  220. NS_PRECONDITION(aContractID, "null parameter");
  221. NS_PRECONDITION(aDestination, "null parameter");
  222. return CallCreateInstance(aContractID,
  223. aDelegate,
  224. NS_GET_IID(DestinationType),
  225. NS_REINTERPRET_CAST(void**, aDestination));
  226. }
  227. template <class DestinationType>
  228. inline
  229. nsresult
  230. CallCreateInstance( const char *aContractID,
  231. DestinationType** aDestination )
  232. {
  233. NS_PRECONDITION(aContractID, "null parameter");
  234. NS_PRECONDITION(aDestination, "null parameter");
  235. return CallCreateInstance(aContractID, nsnull,
  236. NS_GET_IID(DestinationType),
  237. NS_REINTERPRET_CAST(void**, aDestination));
  238. }
  239. template <class DestinationType>
  240. inline
  241. nsresult
  242. CallCreateInstance( nsIFactory *aFactory,
  243. nsISupports *aDelegate,
  244. DestinationType** aDestination )
  245. {
  246. NS_PRECONDITION(aFactory, "null parameter");
  247. NS_PRECONDITION(aDestination, "null parameter");
  248. return aFactory->CreateInstance(aDelegate,
  249. NS_GET_IID(DestinationType),
  250. NS_REINTERPRET_CAST(void**, aDestination));
  251. }
  252. template <class DestinationType>
  253. inline
  254. nsresult
  255. CallCreateInstance( nsIFactory *aFactory,
  256. DestinationType** aDestination )
  257. {
  258. NS_PRECONDITION(aFactory, "null parameter");
  259. NS_PRECONDITION(aDestination, "null parameter");
  260. return aFactory->CreateInstance(nsnull,
  261. NS_GET_IID(DestinationType),
  262. NS_REINTERPRET_CAST(void**, aDestination));
  263. }
  264. template <class DestinationType>
  265. inline
  266. nsresult
  267. CallGetClassObject( const nsCID &aClass,
  268. DestinationType** aDestination )
  269. {
  270. NS_PRECONDITION(aDestination, "null parameter");
  271. return CallGetClassObject(aClass,
  272. NS_GET_IID(DestinationType), NS_REINTERPRET_CAST(void**, aDestination));
  273. }
  274. template <class DestinationType>
  275. inline
  276. nsresult
  277. CallGetClassObject( const char* aContractID,
  278. DestinationType** aDestination )
  279. {
  280. NS_PRECONDITION(aDestination, "null parameter");
  281. return CallGetClassObject(aContractID,
  282. NS_GET_IID(DestinationType), NS_REINTERPRET_CAST(void**, aDestination));
  283. }
  284. #endif /* nsComponentManagerUtils_h__ */