OOPortal OOPortal


COM Aggregation   «Prev 

Blind Aggregation in COM

Blind aggregation means the outer object does not have code that looks for specific interfaces implemented in aggregated objects. Instead, if an unknown interface ID (IID) is passed into QueryInterface, the outer object calls into the nondelegating IUnknown of each aggregated object to see if the interface is supported.
The following pseudo-code makes the following assumptions:
  1. The outer object has aggregated two inner objects: IO1 and IO2.
  2. IO1 implements interface IX1; IO2 implements interface IX2.
  3. The outer object implements interface IX0.
  4. The outer object has nondelegating IUnknown interface pointers into each aggregated object in member variables m_pIUnknown_IO1 and m_pIUnknown_IO2.

class COuterCOMObj : public IX0 {
   IUnknown *m_pIUnknown_IO1;
   IUnknown *m_pIUnknown_IO2;
   HRESULT hr = E_NOINTERFACE;

   HRESULT QueryInterface(REFIID riid, VOID **ppv) {
   /*See if the caller is asking for interface IX0  which is implemented in the outer object*/
   if (riid == IID_IX0) { 
     *ppv = (IX0 *) this;
     hr = S_OK;
   }
   else {
     /* If we don't recognize the IID, check with each aggregated object to see if 
     it is supported */
     hr = m_pIUnknown_IO1->QueryInterface(riid, ppv);
     if (SUCCEEDED(hr)) return hr;
     hr = m_pIUnknown_IO2->QueryInterface(riid, ppv);
     if (SUCCEEDED(hr)) 
       return hr;
     }
     if (FAILED(hr))
       return hr;
     ((IUnknown *) ppv)->AddRef();
     return S_OK;
   }
   ...
};