Leaked source code of windows server 2003
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.

76 lines
2.4 KiB

  1. #if !defined(_FUSION_MI_CIPTR_H_INCLUDED_)
  2. #define _FUSION_MI_CIPTR_H_INCLUDED_
  3. #pragma once
  4. #include <unknwn.h>
  5. #include "CSxsPreserveLastError.h"
  6. template <typename T> class CSmartRef
  7. {
  8. public:
  9. inline CSmartRef() : m_pt(NULL) { }
  10. inline CSmartRef(const CSmartRef& r) : m_pt(r.m_pt) { if (m_pt) m_pt->AddRef(); }
  11. inline CSmartRef(T *pt) : m_pt(pt) { if (pt != NULL) pt->AddRef(); }
  12. inline ~CSmartRef() { if (m_pt != NULL) { CSxsPreserveLastError ple; m_pt->Release(); m_pt = NULL; ple.Restore(); } }
  13. inline T *operator ->() const { ASSERT_NTC(m_pt != NULL); return m_pt; }
  14. inline T **operator &() { ASSERT_NTC(m_pt == NULL); return &m_pt; }
  15. CSmartRef<T> &operator =(T *pt) { if (pt != NULL) pt->AddRef(); if (m_pt != NULL) m_pt->Release(); m_pt = pt; return *this; }
  16. CSmartRef<T> &operator =(const CSmartRef& r) { return operator=(r.m_pt); }
  17. inline operator T *() const { return m_pt; }
  18. inline T *Ptr() const { return m_pt; }
  19. inline void Release() { if (m_pt != NULL) { m_pt->Release(); m_pt = NULL; } }
  20. T *Disown() { T *pt = m_pt; m_pt = NULL; return pt; }
  21. inline void Take(CSmartRef<T> &r) { if (r.m_pt != NULL) { r.m_pt->AddRef(); } if (m_pt != NULL) { m_pt->Release(); } m_pt = r.m_pt; r.m_pt = NULL; }
  22. HRESULT Initialize(T *pt) { (*this) = pt; return NOERROR; }
  23. HRESULT CreateInstance(REFCLSID rclsid, IUnknown *pUnkOuter = NULL, CLSCTX clsctx = static_cast<CLSCTX>(CLSCTX_ALL))
  24. {
  25. T *pt = NULL;
  26. HRESULT hr = ::CoCreateInstance(rclsid, pUnkOuter, clsctx, __uuidof(T), (LPVOID *) &pt);
  27. if (FAILED(hr)) return hr;
  28. if (m_pt != NULL)
  29. m_pt->Release();
  30. m_pt = pt;
  31. pt = NULL;
  32. return NOERROR;
  33. }
  34. HRESULT QueryInterfaceFrom(IUnknown *pIUnknown)
  35. {
  36. HRESULT hr = NOERROR;
  37. T *pt = NULL;
  38. if (pIUnknown == NULL)
  39. this->Release();
  40. else
  41. {
  42. hr = pIUnknown->QueryInterface(__uuidof(T), (void **) &pt);
  43. if (FAILED(hr))
  44. goto Exit;
  45. if (m_pt != NULL)
  46. m_pt->Release();
  47. m_pt = pt;
  48. }
  49. hr = NOERROR;
  50. Exit:
  51. return hr;
  52. }
  53. // Not protected, because we need access to it for the table-based aggregation
  54. // in fusioncom.h to work, but don't touch it unless you understand the consequences!!
  55. T *m_pt;
  56. };
  57. #endif