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.

55 lines
1.1 KiB

  1. #ifndef _REFCOUNTER_H_
  2. #define _REFCOUNTER_H_
  3. //----------------------------------------------------------------------------
  4. //
  5. // CRefCounter
  6. //
  7. // This class is supplementary to the TRefPtr template. It provides an
  8. // easy way to mix in reference counting properties with other classes
  9. //
  10. // Note that the destructor is protected. It must be protected so derivatives
  11. // can use it, but derivatives should not have a public destructor (since
  12. // this violates the reference counting pattern)
  13. //
  14. //---------------------------------------------------------------------------
  15. class CRefCounter
  16. {
  17. public:
  18. CRefCounter();
  19. void AddRef();
  20. void Release();
  21. protected:
  22. virtual ~CRefCounter();
  23. private:
  24. long m_lRefCount;
  25. };
  26. inline
  27. CRefCounter::CRefCounter()
  28. : m_lRefCount(0)
  29. {}
  30. inline
  31. CRefCounter::~CRefCounter()
  32. {
  33. _ASSERT( m_lRefCount == 0 );
  34. }
  35. inline void
  36. CRefCounter::AddRef()
  37. {
  38. ::InterlockedIncrement( &m_lRefCount );
  39. }
  40. inline void
  41. CRefCounter::Release()
  42. {
  43. if ( ::InterlockedDecrement( &m_lRefCount ) == 0 )
  44. {
  45. delete this;
  46. }
  47. }
  48. #endif // !_REFCOUNTER_H_