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.

118 lines
2.5 KiB

  1. //
  2. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  3. //
  4. // ***************************************************************************
  5. //
  6. // Original Author: Rajesh Rao
  7. //
  8. // $Author: rajeshr $
  9. // $Date: 6/11/98 4:43p $
  10. // $Workfile:refcount.cpp $
  11. //
  12. // $Modtime: 6/11/98 11:21a $
  13. // $Revision: 1 $
  14. // $Nokeywords: $
  15. //
  16. //
  17. // Description: Contains the imlementation for a basic reference counted object
  18. //
  19. //***************************************************************************
  20. #include "precomp.h"
  21. CRefCountedObject::CRefCountedObject() :
  22. m_dwRefCount ( 1 ),
  23. m_lpszName ( NULL ),
  24. m_CreationTime ( 0 ),
  25. m_LastAccessTime ( 0 )
  26. {
  27. // Initialize the critical section
  28. InitializeCriticalSection(&m_ReferenceCountSection);
  29. FILETIME fileTime;
  30. GetSystemTimeAsFileTime(&fileTime);
  31. LARGE_INTEGER creationTime;
  32. memcpy((LPVOID)&creationTime, (LPVOID)&fileTime, sizeof LARGE_INTEGER);
  33. m_CreationTime = creationTime.QuadPart;
  34. m_LastAccessTime = creationTime.QuadPart;
  35. }
  36. CRefCountedObject::CRefCountedObject(LPCWSTR lpszName) :
  37. m_dwRefCount ( 1 ),
  38. m_lpszName ( NULL ),
  39. m_CreationTime ( 0 ),
  40. m_LastAccessTime ( 0 )
  41. {
  42. // Initialize the critical section
  43. InitializeCriticalSection(&m_ReferenceCountSection);
  44. if(lpszName)
  45. {
  46. try
  47. {
  48. m_lpszName = new WCHAR[wcslen(lpszName) + 1];
  49. wcscpy(m_lpszName, lpszName);
  50. }
  51. catch ( ... )
  52. {
  53. if ( m_lpszName )
  54. {
  55. delete [] m_lpszName;
  56. m_lpszName = NULL;
  57. }
  58. DeleteCriticalSection(&m_ReferenceCountSection);
  59. throw;
  60. }
  61. }
  62. FILETIME fileTime;
  63. GetSystemTimeAsFileTime(&fileTime);
  64. LARGE_INTEGER creationTime;
  65. memcpy((LPVOID)&creationTime, (LPVOID)&fileTime, sizeof LARGE_INTEGER);
  66. m_CreationTime = creationTime.QuadPart;
  67. }
  68. CRefCountedObject::~CRefCountedObject()
  69. {
  70. delete[] m_lpszName;
  71. // Destroy the critical section
  72. DeleteCriticalSection(&m_ReferenceCountSection);
  73. }
  74. void CRefCountedObject::AddRef()
  75. {
  76. EnterCriticalSection(&m_ReferenceCountSection);
  77. m_dwRefCount ++;
  78. LeaveCriticalSection(&m_ReferenceCountSection);
  79. }
  80. void CRefCountedObject::Release()
  81. {
  82. EnterCriticalSection(&m_ReferenceCountSection);
  83. DWORD dwCount = --m_dwRefCount;
  84. LeaveCriticalSection(&m_ReferenceCountSection);
  85. if( dwCount == 0)
  86. delete this;
  87. }
  88. LPCWSTR CRefCountedObject::GetName()
  89. {
  90. return m_lpszName;
  91. }
  92. void CRefCountedObject::SetName(LPCWSTR lpszName)
  93. {
  94. delete[] m_lpszName;
  95. if(lpszName)
  96. {
  97. m_lpszName = new WCHAR[wcslen(lpszName) + 1];
  98. wcscpy(m_lpszName, lpszName);
  99. }
  100. else
  101. m_lpszName = NULL;
  102. }