Bug 1910362 - Create new Nimbus helper r=aaronmt,ohorvath
[gecko.git] / xpcom / components / nsComponentManagerUtils.h
blobb6f15aabaae076f1299980ac8399b3bbe1413666
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsComponentManagerUtils_h__
8 #define nsComponentManagerUtils_h__
10 #include "nscore.h"
11 #include "nsCOMPtr.h"
13 #include "nsIFactory.h"
15 nsresult CallCreateInstance(const nsCID& aCID, const nsIID& aIID,
16 void** aResult);
18 nsresult CallCreateInstance(const char* aContractID, const nsIID& aIID,
19 void** aResult);
21 nsresult CallGetClassObject(const nsCID& aCID, const nsIID& aIID,
22 void** aResult);
24 nsresult CallGetClassObject(const char* aContractID, const nsIID& aIID,
25 void** aResult);
27 class MOZ_STACK_CLASS nsCreateInstanceByCID final : public nsCOMPtr_helper {
28 public:
29 nsCreateInstanceByCID(const nsCID& aCID, nsresult* aErrorPtr)
30 : mCID(aCID), mErrorPtr(aErrorPtr) {}
32 virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
34 private:
35 const nsCID& mCID;
36 nsresult* mErrorPtr;
39 class MOZ_STACK_CLASS nsCreateInstanceByContractID final
40 : public nsCOMPtr_helper {
41 public:
42 nsCreateInstanceByContractID(const char* aContractID, nsresult* aErrorPtr)
43 : mContractID(aContractID), mErrorPtr(aErrorPtr) {}
45 virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
47 private:
48 const char* mContractID;
49 nsresult* mErrorPtr;
52 class MOZ_STACK_CLASS nsCreateInstanceFromFactory final
53 : public nsCOMPtr_helper {
54 public:
55 nsCreateInstanceFromFactory(nsIFactory* aFactory, nsresult* aErrorPtr)
56 : mFactory(aFactory), mErrorPtr(aErrorPtr) {}
58 virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
60 private:
61 nsIFactory* MOZ_NON_OWNING_REF mFactory;
62 nsresult* mErrorPtr;
65 inline const nsCreateInstanceByCID do_CreateInstance(const nsCID& aCID,
66 nsresult* aError = 0) {
67 return nsCreateInstanceByCID(aCID, aError);
70 inline const nsCreateInstanceByContractID do_CreateInstance(
71 const char* aContractID, nsresult* aError = 0) {
72 return nsCreateInstanceByContractID(aContractID, aError);
75 inline const nsCreateInstanceFromFactory do_CreateInstance(
76 nsIFactory* aFactory, nsresult* aError = 0) {
77 return nsCreateInstanceFromFactory(aFactory, aError);
80 class MOZ_STACK_CLASS nsGetClassObjectByCID final : public nsCOMPtr_helper {
81 public:
82 nsGetClassObjectByCID(const nsCID& aCID, nsresult* aErrorPtr)
83 : mCID(aCID), mErrorPtr(aErrorPtr) {}
85 virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
87 private:
88 const nsCID& mCID;
89 nsresult* mErrorPtr;
92 class MOZ_STACK_CLASS nsGetClassObjectByContractID final
93 : public nsCOMPtr_helper {
94 public:
95 nsGetClassObjectByContractID(const char* aContractID, nsresult* aErrorPtr)
96 : mContractID(aContractID), mErrorPtr(aErrorPtr) {}
98 virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
100 private:
101 const char* mContractID;
102 nsresult* mErrorPtr;
106 * do_GetClassObject can be used to improve performance of callers
107 * that call |CreateInstance| many times. They can cache the factory
108 * and call do_CreateInstance or CallCreateInstance with the cached
109 * factory rather than having the component manager retrieve it every
110 * time.
112 inline const nsGetClassObjectByCID do_GetClassObject(const nsCID& aCID,
113 nsresult* aError = 0) {
114 return nsGetClassObjectByCID(aCID, aError);
117 inline const nsGetClassObjectByContractID do_GetClassObject(
118 const char* aContractID, nsresult* aError = 0) {
119 return nsGetClassObjectByContractID(aContractID, aError);
122 // type-safe shortcuts for calling |CreateInstance|
123 template <class DestinationType>
124 inline nsresult CallCreateInstance(const nsCID& aClass,
125 DestinationType** aDestination) {
126 MOZ_ASSERT(aDestination, "null parameter");
128 return CallCreateInstance(aClass, NS_GET_TEMPLATE_IID(DestinationType),
129 reinterpret_cast<void**>(aDestination));
132 template <class DestinationType>
133 inline nsresult CallCreateInstance(const char* aContractID,
134 DestinationType** aDestination) {
135 MOZ_ASSERT(aContractID, "null parameter");
136 MOZ_ASSERT(aDestination, "null parameter");
138 return CallCreateInstance(aContractID, NS_GET_TEMPLATE_IID(DestinationType),
139 reinterpret_cast<void**>(aDestination));
142 template <class DestinationType>
143 inline nsresult CallCreateInstance(nsIFactory* aFactory,
144 DestinationType** aDestination) {
145 MOZ_ASSERT(aFactory, "null parameter");
146 MOZ_ASSERT(aDestination, "null parameter");
148 return aFactory->CreateInstance(nullptr, NS_GET_TEMPLATE_IID(DestinationType),
149 reinterpret_cast<void**>(aDestination));
152 template <class DestinationType>
153 inline nsresult CallGetClassObject(const nsCID& aClass,
154 DestinationType** aDestination) {
155 MOZ_ASSERT(aDestination, "null parameter");
157 return CallGetClassObject(aClass, NS_GET_TEMPLATE_IID(DestinationType),
158 reinterpret_cast<void**>(aDestination));
161 template <class DestinationType>
162 inline nsresult CallGetClassObject(const char* aContractID,
163 DestinationType** aDestination) {
164 MOZ_ASSERT(aDestination, "null parameter");
166 return CallGetClassObject(aContractID, NS_GET_TEMPLATE_IID(DestinationType),
167 reinterpret_cast<void**>(aDestination));
170 #endif /* nsComponentManagerUtils_h__ */