Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/. */
6 #ifndef nsComponentManagerUtils_h__
7 #define nsComponentManagerUtils_h__
9 #include "nscore.h"
10 #include "nsCOMPtr.h"
12 #include "nsIFactory.h"
15 NS_COM_GLUE nsresult
16 CallCreateInstance
17 (const nsCID &aClass, nsISupports *aDelegate, const nsIID &aIID,
18 void **aResult);
20 NS_COM_GLUE nsresult
21 CallCreateInstance
22 (const char *aContractID, nsISupports *aDelegate, const nsIID &aIID,
23 void **aResult);
25 NS_COM_GLUE nsresult
26 CallGetClassObject
27 (const nsCID &aClass, const nsIID &aIID, void **aResult);
29 NS_COM_GLUE nsresult
30 CallGetClassObject
31 (const char *aContractID, const nsIID &aIID, void **aResult);
34 class NS_COM_GLUE nsCreateInstanceByCID : public nsCOMPtr_helper
35 {
36 public:
37 nsCreateInstanceByCID( const nsCID& aCID, nsISupports* aOuter, nsresult* aErrorPtr )
38 : mCID(aCID),
39 mOuter(aOuter),
40 mErrorPtr(aErrorPtr)
41 {
42 // nothing else to do here
43 }
45 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
47 private:
48 const nsCID& mCID;
49 nsISupports* mOuter;
50 nsresult* mErrorPtr;
51 };
53 class NS_COM_GLUE nsCreateInstanceByContractID : public nsCOMPtr_helper
54 {
55 public:
56 nsCreateInstanceByContractID( const char* aContractID, nsISupports* aOuter, nsresult* aErrorPtr )
57 : mContractID(aContractID),
58 mOuter(aOuter),
59 mErrorPtr(aErrorPtr)
60 {
61 // nothing else to do here
62 }
64 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
66 private:
67 const char* mContractID;
68 nsISupports* mOuter;
69 nsresult* mErrorPtr;
70 };
72 class NS_COM_GLUE nsCreateInstanceFromFactory : public nsCOMPtr_helper
73 {
74 public:
75 nsCreateInstanceFromFactory( nsIFactory* aFactory, nsISupports* aOuter, nsresult* aErrorPtr )
76 : mFactory(aFactory),
77 mOuter(aOuter),
78 mErrorPtr(aErrorPtr)
79 {
80 // nothing else to do here
81 }
83 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
85 private:
86 nsIFactory* mFactory;
87 nsISupports* mOuter;
88 nsresult* mErrorPtr;
89 };
92 inline
93 const nsCreateInstanceByCID
94 do_CreateInstance( const nsCID& aCID, nsresult* error = 0 )
95 {
96 return nsCreateInstanceByCID(aCID, 0, error);
97 }
99 inline
100 const nsCreateInstanceByCID
101 do_CreateInstance( const nsCID& aCID, nsISupports* aOuter, nsresult* error = 0 )
102 {
103 return nsCreateInstanceByCID(aCID, aOuter, error);
104 }
106 inline
107 const nsCreateInstanceByContractID
108 do_CreateInstance( const char* aContractID, nsresult* error = 0 )
109 {
110 return nsCreateInstanceByContractID(aContractID, 0, error);
111 }
113 inline
114 const nsCreateInstanceByContractID
115 do_CreateInstance( const char* aContractID, nsISupports* aOuter, nsresult* error = 0 )
116 {
117 return nsCreateInstanceByContractID(aContractID, aOuter, error);
118 }
120 inline
121 const nsCreateInstanceFromFactory
122 do_CreateInstance( nsIFactory* aFactory, nsresult* error = 0 )
123 {
124 return nsCreateInstanceFromFactory(aFactory, 0, error);
125 }
127 inline
128 const nsCreateInstanceFromFactory
129 do_CreateInstance( nsIFactory* aFactory, nsISupports* aOuter, nsresult* error = 0 )
130 {
131 return nsCreateInstanceFromFactory(aFactory, aOuter, error);
132 }
135 class NS_COM_GLUE nsGetClassObjectByCID : public nsCOMPtr_helper
136 {
137 public:
138 nsGetClassObjectByCID( const nsCID& aCID, nsresult* aErrorPtr )
139 : mCID(aCID),
140 mErrorPtr(aErrorPtr)
141 {
142 // nothing else to do here
143 }
145 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
147 private:
148 const nsCID& mCID;
149 nsresult* mErrorPtr;
150 };
152 class NS_COM_GLUE nsGetClassObjectByContractID : public nsCOMPtr_helper
153 {
154 public:
155 nsGetClassObjectByContractID( const char* aContractID, nsresult* aErrorPtr )
156 : mContractID(aContractID),
157 mErrorPtr(aErrorPtr)
158 {
159 // nothing else to do here
160 }
162 virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
164 private:
165 const char* mContractID;
166 nsresult* mErrorPtr;
167 };
169 /**
170 * do_GetClassObject can be used to improve performance of callers
171 * that call |CreateInstance| many times. They can cache the factory
172 * and call do_CreateInstance or CallCreateInstance with the cached
173 * factory rather than having the component manager retrieve it every
174 * time.
175 */
176 inline const nsGetClassObjectByCID
177 do_GetClassObject( const nsCID& aCID, nsresult* error = 0 )
178 {
179 return nsGetClassObjectByCID(aCID, error);
180 }
182 inline const nsGetClassObjectByContractID
183 do_GetClassObject( const char* aContractID, nsresult* error = 0 )
184 {
185 return nsGetClassObjectByContractID(aContractID, error);
186 }
188 // type-safe shortcuts for calling |CreateInstance|
189 template <class DestinationType>
190 inline
191 nsresult
192 CallCreateInstance( const nsCID &aClass,
193 nsISupports *aDelegate,
194 DestinationType** aDestination )
195 {
196 NS_PRECONDITION(aDestination, "null parameter");
198 return CallCreateInstance(aClass, aDelegate,
199 NS_GET_TEMPLATE_IID(DestinationType),
200 reinterpret_cast<void**>(aDestination));
201 }
203 template <class DestinationType>
204 inline
205 nsresult
206 CallCreateInstance( const nsCID &aClass,
207 DestinationType** aDestination )
208 {
209 NS_PRECONDITION(aDestination, "null parameter");
211 return CallCreateInstance(aClass, nullptr,
212 NS_GET_TEMPLATE_IID(DestinationType),
213 reinterpret_cast<void**>(aDestination));
214 }
216 template <class DestinationType>
217 inline
218 nsresult
219 CallCreateInstance( const char *aContractID,
220 nsISupports *aDelegate,
221 DestinationType** aDestination )
222 {
223 NS_PRECONDITION(aContractID, "null parameter");
224 NS_PRECONDITION(aDestination, "null parameter");
226 return CallCreateInstance(aContractID,
227 aDelegate,
228 NS_GET_TEMPLATE_IID(DestinationType),
229 reinterpret_cast<void**>(aDestination));
230 }
232 template <class DestinationType>
233 inline
234 nsresult
235 CallCreateInstance( const char *aContractID,
236 DestinationType** aDestination )
237 {
238 NS_PRECONDITION(aContractID, "null parameter");
239 NS_PRECONDITION(aDestination, "null parameter");
241 return CallCreateInstance(aContractID, nullptr,
242 NS_GET_TEMPLATE_IID(DestinationType),
243 reinterpret_cast<void**>(aDestination));
244 }
246 template <class DestinationType>
247 inline
248 nsresult
249 CallCreateInstance( nsIFactory *aFactory,
250 nsISupports *aDelegate,
251 DestinationType** aDestination )
252 {
253 NS_PRECONDITION(aFactory, "null parameter");
254 NS_PRECONDITION(aDestination, "null parameter");
256 return aFactory->CreateInstance(aDelegate,
257 NS_GET_TEMPLATE_IID(DestinationType),
258 reinterpret_cast<void**>(aDestination));
259 }
261 template <class DestinationType>
262 inline
263 nsresult
264 CallCreateInstance( nsIFactory *aFactory,
265 DestinationType** aDestination )
266 {
267 NS_PRECONDITION(aFactory, "null parameter");
268 NS_PRECONDITION(aDestination, "null parameter");
270 return aFactory->CreateInstance(nullptr,
271 NS_GET_TEMPLATE_IID(DestinationType),
272 reinterpret_cast<void**>(aDestination));
273 }
275 template <class DestinationType>
276 inline
277 nsresult
278 CallGetClassObject( const nsCID &aClass,
279 DestinationType** aDestination )
280 {
281 NS_PRECONDITION(aDestination, "null parameter");
283 return CallGetClassObject(aClass,
284 NS_GET_TEMPLATE_IID(DestinationType), reinterpret_cast<void**>(aDestination));
285 }
287 template <class DestinationType>
288 inline
289 nsresult
290 CallGetClassObject( const char* aContractID,
291 DestinationType** aDestination )
292 {
293 NS_PRECONDITION(aDestination, "null parameter");
295 return CallGetClassObject(aContractID,
296 NS_GET_TEMPLATE_IID(DestinationType), reinterpret_cast<void**>(aDestination));
297 }
299 #endif /* nsComponentManagerUtils_h__ */