1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsComponentManagerUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,299 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsComponentManagerUtils_h__ 1.10 +#define nsComponentManagerUtils_h__ 1.11 + 1.12 +#include "nscore.h" 1.13 +#include "nsCOMPtr.h" 1.14 + 1.15 +#include "nsIFactory.h" 1.16 + 1.17 + 1.18 +NS_COM_GLUE nsresult 1.19 +CallCreateInstance 1.20 + (const nsCID &aClass, nsISupports *aDelegate, const nsIID &aIID, 1.21 + void **aResult); 1.22 + 1.23 +NS_COM_GLUE nsresult 1.24 +CallCreateInstance 1.25 + (const char *aContractID, nsISupports *aDelegate, const nsIID &aIID, 1.26 + void **aResult); 1.27 + 1.28 +NS_COM_GLUE nsresult 1.29 +CallGetClassObject 1.30 + (const nsCID &aClass, const nsIID &aIID, void **aResult); 1.31 + 1.32 +NS_COM_GLUE nsresult 1.33 +CallGetClassObject 1.34 + (const char *aContractID, const nsIID &aIID, void **aResult); 1.35 + 1.36 + 1.37 +class NS_COM_GLUE nsCreateInstanceByCID : public nsCOMPtr_helper 1.38 +{ 1.39 +public: 1.40 + nsCreateInstanceByCID( const nsCID& aCID, nsISupports* aOuter, nsresult* aErrorPtr ) 1.41 + : mCID(aCID), 1.42 + mOuter(aOuter), 1.43 + mErrorPtr(aErrorPtr) 1.44 + { 1.45 + // nothing else to do here 1.46 + } 1.47 + 1.48 + virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; 1.49 + 1.50 +private: 1.51 + const nsCID& mCID; 1.52 + nsISupports* mOuter; 1.53 + nsresult* mErrorPtr; 1.54 +}; 1.55 + 1.56 +class NS_COM_GLUE nsCreateInstanceByContractID : public nsCOMPtr_helper 1.57 +{ 1.58 +public: 1.59 + nsCreateInstanceByContractID( const char* aContractID, nsISupports* aOuter, nsresult* aErrorPtr ) 1.60 + : mContractID(aContractID), 1.61 + mOuter(aOuter), 1.62 + mErrorPtr(aErrorPtr) 1.63 + { 1.64 + // nothing else to do here 1.65 + } 1.66 + 1.67 + virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; 1.68 + 1.69 +private: 1.70 + const char* mContractID; 1.71 + nsISupports* mOuter; 1.72 + nsresult* mErrorPtr; 1.73 +}; 1.74 + 1.75 +class NS_COM_GLUE nsCreateInstanceFromFactory : public nsCOMPtr_helper 1.76 +{ 1.77 +public: 1.78 + nsCreateInstanceFromFactory( nsIFactory* aFactory, nsISupports* aOuter, nsresult* aErrorPtr ) 1.79 + : mFactory(aFactory), 1.80 + mOuter(aOuter), 1.81 + mErrorPtr(aErrorPtr) 1.82 + { 1.83 + // nothing else to do here 1.84 + } 1.85 + 1.86 + virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; 1.87 + 1.88 +private: 1.89 + nsIFactory* mFactory; 1.90 + nsISupports* mOuter; 1.91 + nsresult* mErrorPtr; 1.92 +}; 1.93 + 1.94 + 1.95 +inline 1.96 +const nsCreateInstanceByCID 1.97 +do_CreateInstance( const nsCID& aCID, nsresult* error = 0 ) 1.98 +{ 1.99 + return nsCreateInstanceByCID(aCID, 0, error); 1.100 +} 1.101 + 1.102 +inline 1.103 +const nsCreateInstanceByCID 1.104 +do_CreateInstance( const nsCID& aCID, nsISupports* aOuter, nsresult* error = 0 ) 1.105 +{ 1.106 + return nsCreateInstanceByCID(aCID, aOuter, error); 1.107 +} 1.108 + 1.109 +inline 1.110 +const nsCreateInstanceByContractID 1.111 +do_CreateInstance( const char* aContractID, nsresult* error = 0 ) 1.112 +{ 1.113 + return nsCreateInstanceByContractID(aContractID, 0, error); 1.114 +} 1.115 + 1.116 +inline 1.117 +const nsCreateInstanceByContractID 1.118 +do_CreateInstance( const char* aContractID, nsISupports* aOuter, nsresult* error = 0 ) 1.119 +{ 1.120 + return nsCreateInstanceByContractID(aContractID, aOuter, error); 1.121 +} 1.122 + 1.123 +inline 1.124 +const nsCreateInstanceFromFactory 1.125 +do_CreateInstance( nsIFactory* aFactory, nsresult* error = 0 ) 1.126 +{ 1.127 + return nsCreateInstanceFromFactory(aFactory, 0, error); 1.128 +} 1.129 + 1.130 +inline 1.131 +const nsCreateInstanceFromFactory 1.132 +do_CreateInstance( nsIFactory* aFactory, nsISupports* aOuter, nsresult* error = 0 ) 1.133 +{ 1.134 + return nsCreateInstanceFromFactory(aFactory, aOuter, error); 1.135 +} 1.136 + 1.137 + 1.138 +class NS_COM_GLUE nsGetClassObjectByCID : public nsCOMPtr_helper 1.139 +{ 1.140 +public: 1.141 + nsGetClassObjectByCID( const nsCID& aCID, nsresult* aErrorPtr ) 1.142 + : mCID(aCID), 1.143 + mErrorPtr(aErrorPtr) 1.144 + { 1.145 + // nothing else to do here 1.146 + } 1.147 + 1.148 + virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; 1.149 + 1.150 +private: 1.151 + const nsCID& mCID; 1.152 + nsresult* mErrorPtr; 1.153 +}; 1.154 + 1.155 +class NS_COM_GLUE nsGetClassObjectByContractID : public nsCOMPtr_helper 1.156 +{ 1.157 +public: 1.158 + nsGetClassObjectByContractID( const char* aContractID, nsresult* aErrorPtr ) 1.159 + : mContractID(aContractID), 1.160 + mErrorPtr(aErrorPtr) 1.161 + { 1.162 + // nothing else to do here 1.163 + } 1.164 + 1.165 + virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; 1.166 + 1.167 +private: 1.168 + const char* mContractID; 1.169 + nsresult* mErrorPtr; 1.170 +}; 1.171 + 1.172 +/** 1.173 + * do_GetClassObject can be used to improve performance of callers 1.174 + * that call |CreateInstance| many times. They can cache the factory 1.175 + * and call do_CreateInstance or CallCreateInstance with the cached 1.176 + * factory rather than having the component manager retrieve it every 1.177 + * time. 1.178 + */ 1.179 +inline const nsGetClassObjectByCID 1.180 +do_GetClassObject( const nsCID& aCID, nsresult* error = 0 ) 1.181 +{ 1.182 + return nsGetClassObjectByCID(aCID, error); 1.183 +} 1.184 + 1.185 +inline const nsGetClassObjectByContractID 1.186 +do_GetClassObject( const char* aContractID, nsresult* error = 0 ) 1.187 +{ 1.188 + return nsGetClassObjectByContractID(aContractID, error); 1.189 +} 1.190 + 1.191 +// type-safe shortcuts for calling |CreateInstance| 1.192 +template <class DestinationType> 1.193 +inline 1.194 +nsresult 1.195 +CallCreateInstance( const nsCID &aClass, 1.196 + nsISupports *aDelegate, 1.197 + DestinationType** aDestination ) 1.198 +{ 1.199 + NS_PRECONDITION(aDestination, "null parameter"); 1.200 + 1.201 + return CallCreateInstance(aClass, aDelegate, 1.202 + NS_GET_TEMPLATE_IID(DestinationType), 1.203 + reinterpret_cast<void**>(aDestination)); 1.204 +} 1.205 + 1.206 +template <class DestinationType> 1.207 +inline 1.208 +nsresult 1.209 +CallCreateInstance( const nsCID &aClass, 1.210 + DestinationType** aDestination ) 1.211 +{ 1.212 + NS_PRECONDITION(aDestination, "null parameter"); 1.213 + 1.214 + return CallCreateInstance(aClass, nullptr, 1.215 + NS_GET_TEMPLATE_IID(DestinationType), 1.216 + reinterpret_cast<void**>(aDestination)); 1.217 +} 1.218 + 1.219 +template <class DestinationType> 1.220 +inline 1.221 +nsresult 1.222 +CallCreateInstance( const char *aContractID, 1.223 + nsISupports *aDelegate, 1.224 + DestinationType** aDestination ) 1.225 +{ 1.226 + NS_PRECONDITION(aContractID, "null parameter"); 1.227 + NS_PRECONDITION(aDestination, "null parameter"); 1.228 + 1.229 + return CallCreateInstance(aContractID, 1.230 + aDelegate, 1.231 + NS_GET_TEMPLATE_IID(DestinationType), 1.232 + reinterpret_cast<void**>(aDestination)); 1.233 +} 1.234 + 1.235 +template <class DestinationType> 1.236 +inline 1.237 +nsresult 1.238 +CallCreateInstance( const char *aContractID, 1.239 + DestinationType** aDestination ) 1.240 +{ 1.241 + NS_PRECONDITION(aContractID, "null parameter"); 1.242 + NS_PRECONDITION(aDestination, "null parameter"); 1.243 + 1.244 + return CallCreateInstance(aContractID, nullptr, 1.245 + NS_GET_TEMPLATE_IID(DestinationType), 1.246 + reinterpret_cast<void**>(aDestination)); 1.247 +} 1.248 + 1.249 +template <class DestinationType> 1.250 +inline 1.251 +nsresult 1.252 +CallCreateInstance( nsIFactory *aFactory, 1.253 + nsISupports *aDelegate, 1.254 + DestinationType** aDestination ) 1.255 +{ 1.256 + NS_PRECONDITION(aFactory, "null parameter"); 1.257 + NS_PRECONDITION(aDestination, "null parameter"); 1.258 + 1.259 + return aFactory->CreateInstance(aDelegate, 1.260 + NS_GET_TEMPLATE_IID(DestinationType), 1.261 + reinterpret_cast<void**>(aDestination)); 1.262 +} 1.263 + 1.264 +template <class DestinationType> 1.265 +inline 1.266 +nsresult 1.267 +CallCreateInstance( nsIFactory *aFactory, 1.268 + DestinationType** aDestination ) 1.269 +{ 1.270 + NS_PRECONDITION(aFactory, "null parameter"); 1.271 + NS_PRECONDITION(aDestination, "null parameter"); 1.272 + 1.273 + return aFactory->CreateInstance(nullptr, 1.274 + NS_GET_TEMPLATE_IID(DestinationType), 1.275 + reinterpret_cast<void**>(aDestination)); 1.276 +} 1.277 + 1.278 +template <class DestinationType> 1.279 +inline 1.280 +nsresult 1.281 +CallGetClassObject( const nsCID &aClass, 1.282 + DestinationType** aDestination ) 1.283 +{ 1.284 + NS_PRECONDITION(aDestination, "null parameter"); 1.285 + 1.286 + return CallGetClassObject(aClass, 1.287 + NS_GET_TEMPLATE_IID(DestinationType), reinterpret_cast<void**>(aDestination)); 1.288 +} 1.289 + 1.290 +template <class DestinationType> 1.291 +inline 1.292 +nsresult 1.293 +CallGetClassObject( const char* aContractID, 1.294 + DestinationType** aDestination ) 1.295 +{ 1.296 + NS_PRECONDITION(aDestination, "null parameter"); 1.297 + 1.298 + return CallGetClassObject(aContractID, 1.299 + NS_GET_TEMPLATE_IID(DestinationType), reinterpret_cast<void**>(aDestination)); 1.300 +} 1.301 + 1.302 +#endif /* nsComponentManagerUtils_h__ */