do_QueryInterface( nsISupports* aRawPtr )分析
操作语法:nsCOMPtr<nsIDirectoryServiceProvider> prov = do_QueryInterface(aElement);
它等价于:aElement->QueryInterface(iid, &prov)
具体流程如下:
1. 首先构造一个nsQueryInterface对象,它封装了一个nsISupports接口指针。它是一个函数子,被构造nsCOMPtr对象时所回调,用来查询指定接口。
nsQueryInterface do_QueryInterface( nsISupports* aRawPtr )
{
return nsQueryInterface(aRawPtr);
}
2. 接下来,构造赋值nsCOMPtr对象
nsCOMPtr( const nsQueryInterface qi )
: NSCAP_CTOR_BASE(0)
// construct from |do_QueryInterface(expr)|
{
NSCAP_LOG_ASSIGNMENT(this, 0);
assign_from_qi(qi, NS_GET_IID(T));
}
3. 接下来,开始查询接口,便于给nsCOMPtr对象赋值
template <class T>
void nsCOMPtr<T>::assign_from_qi( const nsQueryInterface qi, const nsIID& aIID )
{
T* newRawPtr;
if ( NS_FAILED( qi(aIID, NS_REINTERPRET_CAST(void**, &newRawPtr)) ) )
newRawPtr = 0;
assign_assuming_AddRef(newRawPtr);
}
4. 调用函数子,开始查询接口
nsresult nsQueryInterface::operator()( const nsIID& aIID, void** answer ) const
{
nsresult status;
if ( mRawPtr )
{
status = mRawPtr->QueryInterface(aIID, answer);
}
else
status = NS_ERROR_NULL_POINTER;
return status;
}