Export (0) Print
Expand All

Supporting Multiple Callbacks

If you call more than one asynchronous method, each one requires a separate implementation of IMFAsyncCallback::Invoke. However, you might want to implement the callbacks inside a single C++ class. The class can have only one Invoke method, so one solution is to provide a helper class that delegates Invoke calls to another method on a container class.

The following code shows a class template named AsyncCallback, which demonstrates this approach.


//////////////////////////////////////////////////////////////////////////
//  AsyncCallback [template]
//
//  Description:
//  Helper class that routes IMFAsyncCallback::Invoke calls to a class
//  method on the parent class.
//
//  Usage:
//  Add this class as a member variable. In the parent class constructor,
//  initialize the AsyncCallback class like this:
//      m_cb(this, &CYourClass::OnInvoke)
//  where
//      m_cb       = AsyncCallback object
//      CYourClass = parent class
//      OnInvoke   = Method in the parent class to receive Invoke calls.
//
//  The parent's OnInvoke method (you can name it anything you like) must
//  have a signature that matches the InvokeFn typedef below.
//////////////////////////////////////////////////////////////////////////

// T: Type of the parent object
template<class T>
class AsyncCallback : public IMFAsyncCallback
{
public:
    typedef HRESULT (T::*InvokeFn)(IMFAsyncResult *pAsyncResult);

    AsyncCallback(T *pParent, InvokeFn fn) : m_pParent(pParent), m_pInvokeFn(fn)
    {
    }

    // IUnknown
    STDMETHODIMP QueryInterface(REFIID riid, void** ppv)
    {
        static const QITAB qit[] =
        {
            QITABENT(AsyncCallback, IMFAsyncCallback),
            { 0 }
        };
        return QISearch(this, qit, riid, ppv);
    }
    STDMETHODIMP_(ULONG) AddRef() {
        // Delegate to parent class.
        return m_pParent->AddRef();
    }
    STDMETHODIMP_(ULONG) Release() {
        // Delegate to parent class.
        return m_pParent->Release();
    }

    // IMFAsyncCallback methods
    STDMETHODIMP GetParameters(DWORD*, DWORD*)
    {
        // Implementation of this method is optional.
        return E_NOTIMPL;
    }

    STDMETHODIMP Invoke(IMFAsyncResult* pAsyncResult)
    {
        return (m_pParent->*m_pInvokeFn)(pAsyncResult);
    }

    T *m_pParent;
    InvokeFn m_pInvokeFn;
};


The template parameter is the name of container class. The AsyncCallback constructor has two parameters: a pointer to the container class, and the address of a callback method on the container class. The container class can have multiple instances of the AsyncCallback class as member variables, one for each asynchronous method. When the container class calls an asynchronous method, it use the IMFAsyncCallback interface of the appropriate AsyncCallback object. When the AsyncCallback object's Invoke method is called, the call is delegated to the correct method on the container class.

The AsyncCallback object also delegates AddRef and Release calls to the container class, so the container class manages the lifetime of the AsyncCallback object. This guarantees that the AsyncCallback object will not be deleted until the container object itself is deleted.

The following code shows how to use this template:


#pragma warning( push )
#pragma warning( disable : 4355 )  // 'this' used in base member initializer list

class CMyObject : public IUnknown
{
public:

    CMyObject() : m_CB(this, &CMyObject::OnInvoke)
    {
        // Other initialization here.
    }

    STDMETHODIMP_(ULONG) AddRef();
    STDMETHODIMP_(ULONG) Release();
    STDMETHODIMP QueryInterface(REFIID iid, void** ppv);


private:

    AsyncCallback<CMyObject>   m_CB;

    HRESULT OnInvoke(IMFAsyncResult *pAsyncResult);
};

#pragma warning( pop )

In this example, the container class is named CMyObject. The m_CB member variable is a AsyncCallback object. In the CMyObject constructor, the m_CB member variable is initialized with the address of the CMyObject::OnInvoke method.

Related topics

Asynchronous Callback Methods

 

 

Community Additions

ADD
Show:
© 2014 Microsoft