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.

112 lines
3.0 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: CountedObjects.cpp
  3. //
  4. // Copyright (c) 1999-2000, Microsoft Corporation
  5. //
  6. // Base class that implements object reference counting
  7. //
  8. // History: 1999-08-17 vtan created
  9. // 2000-02-01 vtan moved from Neptune to Whistler
  10. // --------------------------------------------------------------------------
  11. #include "StandardHeader.h"
  12. #include "CountedObject.h"
  13. // --------------------------------------------------------------------------
  14. // CCountedObject::CCountedObject
  15. //
  16. // Arguments: <none>
  17. //
  18. // Returns: <none>
  19. //
  20. // Purpose: Initializes the reference count to 1.
  21. //
  22. // History: 1999-08-17 vtan created
  23. // --------------------------------------------------------------------------
  24. CCountedObject::CCountedObject (void) :
  25. _lReferenceCount(1),
  26. _fReleased(false)
  27. {
  28. }
  29. // --------------------------------------------------------------------------
  30. // CCountedObject::~CCountedObject
  31. //
  32. // Arguments: <none>
  33. //
  34. // Returns: <none>
  35. //
  36. // Purpose: Virtual destructor that just checks valid deletion.
  37. //
  38. // History: 1999-08-17 vtan created
  39. // --------------------------------------------------------------------------
  40. CCountedObject::~CCountedObject (void)
  41. {
  42. ASSERTMSG(_fReleased, "CCountedObject::~CCountedObject invoked without being released");
  43. }
  44. // --------------------------------------------------------------------------
  45. // CCountedObject::AddRef
  46. //
  47. // Arguments: <none>
  48. //
  49. // Returns: <none>
  50. //
  51. // Purpose: Increments the object's reference count.
  52. //
  53. // History: 1999-08-17 vtan created
  54. // --------------------------------------------------------------------------
  55. void CCountedObject::AddRef (void)
  56. {
  57. InterlockedIncrement(&_lReferenceCount);
  58. }
  59. // --------------------------------------------------------------------------
  60. // CCountedObject::Release
  61. //
  62. // Arguments: <none>
  63. //
  64. // Returns: <none>
  65. //
  66. // Purpose: Decrements the object's reference count. When the count
  67. // reaches zero the object is deleted. Do NOT use referenced
  68. // objects when using stack based objects. These must be
  69. // dynamically allocated.
  70. //
  71. // History: 1999-08-17 vtan created
  72. // --------------------------------------------------------------------------
  73. void CCountedObject::Release (void)
  74. {
  75. if (InterlockedDecrement(&_lReferenceCount) == 0)
  76. {
  77. _fReleased = true;
  78. delete this;
  79. }
  80. }
  81. // --------------------------------------------------------------------------
  82. // CCountedObject::GetCount
  83. //
  84. // Arguments: <none>
  85. //
  86. // Returns: LONG
  87. //
  88. // Purpose: Returns the count of outstanding references on the object.
  89. //
  90. // History: 2000-07-17 vtan created
  91. // --------------------------------------------------------------------------
  92. LONG CCountedObject::GetCount (void) const
  93. {
  94. return(_lReferenceCount);
  95. }