AsyncCB.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. //////////////////////////////////////////////////////////////////////////
  3. // AsyncCallback [template]
  4. //
  5. // Description:
  6. // Helper class that routes IMFAsyncCallback::Invoke calls to a class
  7. // method on the parent class.
  8. //
  9. // Usage:
  10. // Add this class as a member variable. In the parent class constructor,
  11. // initialize the AsyncCallback class like this:
  12. // m_cb(this, &CYourClass::OnInvoke)
  13. // where
  14. // m_cb = AsyncCallback object
  15. // CYourClass = parent class
  16. // OnInvoke = Method in the parent class to receive Invoke calls.
  17. //
  18. // The parent's OnInvoke method (you can name it anything you like) must
  19. // have a signature that matches the InvokeFn typedef below.
  20. //////////////////////////////////////////////////////////////////////////
  21. // T: Type of the parent object
  22. template<class T>
  23. class AsyncCallback : public IMFAsyncCallback
  24. {
  25. public:
  26. typedef HRESULT (T::*InvokeFn)(IMFAsyncResult *pAsyncResult);
  27. AsyncCallback(T *pParent, InvokeFn fn) : m_pParent(pParent), m_pInvokeFn(fn)
  28. {
  29. }
  30. // IUnknown
  31. STDMETHODIMP_(ULONG) AddRef() {
  32. // Delegate to parent class.
  33. return m_pParent->AddRef();
  34. }
  35. STDMETHODIMP_(ULONG) Release() {
  36. // Delegate to parent class.
  37. return m_pParent->Release();
  38. }
  39. STDMETHODIMP QueryInterface(REFIID iid, void** ppv)
  40. {
  41. if (!ppv)
  42. {
  43. return E_POINTER;
  44. }
  45. if (iid == __uuidof(IUnknown))
  46. {
  47. *ppv = static_cast<IUnknown*>(static_cast<IMFAsyncCallback*>(this));
  48. }
  49. else if (iid == __uuidof(IMFAsyncCallback))
  50. {
  51. *ppv = static_cast<IMFAsyncCallback*>(this);
  52. }
  53. else
  54. {
  55. *ppv = NULL;
  56. return E_NOINTERFACE;
  57. }
  58. AddRef();
  59. return S_OK;
  60. }
  61. // IMFAsyncCallback methods
  62. STDMETHODIMP GetParameters(DWORD*, DWORD*)
  63. {
  64. // Implementation of this method is optional.
  65. return E_NOTIMPL;
  66. }
  67. STDMETHODIMP Invoke(IMFAsyncResult* pAsyncResult)
  68. {
  69. return (m_pParent->*m_pInvokeFn)(pAsyncResult);
  70. }
  71. T *m_pParent;
  72. InvokeFn m_pInvokeFn;
  73. };