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.

160 lines
3.7 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1999
  5. //
  6. // File: SafeTemp.h
  7. //
  8. // Contents: A template for safe pointers.
  9. //
  10. // Classes: XSafeInterfacePtr<ISome>
  11. //
  12. // History: 6/3/1996 RaviR Created
  13. //____________________________________________________________________________
  14. //
  15. //____________________________________________________________________________
  16. //
  17. // Template: XSafeInterfacePtr
  18. //
  19. // Purpose: Safe pointer to any interface that supports AddRef/Release
  20. //
  21. // Notes: This works for classes that define AddRef/Release, or for
  22. // OLE interfaces. It is not necessary that the class
  23. // be a derivative of IUnknown, so long as it supports
  24. // AddRef and Release methods which have the same semantics as
  25. // those in IUnknown.
  26. //
  27. // The constructor takes a parameter which specifies whether
  28. // the captured pointer should be AddRef'd, defaulting to TRUE.
  29. //
  30. // The Copy function creates a valid additional copy of
  31. // the captured pointer (following the AddRef/Release protocol)
  32. // so can be used to hand out copies from a safe pointer declared
  33. // as a member of some other class.
  34. //
  35. // The 'Transfer' function transfers the interface pointer, and
  36. // invalidates its member value (by setting it to NULL).
  37. //
  38. // To release the existing interface ptr and set it to a new
  39. // instance use the 'Set' member fuction. This method takes a
  40. // parameter which specifies whether the new pointer should be
  41. // AddRef'd, defaulting to TRUE.
  42. //
  43. // The following methods manipulate the interface pointer with
  44. // out following the AddRef/Release protocol: Transfer, Attach
  45. // and Detach.
  46. //
  47. // History: 6/3/1996 RaviR Created
  48. //____________________________________________________________________________
  49. //
  50. template<class ISome>
  51. class XSafeInterfacePtr
  52. {
  53. public:
  54. inline XSafeInterfacePtr(ISome * pinter=NULL, BOOL fInc=TRUE)
  55. : _p ( pinter )
  56. {
  57. if (fInc && (_p != NULL))
  58. {
  59. _p->AddRef();
  60. }
  61. }
  62. inline ~XSafeInterfacePtr()
  63. {
  64. if (_p != NULL)
  65. {
  66. _p->Release();
  67. _p = NULL;
  68. }
  69. }
  70. inline BOOL IsNull(void)
  71. {
  72. return (_p == NULL);
  73. }
  74. inline void Copy(ISome **pxtmp)
  75. {
  76. *pxtmp = _p;
  77. if (_p != NULL)
  78. _p->AddRef();
  79. }
  80. inline void Transfer(ISome **pxtmp)
  81. {
  82. *pxtmp = _p;
  83. _p = NULL;
  84. }
  85. inline void Set(ISome* p, BOOL fInc = TRUE)
  86. {
  87. if (_p)
  88. {
  89. _p->Release();
  90. }
  91. _p = p;
  92. if (fInc && _p)
  93. {
  94. _p->AddRef();
  95. }
  96. }
  97. inline void SafeRelease(void)
  98. {
  99. if (_p)
  100. {
  101. _p->Release();
  102. _p = NULL;
  103. }
  104. }
  105. inline void SimpleRelease(void)
  106. {
  107. ASSERT(_p != NULL);
  108. _p->Release();
  109. _p = NULL;
  110. }
  111. inline void Attach(ISome* p)
  112. {
  113. ASSERT(_p == NULL);
  114. _p = p;
  115. }
  116. inline void Detach(void)
  117. {
  118. _p = NULL;
  119. }
  120. inline ISome * operator-> () { return _p; }
  121. inline ISome& operator * () { return *_p; }
  122. inline operator ISome *() { return _p; }
  123. inline ISome ** operator &()
  124. {
  125. ASSERT( _p == NULL );
  126. return &_p;
  127. }
  128. inline ISome *Self(void) { return _p; }
  129. private:
  130. ISome * _p;
  131. inline void operator= (const XSafeInterfacePtr &) {;}
  132. inline XSafeInterfacePtr(const XSafeInterfacePtr &){;}
  133. };