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.

142 lines
3.0 KiB

  1. /*++
  2. Copyright (c) 1998-2000 Microsoft Corporation
  3. Module Name :
  4. smartptr.h
  5. Abstract:
  6. Smart pointers and reference counting
  7. Revision History:
  8. --*/
  9. #pragma once
  10. #include "topobj.h"
  11. const DWORD kdwStackSize = 10;
  12. #if DBG
  13. typedef struct tagReferenceTraceRecord {
  14. PVOID Stack[kdwStackSize];
  15. class RefCount *pRefCount;
  16. PCHAR ClassName;
  17. LONG refs;
  18. } ReferenceTraceRecord;
  19. const DWORD kReferenceTraceMask = 0xFF;
  20. #endif
  21. class RefCount : public TopObj
  22. {
  23. private:
  24. LONG _crefs;
  25. #if DBG
  26. DWORD _dwReferenceTraceIndex;
  27. ReferenceTraceRecord _TraceRecordList[kReferenceTraceMask + 1];
  28. void RecordReferenceStack(LONG refs);
  29. #else
  30. #define RecordReferenceStack(refs)
  31. #endif
  32. public:
  33. RefCount(void) : _crefs(0)
  34. {
  35. #if DBG
  36. _dwReferenceTraceIndex = -1;
  37. #endif
  38. }
  39. virtual ~RefCount();
  40. void AddRef(void);
  41. void Release(void);
  42. };
  43. template <class T> class SmartPtr {
  44. T* p;
  45. public:
  46. SmartPtr()
  47. {
  48. p = NULL;
  49. }
  50. SmartPtr(SmartPtr<T> &sp)
  51. {
  52. p = sp;
  53. if (p != NULL) {
  54. p->AddRef();
  55. }
  56. }
  57. SmartPtr(T* p_) : p(p_)
  58. {
  59. if (p != NULL) {
  60. p->AddRef();
  61. }
  62. }
  63. ~SmartPtr(void)
  64. {
  65. if ( p != NULL) {
  66. p->Release();
  67. }
  68. }
  69. inline T* operator->(void)
  70. {
  71. // No referencing needed to access a member
  72. ASSERT(p != NULL);
  73. return p;
  74. }
  75. inline SmartPtr& operator=(SmartPtr<T> &p_)
  76. {
  77. // Referencing comes from using the other operator
  78. return operator=((T *) p_);
  79. }
  80. inline T& operator*(void)
  81. {
  82. // No referencing needed to derefence
  83. ASSERT(p != NULL);
  84. return *p;
  85. }
  86. inline operator T*(void)
  87. {
  88. // The assignee is responsible for doing the AddRef,
  89. // and in the SmartPtr case, does
  90. return p;
  91. }
  92. inline int operator==(const SmartPtr<T> &p_) const
  93. {
  94. // The cast doesn't reference, so we can just do the compare
  95. return ((T*)p_ == p);
  96. }
  97. inline int operator==(const void *p_) const
  98. {
  99. // The cast doesn't reference, so we can just do the compare
  100. return ((T*)p_ == p);
  101. }
  102. inline int operator!=(const SmartPtr<T> &p_) const
  103. {
  104. // The cast doesn't reference, so we can just do the compare
  105. return ((T*)p_ != p);
  106. }
  107. inline int operator!=(const void *p_) const
  108. {
  109. // The cast doesn't reference, so we can just do the compare
  110. return ((T*)p_ != p);
  111. }
  112. inline int operator!()
  113. {
  114. return !p;
  115. }
  116. SmartPtr& operator=(T* p_) {
  117. if (p != NULL) {
  118. // Remove our reference to the old one
  119. p->Release();
  120. }
  121. p = p_;
  122. if (p != NULL) {
  123. // Add our reference to the new one
  124. p->AddRef();
  125. }
  126. return *this;
  127. }
  128. };