Add helper functions that make it easier to cache factories. b=261310 r=darin

This commit is contained in:
dbaron%dbaron.org 2004-10-02 00:44:13 +00:00
Родитель 0bfe106289
Коммит 338d2bb649
8 изменённых файлов: 385 добавлений и 47 удалений

Просмотреть файл

@ -212,12 +212,7 @@ nsCSSScanner::~nsCSSScanner()
nsresult rv = CallGetService(NS_CONSOLESERVICE_CONTRACTID, &gConsoleService);
NS_ENSURE_SUCCESS(rv, PR_FALSE);
nsCOMPtr<nsIComponentManager> compMgr;
rv = NS_GetComponentManager(getter_AddRefs(compMgr));
NS_ENSURE_SUCCESS(rv, PR_FALSE);
rv = compMgr->GetClassObjectByContractID(NS_SCRIPTERROR_CONTRACTID,
NS_GET_IID(nsIFactory),
(void**)&gScriptErrorFactory);
rv = CallGetClassObject(NS_SCRIPTERROR_CONTRACTID, &gScriptErrorFactory);
NS_ENSURE_SUCCESS(rv, PR_FALSE);
NS_ASSERTION(gConsoleService && gScriptErrorFactory,
"unexpected null pointer without failure");
@ -287,10 +282,9 @@ void nsCSSScanner::OutputError()
// Log it to the JavaScript console
if (InitGlobals()) {
nsCOMPtr<nsIScriptError> errorObject;
nsresult rv =
gScriptErrorFactory->CreateInstance(nsnull, NS_GET_IID(nsIScriptError),
getter_AddRefs(errorObject));
nsresult rv;
nsCOMPtr<nsIScriptError> errorObject =
do_CreateInstance(gScriptErrorFactory, &rv);
if (NS_SUCCEEDED(rv)) {
rv = errorObject->Init(mError.get(),
NS_ConvertASCIItoUCS2(mFileName.get()).get(),

Просмотреть файл

@ -212,12 +212,7 @@ nsCSSScanner::~nsCSSScanner()
nsresult rv = CallGetService(NS_CONSOLESERVICE_CONTRACTID, &gConsoleService);
NS_ENSURE_SUCCESS(rv, PR_FALSE);
nsCOMPtr<nsIComponentManager> compMgr;
rv = NS_GetComponentManager(getter_AddRefs(compMgr));
NS_ENSURE_SUCCESS(rv, PR_FALSE);
rv = compMgr->GetClassObjectByContractID(NS_SCRIPTERROR_CONTRACTID,
NS_GET_IID(nsIFactory),
(void**)&gScriptErrorFactory);
rv = CallGetClassObject(NS_SCRIPTERROR_CONTRACTID, &gScriptErrorFactory);
NS_ENSURE_SUCCESS(rv, PR_FALSE);
NS_ASSERTION(gConsoleService && gScriptErrorFactory,
"unexpected null pointer without failure");
@ -287,10 +282,9 @@ void nsCSSScanner::OutputError()
// Log it to the JavaScript console
if (InitGlobals()) {
nsCOMPtr<nsIScriptError> errorObject;
nsresult rv =
gScriptErrorFactory->CreateInstance(nsnull, NS_GET_IID(nsIScriptError),
getter_AddRefs(errorObject));
nsresult rv;
nsCOMPtr<nsIScriptError> errorObject =
do_CreateInstance(gScriptErrorFactory, &rv);
if (NS_SUCCEEDED(rv)) {
rv = errorObject->Init(mError.get(),
NS_ConvertASCIItoUCS2(mFileName.get()).get(),

Просмотреть файл

@ -533,22 +533,15 @@ nsViewManager::CreateRegion(nsIRegion* *result)
nsresult rv;
if (!mRegionFactory) {
nsCOMPtr<nsIComponentManager> compMgr;
rv = NS_GetComponentManager(getter_AddRefs(compMgr));
if (NS_SUCCEEDED(rv))
rv = compMgr->GetClassObject(kRegionCID,
NS_GET_IID(nsIFactory),
getter_AddRefs(mRegionFactory));
if (!mRegionFactory) {
mRegionFactory = do_GetClassObject(kRegionCID, &rv);
if (NS_FAILED(rv)) {
*result = nsnull;
return NS_ERROR_FAILURE;
return rv;
}
}
nsIRegion* region = nsnull;
rv = mRegionFactory->CreateInstance(nsnull, NS_GET_IID(nsIRegion), (void**)&region);
rv = CallCreateInstance(mRegionFactory, &region);
if (NS_SUCCEEDED(rv)) {
rv = region->Init();
*result = region;

Просмотреть файл

@ -39,6 +39,7 @@
// Global Static Component Manager Methods
// (for when you need to link with xpcom)
#include "nsXPCOM.h"
#include "nsIComponentManagerObsolete.h"
#include "nsComponentManagerObsolete.h"
@ -69,6 +70,17 @@ nsComponentManager::GetClassObject(const nsCID &aClass, const nsIID &aIID,
return cm->GetClassObject(aClass, aIID, aResult);
}
nsresult
nsComponentManager::GetClassObjectByContractID(const char *aContractID,
const nsIID &aIID,
void **aResult)
{
nsCOMPtr<nsIComponentManager> cm;
nsresult rv = NS_GetComponentManager(getter_AddRefs(cm));
if (NS_FAILED(rv)) return rv;
return cm->GetClassObjectByContractID(aContractID, aIID, aResult);
}
nsresult
nsComponentManager::ContractIDToClassID(const char *aContractID,
nsCID *aClass)

Просмотреть файл

@ -83,6 +83,11 @@ public:
static nsresult GetClassObject(const nsCID &aClass, const nsIID &aIID,
void **aResult);
// Get the singleton class object that implements the CID aClass
static nsresult GetClassObjectByContractID(const char *aContractID,
const nsIID &aIID,
void **aResult);
// Finds a class ID for a specific Program ID
static nsresult ContractIDToClassID(const char *aContractID,
nsCID *aClass);

Просмотреть файл

@ -73,9 +73,9 @@ public:
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
const nsCID& mCID;
nsISupports* mOuter;
nsresult* mErrorPtr;
const nsCID& mCID;
nsISupports* mOuter;
nsresult* mErrorPtr;
};
class NS_COM nsCreateInstanceByContractID : public nsCOMPtr_helper
@ -85,18 +85,38 @@ public:
: mContractID(aContractID),
mOuter(aOuter),
mErrorPtr(aErrorPtr)
{
// nothing else to do here
}
{
// nothing else to do here
}
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
const char* mContractID;
const char* mContractID;
nsISupports* mOuter;
nsresult* mErrorPtr;
nsresult* mErrorPtr;
};
class NS_COM nsCreateInstanceFromFactory : public nsCOMPtr_helper
{
public:
nsCreateInstanceFromFactory( nsIFactory* aFactory, nsISupports* aOuter, nsresult* aErrorPtr )
: mFactory(aFactory),
mOuter(aOuter),
mErrorPtr(aErrorPtr)
{
// nothing else to do here
}
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
nsIFactory* mFactory;
nsISupports* mOuter;
nsresult* mErrorPtr;
};
inline
const nsCreateInstanceByCID
do_CreateInstance( const nsCID& aCID, nsresult* error = 0 )
@ -125,6 +145,74 @@ do_CreateInstance( const char* aContractID, nsISupports* aOuter, nsresult* error
return nsCreateInstanceByContractID(aContractID, aOuter, error);
}
inline
const nsCreateInstanceFromFactory
do_CreateInstance( nsIFactory* aFactory, nsresult* error = 0 )
{
return nsCreateInstanceFromFactory(aFactory, 0, error);
}
inline
const nsCreateInstanceFromFactory
do_CreateInstance( nsIFactory* aFactory, nsISupports* aOuter, nsresult* error = 0 )
{
return nsCreateInstanceFromFactory(aFactory, aOuter, error);
}
class NS_COM nsGetClassObjectByCID : public nsCOMPtr_helper
{
public:
nsGetClassObjectByCID( const nsCID& aCID, nsresult* aErrorPtr )
: mCID(aCID),
mErrorPtr(aErrorPtr)
{
// nothing else to do here
}
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
const nsCID& mCID;
nsresult* mErrorPtr;
};
class NS_COM nsGetClassObjectByContractID : public nsCOMPtr_helper
{
public:
nsGetClassObjectByContractID( const char* aContractID, nsresult* aErrorPtr )
: mContractID(aContractID),
mErrorPtr(aErrorPtr)
{
// nothing else to do here
}
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
const char* mContractID;
nsresult* mErrorPtr;
};
/**
* do_GetClassObject can be used to improve performance of callers
* that call |CreateInstance| many times. They can cache the factory
* and call do_CreateInstance or CallCreateInstance with the cached
* factory rather than having the component manager retrieve it every
* time.
*/
inline const nsGetClassObjectByCID
do_GetClassObject( const nsCID& aCID, nsresult* error = 0 )
{
return nsGetClassObjectByCID(aCID, error);
}
inline const nsGetClassObjectByContractID
do_GetClassObject( const char* aContractID, nsresult* error = 0 )
{
return nsGetClassObjectByContractID(aContractID, error);
}
// type-safe shortcuts for calling |CreateInstance|
template <class DestinationType>
inline
@ -183,6 +271,59 @@ CallCreateInstance( const char *aContractID,
NS_REINTERPRET_CAST(void**, aDestination));
}
template <class DestinationType>
inline
nsresult
CallCreateInstance( nsIFactory *aFactory,
nsISupports *aDelegate,
DestinationType** aDestination )
{
NS_PRECONDITION(aFactory, "null parameter");
NS_PRECONDITION(aDestination, "null parameter");
return aFactory->CreateInstance(aDelegate,
NS_GET_IID(DestinationType),
NS_REINTERPRET_CAST(void**, aDestination));
}
template <class DestinationType>
inline
nsresult
CallCreateInstance( nsIFactory *aFactory,
DestinationType** aDestination )
{
NS_PRECONDITION(aFactory, "null parameter");
NS_PRECONDITION(aDestination, "null parameter");
return aFactory->CreateInstance(nsnull,
NS_GET_IID(DestinationType),
NS_REINTERPRET_CAST(void**, aDestination));
}
template <class DestinationType>
inline
nsresult
CallGetClassObject( const nsCID &aClass,
DestinationType** aDestination )
{
NS_PRECONDITION(aDestination, "null parameter");
return nsComponentManager::GetClassObject(aClass,
NS_GET_IID(DestinationType), NS_REINTERPRET_CAST(void**, aDestination));
}
template <class DestinationType>
inline
nsresult
CallGetClassObject( const char* aContractID,
DestinationType** aDestination )
{
NS_PRECONDITION(aDestination, "null parameter");
return nsComponentManager::GetClassObjectByContractID(aContractID,
NS_GET_IID(DestinationType), NS_REINTERPRET_CAST(void**, aDestination));
}
/* keys for registry use */
extern const char xpcomKeyName[];
extern const char xpcomComponentsKeyName[];

Просмотреть файл

@ -87,6 +87,64 @@ nsCreateInstanceByContractID::operator()( const nsIID& aIID, void** aInstancePtr
return status;
}
nsresult
nsCreateInstanceFromFactory::operator()( const nsIID& aIID, void** aInstancePtr ) const
{
nsresult status;
if ( mFactory )
status = mFactory->CreateInstance(mOuter, aIID, aInstancePtr);
else
status = NS_ERROR_NULL_POINTER;
if ( NS_FAILED(status) )
*aInstancePtr = 0;
if ( mErrorPtr )
*mErrorPtr = status;
return status;
}
nsresult
nsGetClassObjectByCID::operator()( const nsIID& aIID, void** aInstancePtr ) const
{
nsCOMPtr<nsIComponentManager> compMgr;
nsresult status = NS_GetComponentManager(getter_AddRefs(compMgr));
if (compMgr)
status = compMgr->GetClassObject(mCID, aIID, aInstancePtr);
else if (NS_SUCCEEDED(status))
status = NS_ERROR_UNEXPECTED;
if ( NS_FAILED(status) )
*aInstancePtr = 0;
if ( mErrorPtr )
*mErrorPtr = status;
return status;
}
nsresult
nsGetClassObjectByContractID::operator()( const nsIID& aIID, void** aInstancePtr ) const
{
nsresult status;
if ( mContractID ) {
nsCOMPtr<nsIComponentManager> compMgr;
status = NS_GetComponentManager(getter_AddRefs(compMgr));
if (compMgr)
status = compMgr->GetClassObjectByContractID(mContractID,
aIID, aInstancePtr);
else if (NS_SUCCEEDED(status))
status = NS_ERROR_UNEXPECTED;
}
else
status = NS_ERROR_NULL_POINTER;
if ( NS_FAILED(status) )
*aInstancePtr = 0;
if ( mErrorPtr )
*mErrorPtr = status;
return status;
}
nsresult
nsGetServiceByCID::operator()( const nsIID& aIID, void** aInstancePtr ) const

Просмотреть файл

@ -73,9 +73,9 @@ public:
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
const nsCID& mCID;
nsISupports* mOuter;
nsresult* mErrorPtr;
const nsCID& mCID;
nsISupports* mOuter;
nsresult* mErrorPtr;
};
class NS_COM nsCreateInstanceByContractID : public nsCOMPtr_helper
@ -85,18 +85,38 @@ public:
: mContractID(aContractID),
mOuter(aOuter),
mErrorPtr(aErrorPtr)
{
// nothing else to do here
}
{
// nothing else to do here
}
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
const char* mContractID;
const char* mContractID;
nsISupports* mOuter;
nsresult* mErrorPtr;
nsresult* mErrorPtr;
};
class NS_COM nsCreateInstanceFromFactory : public nsCOMPtr_helper
{
public:
nsCreateInstanceFromFactory( nsIFactory* aFactory, nsISupports* aOuter, nsresult* aErrorPtr )
: mFactory(aFactory),
mOuter(aOuter),
mErrorPtr(aErrorPtr)
{
// nothing else to do here
}
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
nsIFactory* mFactory;
nsISupports* mOuter;
nsresult* mErrorPtr;
};
inline
const nsCreateInstanceByCID
do_CreateInstance( const nsCID& aCID, nsresult* error = 0 )
@ -125,6 +145,74 @@ do_CreateInstance( const char* aContractID, nsISupports* aOuter, nsresult* error
return nsCreateInstanceByContractID(aContractID, aOuter, error);
}
inline
const nsCreateInstanceFromFactory
do_CreateInstance( nsIFactory* aFactory, nsresult* error = 0 )
{
return nsCreateInstanceFromFactory(aFactory, 0, error);
}
inline
const nsCreateInstanceFromFactory
do_CreateInstance( nsIFactory* aFactory, nsISupports* aOuter, nsresult* error = 0 )
{
return nsCreateInstanceFromFactory(aFactory, aOuter, error);
}
class NS_COM nsGetClassObjectByCID : public nsCOMPtr_helper
{
public:
nsGetClassObjectByCID( const nsCID& aCID, nsresult* aErrorPtr )
: mCID(aCID),
mErrorPtr(aErrorPtr)
{
// nothing else to do here
}
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
const nsCID& mCID;
nsresult* mErrorPtr;
};
class NS_COM nsGetClassObjectByContractID : public nsCOMPtr_helper
{
public:
nsGetClassObjectByContractID( const char* aContractID, nsresult* aErrorPtr )
: mContractID(aContractID),
mErrorPtr(aErrorPtr)
{
// nothing else to do here
}
virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
private:
const char* mContractID;
nsresult* mErrorPtr;
};
/**
* do_GetClassObject can be used to improve performance of callers
* that call |CreateInstance| many times. They can cache the factory
* and call do_CreateInstance or CallCreateInstance with the cached
* factory rather than having the component manager retrieve it every
* time.
*/
inline const nsGetClassObjectByCID
do_GetClassObject( const nsCID& aCID, nsresult* error = 0 )
{
return nsGetClassObjectByCID(aCID, error);
}
inline const nsGetClassObjectByContractID
do_GetClassObject( const char* aContractID, nsresult* error = 0 )
{
return nsGetClassObjectByContractID(aContractID, error);
}
// type-safe shortcuts for calling |CreateInstance|
template <class DestinationType>
inline
@ -183,6 +271,59 @@ CallCreateInstance( const char *aContractID,
NS_REINTERPRET_CAST(void**, aDestination));
}
template <class DestinationType>
inline
nsresult
CallCreateInstance( nsIFactory *aFactory,
nsISupports *aDelegate,
DestinationType** aDestination )
{
NS_PRECONDITION(aFactory, "null parameter");
NS_PRECONDITION(aDestination, "null parameter");
return aFactory->CreateInstance(aDelegate,
NS_GET_IID(DestinationType),
NS_REINTERPRET_CAST(void**, aDestination));
}
template <class DestinationType>
inline
nsresult
CallCreateInstance( nsIFactory *aFactory,
DestinationType** aDestination )
{
NS_PRECONDITION(aFactory, "null parameter");
NS_PRECONDITION(aDestination, "null parameter");
return aFactory->CreateInstance(nsnull,
NS_GET_IID(DestinationType),
NS_REINTERPRET_CAST(void**, aDestination));
}
template <class DestinationType>
inline
nsresult
CallGetClassObject( const nsCID &aClass,
DestinationType** aDestination )
{
NS_PRECONDITION(aDestination, "null parameter");
return nsComponentManager::GetClassObject(aClass,
NS_GET_IID(DestinationType), NS_REINTERPRET_CAST(void**, aDestination));
}
template <class DestinationType>
inline
nsresult
CallGetClassObject( const char* aContractID,
DestinationType** aDestination )
{
NS_PRECONDITION(aDestination, "null parameter");
return nsComponentManager::GetClassObjectByContractID(aContractID,
NS_GET_IID(DestinationType), NS_REINTERPRET_CAST(void**, aDestination));
}
/* keys for registry use */
extern const char xpcomKeyName[];
extern const char xpcomComponentsKeyName[];