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.

98 lines
2.3 KiB

  1. /*=========================================================================*\
  2. Module: _refcnt.h
  3. Copyright Microsoft Corporation 1997, All Rights Reserved.
  4. Stolen from Storext.h
  5. Description: Ref counted object defintion
  6. \*=========================================================================*/
  7. #ifndef _EX_REFCNT_H_
  8. #define _EX_REFCNT_H_
  9. /*==========================================================================*\
  10. IRefCounted
  11. Description: Your basic reference counting interface.
  12. Note:
  13. In most cases you shouldn't need to include this class as a base
  14. class in your refcounted object. Instead you should just derive
  15. derive your class directly from CRefCountedObject. You would only
  16. use IRefCounted with objects that can be used used where the
  17. code cannot or does not make assumptions on how the object
  18. implements its refcounting. E.g. classes that forward refcounting
  19. to parent classes or derive from two concrete refcounted base
  20. classes.
  21. \*==========================================================================*/
  22. class IRefCounted
  23. {
  24. // NOT IMPLEMENTED
  25. //
  26. IRefCounted& operator=(const IRefCounted&);
  27. public:
  28. // CREATORS
  29. //
  30. virtual ~IRefCounted() = 0 {}
  31. // MANIPULATORS
  32. //
  33. virtual void AddRef() = 0;
  34. virtual void Release() = 0;
  35. };
  36. /*==========================================================================*\
  37. CRefCountedObject
  38. Description: Provide simple reference counting for internal objects.
  39. NOTE: The ref-counting used here is NOT consistent with OLE/COM refcounting.
  40. This class was meant to be used with auto_ref_ptr.
  41. \*==========================================================================*/
  42. class CRefCountedObject
  43. {
  44. private:
  45. // NOT IMPLEMENTED
  46. //
  47. // Force an error in instances where a copy constructor
  48. // was needed, but none was provided.
  49. //
  50. CRefCountedObject& operator=(const CRefCountedObject&);
  51. CRefCountedObject(const CRefCountedObject&);
  52. protected:
  53. LONG m_cRef;
  54. public:
  55. CRefCountedObject() : m_cRef(0) {}
  56. virtual ~CRefCountedObject() = 0 {}
  57. void AddRef()
  58. {
  59. InterlockedIncrement(&m_cRef);
  60. }
  61. void Release()
  62. {
  63. if (0 == InterlockedDecrement(&m_cRef))
  64. delete this;
  65. }
  66. };
  67. #endif // !_EX_REFCNT_H_