Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.1 KiB

  1. #ifndef _COWSITE_H_
  2. #define _COWSITE_H_
  3. class CObjectWithSite : public IObjectWithSite
  4. {
  5. public:
  6. CObjectWithSite() {_punkSite = NULL;};
  7. virtual ~CObjectWithSite() {ATOMICRELEASE(_punkSite);}
  8. //*** IUnknown ****
  9. // (client must provide!)
  10. //*** IObjectWithSite ***
  11. STDMETHOD(SetSite)(IUnknown *punkSite);
  12. STDMETHOD(GetSite)(REFIID riid, void **ppvSite);
  13. protected:
  14. IUnknown* _punkSite;
  15. };
  16. //
  17. // use this when you dont have a good Destroy site chain event - ZekeL - 20-DEC-2000
  18. // if you need to call SetSite(NULL) on your children, and
  19. // would prefer to do this cleanup in your destructor.
  20. // your object should be implemented like this
  21. //
  22. /******
  23. class CMyObject : public IMyInterface
  24. {
  25. private:
  26. CSafeServiceSite *_psss;
  27. IKid _pkid;
  28. CMyObject()
  29. {
  30. _psss = new CSafeServiceSite();
  31. if (_psss)
  32. _psss->SetProviderWeakRef(this);
  33. }
  34. ~CMyObject()
  35. {
  36. if (_psss)
  37. {
  38. _psss->SetProviderWeakRef(NULL);
  39. _psss->Release();
  40. }
  41. if (_pkid)
  42. {
  43. IUnknown_SetSite(_pkid, _psss);
  44. _pkid->Release();
  45. }
  46. }
  47. public:
  48. // IMyInterface
  49. HRESULT Init()
  50. {
  51. CoCreate(CLSID_Kid, &_pkid);
  52. IUnknown_SetSite(_pkid, _psss);
  53. }
  54. // NOTE - there is no Uninit()
  55. // so it's hard to know when to release _pkid
  56. // and you dont want to _pkid->SetSite(NULL)
  57. // unless you are sure you are done
  58. };
  59. ******/
  60. class CSafeServiceSite : public IServiceProvider
  61. {
  62. public:
  63. CSafeServiceSite() : _cRef(1), _psp(NULL) {}
  64. // IUnknown
  65. STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
  66. STDMETHODIMP_(ULONG) AddRef();
  67. STDMETHODIMP_(ULONG) Release();
  68. // IServiceProvider
  69. STDMETHODIMP QueryService(REFGUID guidService, REFIID riid, void **ppvObj);
  70. // our personal weak ref
  71. HRESULT SetProviderWeakRef(IServiceProvider *psp);
  72. private: // methods
  73. ~CSafeServiceSite()
  74. { ASSERT(_psp == NULL); }
  75. private: // members
  76. LONG _cRef;
  77. IServiceProvider *_psp;
  78. };
  79. #endif