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.

109 lines
2.5 KiB

  1. //
  2. // auto_rel.h
  3. //
  4. #pragma once
  5. // class I - Multi-Inheritance casting for ATL type classes
  6. // ergo C2385 - T::Release() is ambiguous
  7. //
  8. template<class T, class I = T>
  9. class auto_rel
  10. {
  11. public:
  12. explicit auto_rel(T* p = 0)
  13. : pointee(p) {};
  14. // Don't AddRef()
  15. auto_rel(auto_rel<T,I>& rhs)
  16. : pointee(rhs.get()) { if (pointee) ((I*)pointee)->AddRef(); }
  17. ~auto_rel()
  18. {
  19. if (pointee)
  20. ((I*)pointee)->Release();
  21. };
  22. auto_rel<T,I>& operator= (const auto_rel<T,I>& rhs)
  23. {
  24. if (this != rhs.getThis())
  25. {
  26. reset (rhs.get());
  27. if (pointee) ((I*)pointee)->AddRef();
  28. }
  29. return *this;
  30. };
  31. auto_rel<T,I>& operator= (T*rhs)
  32. {
  33. reset (rhs);
  34. // Don't AddRef()
  35. return *this;
  36. };
  37. T& operator*() const
  38. { return *pointee; };
  39. T* operator-> () const
  40. { return pointee; };
  41. T** operator& () // for OpenEntry etc...
  42. { reset(); return &pointee; };
  43. operator T* ()
  44. { return pointee; };
  45. #ifdef MAPIDEFS_H
  46. operator LPMAPIPROP ()
  47. { return (LPMAPIPROP)pointee; };
  48. #endif
  49. operator bool ()
  50. { return pointee != NULL; };
  51. operator bool () const
  52. { return pointee != NULL; };
  53. bool operator! ()
  54. { return pointee == NULL; };
  55. bool operator! () const
  56. { return pointee == NULL; };
  57. // Checks for NULL
  58. bool operator== (LPVOID lpv)
  59. { return pointee == lpv; };
  60. bool operator!= (LPVOID lpv)
  61. { return pointee != lpv; };
  62. bool operator== (const auto_rel<T,I>& rhs)
  63. { return pointee == rhs.pointee; }
  64. bool operator< (const auto_rel<T,I>& rhs)
  65. { return pointee < rhs.pointee; }
  66. // return value of current dumb pointer
  67. T* get() const
  68. { return pointee; };
  69. // relinquish ownership
  70. T* release()
  71. { T * oldPointee = pointee;
  72. pointee = 0;
  73. return oldPointee;
  74. };
  75. // delete owned pointer; assume ownership of p
  76. ULONG reset (T* p = 0)
  77. { ULONG ul = 0;
  78. if (pointee)
  79. ul = ((I*)pointee)->Release();
  80. pointee = p;
  81. return ul;
  82. };
  83. private:
  84. #ifdef MAPIDEFS_H
  85. // these are here on purpose, better to find out at compile time
  86. // use auto_padr<T,I> or auto_prow<T,I>
  87. operator LPADRLIST () { return 0; };
  88. operator LPSRowSet () { return 0; };
  89. #endif
  90. // operator& throws off operator=
  91. const auto_rel<T,I> * getThis() const
  92. { return this; };
  93. T* pointee;
  94. };