nsComponentManagerUtils.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsComponentManagerUtils_h__
  6. #define nsComponentManagerUtils_h__
  7. #include "nscore.h"
  8. #include "nsCOMPtr.h"
  9. #include "nsIFactory.h"
  10. nsresult CallCreateInstance(const nsCID& aClass, nsISupports* aDelegate,
  11. const nsIID& aIID, void** aResult);
  12. nsresult CallCreateInstance(const char* aContractID, nsISupports* aDelegate,
  13. const nsIID& aIID, void** aResult);
  14. nsresult CallGetClassObject(const nsCID& aClass, const nsIID& aIID,
  15. void** aResult);
  16. nsresult CallGetClassObject(const char* aContractID, const nsIID& aIID,
  17. void** aResult);
  18. class MOZ_STACK_CLASS nsCreateInstanceByCID final : public nsCOMPtr_helper
  19. {
  20. public:
  21. nsCreateInstanceByCID(const nsCID& aCID, nsresult* aErrorPtr)
  22. : mCID(aCID)
  23. , mErrorPtr(aErrorPtr)
  24. {
  25. }
  26. virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const
  27. override;
  28. private:
  29. const nsCID& mCID;
  30. nsresult* mErrorPtr;
  31. };
  32. class MOZ_STACK_CLASS nsCreateInstanceByContractID final : public nsCOMPtr_helper
  33. {
  34. public:
  35. nsCreateInstanceByContractID(const char* aContractID, nsresult* aErrorPtr)
  36. : mContractID(aContractID)
  37. , mErrorPtr(aErrorPtr)
  38. {
  39. }
  40. virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
  41. private:
  42. const char* mContractID;
  43. nsresult* mErrorPtr;
  44. };
  45. class MOZ_STACK_CLASS nsCreateInstanceFromFactory final : public nsCOMPtr_helper
  46. {
  47. public:
  48. nsCreateInstanceFromFactory(nsIFactory* aFactory, nsresult* aErrorPtr)
  49. : mFactory(aFactory)
  50. , mErrorPtr(aErrorPtr)
  51. {
  52. }
  53. virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
  54. private:
  55. nsIFactory* MOZ_NON_OWNING_REF mFactory;
  56. nsresult* mErrorPtr;
  57. };
  58. inline const nsCreateInstanceByCID
  59. do_CreateInstance(const nsCID& aCID, nsresult* aError = 0)
  60. {
  61. return nsCreateInstanceByCID(aCID, aError);
  62. }
  63. inline const nsCreateInstanceByContractID
  64. do_CreateInstance(const char* aContractID, nsresult* aError = 0)
  65. {
  66. return nsCreateInstanceByContractID(aContractID, aError);
  67. }
  68. inline const nsCreateInstanceFromFactory
  69. do_CreateInstance(nsIFactory* aFactory, nsresult* aError = 0)
  70. {
  71. return nsCreateInstanceFromFactory(aFactory, aError);
  72. }
  73. class MOZ_STACK_CLASS nsGetClassObjectByCID final : public nsCOMPtr_helper
  74. {
  75. public:
  76. nsGetClassObjectByCID(const nsCID& aCID, nsresult* aErrorPtr)
  77. : mCID(aCID)
  78. , mErrorPtr(aErrorPtr)
  79. {
  80. }
  81. virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
  82. private:
  83. const nsCID& mCID;
  84. nsresult* mErrorPtr;
  85. };
  86. class MOZ_STACK_CLASS nsGetClassObjectByContractID final : public nsCOMPtr_helper
  87. {
  88. public:
  89. nsGetClassObjectByContractID(const char* aContractID, nsresult* aErrorPtr)
  90. : mContractID(aContractID)
  91. , mErrorPtr(aErrorPtr)
  92. {
  93. }
  94. virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
  95. private:
  96. const char* mContractID;
  97. nsresult* mErrorPtr;
  98. };
  99. /**
  100. * do_GetClassObject can be used to improve performance of callers
  101. * that call |CreateInstance| many times. They can cache the factory
  102. * and call do_CreateInstance or CallCreateInstance with the cached
  103. * factory rather than having the component manager retrieve it every
  104. * time.
  105. */
  106. inline const nsGetClassObjectByCID
  107. do_GetClassObject(const nsCID& aCID, nsresult* aError = 0)
  108. {
  109. return nsGetClassObjectByCID(aCID, aError);
  110. }
  111. inline const nsGetClassObjectByContractID
  112. do_GetClassObject(const char* aContractID, nsresult* aError = 0)
  113. {
  114. return nsGetClassObjectByContractID(aContractID, aError);
  115. }
  116. // type-safe shortcuts for calling |CreateInstance|
  117. template<class DestinationType>
  118. inline nsresult
  119. CallCreateInstance(const nsCID& aClass,
  120. nsISupports* aDelegate,
  121. DestinationType** aDestination)
  122. {
  123. NS_PRECONDITION(aDestination, "null parameter");
  124. return CallCreateInstance(aClass, aDelegate,
  125. NS_GET_TEMPLATE_IID(DestinationType),
  126. reinterpret_cast<void**>(aDestination));
  127. }
  128. template<class DestinationType>
  129. inline nsresult
  130. CallCreateInstance(const nsCID& aClass, DestinationType** aDestination)
  131. {
  132. NS_PRECONDITION(aDestination, "null parameter");
  133. return CallCreateInstance(aClass, nullptr,
  134. NS_GET_TEMPLATE_IID(DestinationType),
  135. reinterpret_cast<void**>(aDestination));
  136. }
  137. template<class DestinationType>
  138. inline nsresult
  139. CallCreateInstance(const char* aContractID,
  140. nsISupports* aDelegate,
  141. DestinationType** aDestination)
  142. {
  143. NS_PRECONDITION(aContractID, "null parameter");
  144. NS_PRECONDITION(aDestination, "null parameter");
  145. return CallCreateInstance(aContractID,
  146. aDelegate,
  147. NS_GET_TEMPLATE_IID(DestinationType),
  148. reinterpret_cast<void**>(aDestination));
  149. }
  150. template<class DestinationType>
  151. inline nsresult
  152. CallCreateInstance(const char* aContractID, DestinationType** aDestination)
  153. {
  154. NS_PRECONDITION(aContractID, "null parameter");
  155. NS_PRECONDITION(aDestination, "null parameter");
  156. return CallCreateInstance(aContractID, nullptr,
  157. NS_GET_TEMPLATE_IID(DestinationType),
  158. reinterpret_cast<void**>(aDestination));
  159. }
  160. template<class DestinationType>
  161. inline nsresult
  162. CallCreateInstance(nsIFactory* aFactory,
  163. nsISupports* aDelegate,
  164. DestinationType** aDestination)
  165. {
  166. NS_PRECONDITION(aFactory, "null parameter");
  167. NS_PRECONDITION(aDestination, "null parameter");
  168. return aFactory->CreateInstance(aDelegate,
  169. NS_GET_TEMPLATE_IID(DestinationType),
  170. reinterpret_cast<void**>(aDestination));
  171. }
  172. template<class DestinationType>
  173. inline nsresult
  174. CallCreateInstance(nsIFactory* aFactory, DestinationType** aDestination)
  175. {
  176. NS_PRECONDITION(aFactory, "null parameter");
  177. NS_PRECONDITION(aDestination, "null parameter");
  178. return aFactory->CreateInstance(nullptr,
  179. NS_GET_TEMPLATE_IID(DestinationType),
  180. reinterpret_cast<void**>(aDestination));
  181. }
  182. template<class DestinationType>
  183. inline nsresult
  184. CallGetClassObject(const nsCID& aClass, DestinationType** aDestination)
  185. {
  186. NS_PRECONDITION(aDestination, "null parameter");
  187. return CallGetClassObject(aClass, NS_GET_TEMPLATE_IID(DestinationType),
  188. reinterpret_cast<void**>(aDestination));
  189. }
  190. template<class DestinationType>
  191. inline nsresult
  192. CallGetClassObject(const char* aContractID, DestinationType** aDestination)
  193. {
  194. NS_PRECONDITION(aDestination, "null parameter");
  195. return CallGetClassObject(aContractID, NS_GET_TEMPLATE_IID(DestinationType),
  196. reinterpret_cast<void**>(aDestination));
  197. }
  198. #endif /* nsComponentManagerUtils_h__ */