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.

151 lines
5.5 KiB

  1. #if !defined(_FUSION_INC_SMARTPTR_H_INCLUDED_)
  2. #define _FUSION_INC_SMARTPTR_H_INCLUDED_
  3. /*++
  4. Copyright (c) 2000 Microsoft Corporation
  5. Module Name:
  6. SxsExceptionHandling.h
  7. Abstract:
  8. Author:
  9. Jay Krell (a-JayK) October 2000
  10. Revision History:
  11. --*/
  12. #pragma once
  13. #include "nt.h"
  14. #include "ntrtl.h"
  15. #include "nturtl.h"
  16. #include "fusiontrace.h"
  17. #include "csxspreservelasterror.h" // Most destructors should use this.
  18. #include "fusionheap.h"
  19. //template <typename T> void SxsDelete(T* /*&*/ p) { T* q = p; p = NULL; delete q; }
  20. //
  21. // Need to flesh this out.
  22. // See \\cpvsbuild\Drops\v7.0\raw\current\vs\src\VSEE\lib\Memory\*Pointer*.
  23. //
  24. //
  25. // Split off into ATL style Base/Derived to avoid compiler ICE;
  26. // Otherwise was void (*Delete)(T*) = SxsDelete<T>
  27. //
  28. template <typename T, typename Derived>
  29. class CSxsPointerBase
  30. {
  31. CSxsPointerBase(const CSxsPointerBase&); // deliberately not implemented
  32. void operator=(const CSxsPointerBase&); // deliberately not implemented
  33. protected:
  34. T* m_p;
  35. void operator=(T* p);
  36. HRESULT HrAllocateBase(PCSTR pszFileName, int nLine, PCSTR pszTypeName) { HRESULT hr = E_FAIL; FN_TRACE_HR(hr); INTERNAL_ERROR_CHECK(m_p == NULL); IFALLOCFAILED_EXIT(m_p = new(pszFileName, nLine, pszTypeName) T); hr = NOERROR; Exit: return hr; }
  37. BOOL Win32AllocateBase(PCSTR pszFileName, int nLine, PCSTR pszTypeName) { BOOL fSuccess = FALSE; FN_TRACE_WIN32(fSuccess); INTERNAL_ERROR_CHECK(m_p == NULL); IFALLOCFAILED_EXIT(m_p = new(pszFileName, nLine, pszTypeName) T); fSuccess = TRUE; Exit: return fSuccess; }
  38. public:
  39. CSxsPointerBase(T* p = NULL);
  40. ~CSxsPointerBase();
  41. operator T*() { return m_p; }
  42. operator T const *() const { return m_p; }
  43. T** operator&() { ASSERT_NTC( m_p == NULL ); return &m_p; }
  44. T* operator->() { return m_p; }
  45. T const * operator->() const { return m_p; }
  46. // this stores null in m_p and returns its previous value
  47. T* Detach() { T *p = m_p; m_p = NULL; return p; }
  48. // these are synonyms of each other
  49. void Delete();
  50. };
  51. template <typename T, PCSTR pszTypeName>
  52. class CSxsPointer : public CSxsPointerBase<T, CSxsPointer>
  53. {
  54. typedef CSxsPointerBase<T, CSxsPointer> Base;
  55. CSxsPointer(const CSxsPointer&); // deliberately not implemented
  56. operator=(const CSxsPointer&); // deliberately not implemented
  57. public:
  58. CSxsPointer(T* p = NULL) : Base(p) { }
  59. CSxsPointer& operator=(T* p) { Base::operator=(p); return *this; }
  60. bool operator ==(const CSxsPointer &r) const { return m_p == r.m_p; }
  61. ~CSxsPointer() { }
  62. static void Delete(T* p) { if (p != NULL) { CSxsPreserveLastError ple; FUSION_DELETE_SINGLETON(p); ple.Restore(); } }
  63. HRESULT HrAllocate(PCSTR pszFileName, int nLine) { return Base::HrAllocateBase(pszFileName, nLine, pszTypeName); }
  64. BOOL Win32Allocate(PCSTR pszFileName, int nLine) { return Base::Win32AllocateBase(pszFileName, nLine, pszTypeName); }
  65. };
  66. #define SMARTPTR(_T) CSxsPointer<_T, _T::ms_szTypeName>
  67. #define SMARTTYPEDEF(_T) static PCSTR ms_szTypeName
  68. #define SMARTTYPE(_T) __declspec(selectany) PCSTR _T::ms_szTypeName = #_T
  69. template <typename T>
  70. class CSxsArrayPointer : public CSxsPointerBase<T, CSxsArrayPointer>
  71. {
  72. typedef CSxsPointerBase<T, CSxsArrayPointer> Base;
  73. public:
  74. CSxsArrayPointer(T* p = NULL) : Base(p) { }
  75. CSxsArrayPointer& operator=(T* p) { Base::operator=(p); return *this; }
  76. bool operator ==(const CSxsArrayPointer &r) const { return m_p == r.m_p; }
  77. bool operator ==(T *prgt) const { return m_p == prgt; }
  78. ~CSxsArrayPointer() { }
  79. static void Delete(T* p) { if (p != NULL) { CSxsPreserveLastError ple; FUSION_DELETE_ARRAY(p); ple.Restore(); } }
  80. private:
  81. CSxsArrayPointer(const CSxsArrayPointer &);
  82. void operator =(const CSxsArrayPointer &);
  83. };
  84. template <typename T, void (*Destructor)(T*)>
  85. class CSxsPointerWithNamedDestructor : public CSxsPointerBase<T, CSxsPointerWithNamedDestructor>
  86. {
  87. typedef CSxsPointerBase<T, CSxsPointerWithNamedDestructor> Base;
  88. CSxsPointerWithNamedDestructor(const CSxsPointerWithNamedDestructor&); // deliberately not implemented
  89. void operator=(const CSxsPointerWithNamedDestructor&); // deliberately not implemented
  90. public:
  91. CSxsPointerWithNamedDestructor(T* p = NULL) : Base(p) { }
  92. CSxsPointerWithNamedDestructor& operator=(T* p) { Base::operator=(p); return *this; }
  93. bool operator ==(const CSxsPointerWithNamedDestructor &r) const { return m_p == r.m_p; }
  94. bool operator ==(T *pt) const { return m_p == pt; }
  95. ~CSxsPointerWithNamedDestructor() { CSxsPreserveLastError ple; Base::Delete(); ple.Restore(); }
  96. static void Delete(T* p) { if (p != NULL) { CSxsPreserveLastError ple; Destructor(p); ple.Restore(); } }
  97. };
  98. template <typename T, typename Derived>
  99. inline CSxsPointerBase<T, Derived>::CSxsPointerBase(T* p)
  100. : m_p(p) { }
  101. // if verify is an expression then
  102. //define IF_VERIFY(x) if (VERIFY(x))
  103. //define IF_NOT_VERIFY(x) if (!VERIFY(x))
  104. #define IF_VERIFY(x) VERIFY(x); if (x)
  105. #define IF_NOT_VERIFY(x) VERIFY(x); if (!(x))
  106. // in the second case, x must not have side effects
  107. template <typename T, typename Derived>
  108. inline void CSxsPointerBase<T, Derived>::operator=(T* p)
  109. { FN_TRACE(); IF_VERIFY(m_p == NULL) { m_p = p; } }
  110. template <typename T, typename Derived>
  111. inline void CSxsPointerBase<T, Derived>::Delete()
  112. { CSxsPreserveLastError ple; static_cast<Derived*>(this)->Delete(Detach()); ple.Restore(); }
  113. template <typename T, typename Derived>
  114. inline CSxsPointerBase<T, Derived>::~CSxsPointerBase()
  115. { CSxsPreserveLastError ple; Delete(); ple.Restore(); }
  116. #endif // !defined(_FUSION_INC_SMARTPTR_H_INCLUDED_)