DownloadCallbackT.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef NULLSOFT_WAC_DOWNLOAD_MANAGER_DOWNLOADCALLBACKT_H
  2. #define NULLSOFT_WAC_DOWNLOAD_MANAGER_DOWNLOADCALLBACKT_H
  3. #include "wac_downloadManager_api.h"
  4. #define WIN32_LEAN_AND_MEAN
  5. //#include <windows.h> // for InterlockedIncrememt/Decrement
  6. #include <atomic>
  7. /* DownloadCallbackT is reference counted for you. if you don't like that, inherit from ifc_downloadManagerCallback yourself */
  8. template <class T>
  9. class DownloadCallbackT : public ifc_downloadManagerCallback
  10. {
  11. public:
  12. size_t AddRef()
  13. {
  14. return _ref_count.fetch_add( 1 );
  15. }
  16. size_t Release()
  17. {
  18. if (_ref_count.load() == 0 )
  19. return _ref_count.load();
  20. LONG l_ref_count = _ref_count.fetch_sub( 1 );
  21. if ( !l_ref_count )
  22. delete( static_cast<T *>( this ) );
  23. return l_ref_count;
  24. }
  25. protected:
  26. DownloadCallbackT() {}
  27. void OnFinish( DownloadToken token ) { Release(); }
  28. void OnTick( DownloadToken token ) {}
  29. void OnError( DownloadToken token, int error ) { Release(); }
  30. void OnCancel( DownloadToken token ) { Release(); }
  31. void OnConnect( DownloadToken token ) {}
  32. void OnInit( DownloadToken token ) {}
  33. void OnData( DownloadToken token, void *data, size_t datalen ) {}
  34. int GetSource(wchar_t *source, size_t source_cch) { return 1;}
  35. int GetTitle(wchar_t *title, size_t title_cch) { return 1;}
  36. int GetLocation(wchar_t *location, size_t location_cch) { return 1;}
  37. #define CBCLASS T
  38. #define CBCLASST DownloadCallbackT<T>
  39. START_DISPATCH_INLINE;
  40. CBT(ADDREF, AddRef);
  41. CBT(RELEASE, Release);
  42. VCBT(IFC_DOWNLOADMANAGERCALLBACK_ONFINISH, OnFinish);
  43. VCBT(IFC_DOWNLOADMANAGERCALLBACK_ONTICK, OnTick);
  44. VCBT(IFC_DOWNLOADMANAGERCALLBACK_ONERROR, OnError);
  45. VCBT(IFC_DOWNLOADMANAGERCALLBACK_ONCANCEL, OnCancel);
  46. VCBT(IFC_DOWNLOADMANAGERCALLBACK_ONCONNECT, OnConnect);
  47. VCBT(IFC_DOWNLOADMANAGERCALLBACK_ONINIT, OnInit);
  48. VCBT(IFC_DOWNLOADMANAGERCALLBACK_ONDATA, OnData);
  49. CBT( IFC_DOWNLOADMANAGERCALLBACK_GETSOURCE, GetSource);
  50. CBT( IFC_DOWNLOADMANAGERCALLBACK_GETTITLE, GetTitle);
  51. CBT( IFC_DOWNLOADMANAGERCALLBACK_GETLOCATION, GetLocation);
  52. END_DISPATCH;
  53. #undef CBCLASS
  54. #undef CBCLASST
  55. private:
  56. std::atomic<std::size_t> _ref_count;
  57. };
  58. #endif // !NULLSOFT_WAC_DOWNLOAD_MANAGER_DOWNLOADCALLBACKT_H