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.

98 lines
1.5 KiB

  1. /*++
  2. Copyright (C) 1996-1999 Microsoft Corporation
  3. Module Name:
  4. SMARTREF.H
  5. History:
  6. --*/
  7. #ifndef ESPUTIL_SmartRef_H
  8. #define ESPUTIL_SmartRef_H
  9. template<class T>
  10. class SmartRef
  11. {
  12. private:
  13. T *m_pInterface;
  14. public:
  15. NOTHROW SmartRef()
  16. {
  17. m_pInterface = NULL;
  18. }
  19. // Compiler bug, must be inline!
  20. NOTHROW SmartRef(T *pI) {m_pInterface = pI;};
  21. NOTHROW SmartRef(const SmartRef<T> &);
  22. NOTHROW T * operator->(void)
  23. {
  24. LTASSERT(m_pInterface != NULL);
  25. return m_pInterface;
  26. }
  27. NOTHROW const T * operator->(void) const
  28. {
  29. LTASSERT(m_pInterface != NULL);
  30. return m_pInterface;
  31. }
  32. NOTHROW T & operator*(void)
  33. {
  34. LTASSERT(m_pInterface != NULL);
  35. return *m_pInterface;
  36. }
  37. NOTHROW T * Extract(void)
  38. {
  39. return ExtractImpl();
  40. };
  41. NOTHROW T * GetInterface(BOOL fAddRef = FALSE)
  42. {
  43. return GetInterfaceImpl(fAddRef);
  44. };
  45. NOTHROW const T * GetInterface(void) const
  46. {
  47. return m_pInterface;
  48. }
  49. NOTHROW BOOL IsNull(void) const
  50. {
  51. return m_pInterface == NULL;
  52. }
  53. NOTHROW ~SmartRef()
  54. {
  55. if (m_pInterface != NULL)
  56. {
  57. m_pInterface->Release();
  58. }
  59. }
  60. void operator=(T* pOther)
  61. {
  62. opEqImpl(pOther);
  63. }
  64. void operator=(const SmartRef<T> &other);
  65. T ** operator&(void);
  66. operator T* &(void) {return opTpRef();};
  67. private:
  68. void operator delete(void *);
  69. NOTHROW T * ExtractImpl(void);
  70. NOTHROW T * GetInterfaceImpl(BOOL fAddRef);
  71. NOTHROW T * & opTpRef(void);
  72. void opEqImpl(T* pOther);
  73. };
  74. #include "SmartRef.inl"
  75. #endif