Bug 1700051: part 30) Narrow scope of `newOffset`. r=smaug
[gecko.git] / xpcom / components / nsComponentManagerUtils.h
blobdc164f83b6abaa85613ddfa66f8c61b9c96c1a2a
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& aClass, nsISupports* aDelegate,
16 const nsIID& aIID, void** aResult);
18 nsresult CallCreateInstance(const char* aContractID, nsISupports* aDelegate,
19 const nsIID& aIID, void** aResult);
21 nsresult CallGetClassObject(const nsCID& aClass, 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, nsISupports* aDelegate,
125 DestinationType** aDestination) {
126 MOZ_ASSERT(aDestination, "null parameter");
128 return CallCreateInstance(aClass, aDelegate,
129 NS_GET_TEMPLATE_IID(DestinationType),
130 reinterpret_cast<void**>(aDestination));
133 template <class DestinationType>
134 inline nsresult CallCreateInstance(const nsCID& aClass,
135 DestinationType** aDestination) {
136 MOZ_ASSERT(aDestination, "null parameter");
138 return CallCreateInstance(aClass, nullptr,
139 NS_GET_TEMPLATE_IID(DestinationType),
140 reinterpret_cast<void**>(aDestination));
143 template <class DestinationType>
144 inline nsresult CallCreateInstance(const char* aContractID,
145 nsISupports* aDelegate,
146 DestinationType** aDestination) {
147 MOZ_ASSERT(aContractID, "null parameter");
148 MOZ_ASSERT(aDestination, "null parameter");
150 return CallCreateInstance(aContractID, aDelegate,
151 NS_GET_TEMPLATE_IID(DestinationType),
152 reinterpret_cast<void**>(aDestination));
155 template <class DestinationType>
156 inline nsresult CallCreateInstance(const char* aContractID,
157 DestinationType** aDestination) {
158 MOZ_ASSERT(aContractID, "null parameter");
159 MOZ_ASSERT(aDestination, "null parameter");
161 return CallCreateInstance(aContractID, nullptr,
162 NS_GET_TEMPLATE_IID(DestinationType),
163 reinterpret_cast<void**>(aDestination));
166 template <class DestinationType>
167 inline nsresult CallCreateInstance(nsIFactory* aFactory, nsISupports* aDelegate,
168 DestinationType** aDestination) {
169 MOZ_ASSERT(aFactory, "null parameter");
170 MOZ_ASSERT(aDestination, "null parameter");
172 return aFactory->CreateInstance(aDelegate,
173 NS_GET_TEMPLATE_IID(DestinationType),
174 reinterpret_cast<void**>(aDestination));
177 template <class DestinationType>
178 inline nsresult CallCreateInstance(nsIFactory* aFactory,
179 DestinationType** aDestination) {
180 MOZ_ASSERT(aFactory, "null parameter");
181 MOZ_ASSERT(aDestination, "null parameter");
183 return aFactory->CreateInstance(nullptr, NS_GET_TEMPLATE_IID(DestinationType),
184 reinterpret_cast<void**>(aDestination));
187 template <class DestinationType>
188 inline nsresult CallGetClassObject(const nsCID& aClass,
189 DestinationType** aDestination) {
190 MOZ_ASSERT(aDestination, "null parameter");
192 return CallGetClassObject(aClass, NS_GET_TEMPLATE_IID(DestinationType),
193 reinterpret_cast<void**>(aDestination));
196 template <class DestinationType>
197 inline nsresult CallGetClassObject(const char* aContractID,
198 DestinationType** aDestination) {
199 MOZ_ASSERT(aDestination, "null parameter");
201 return CallGetClassObject(aContractID, NS_GET_TEMPLATE_IID(DestinationType),
202 reinterpret_cast<void**>(aDestination));
205 #endif /* nsComponentManagerUtils_h__ */