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.

131 lines
3.0 KiB

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Filename : AutoHndl.h
  4. // Purpose : To automatically close open handles.
  5. //
  6. // Project : FTFS
  7. // Component: Common
  8. //
  9. // Author : urib
  10. //
  11. // Log:
  12. // Jan 20 1997 urib Creation
  13. // Jun 12 1997 urib Define the BAD_HANDLE macro if needed.
  14. // Feb 22 2000 urib fix bug 12038. Assignment doesn't free old handle.
  15. //
  16. ////////////////////////////////////////////////////////////////////////////////
  17. #ifndef AUTOHNDL_H
  18. #define AUTOHNDL_H
  19. #ifndef BAD_HANDLE
  20. #define BAD_HANDLE(h) ((0 == ((HANDLE)h))|| \
  21. (INVALID_HANDLE_VALUE == ((HANDLE)h)))
  22. #endif
  23. ////////////////////////////////////////////////////////////////////////////////
  24. //
  25. // CAutoHandle class definition
  26. //
  27. ////////////////////////////////////////////////////////////////////////////////
  28. class CAutoHandle
  29. {
  30. public:
  31. // Constructor
  32. CAutoHandle(HANDLE h = NULL)
  33. :m_h(h){}
  34. // Behave like a HANDLE in assignments
  35. CAutoHandle& operator=(HANDLE h)
  36. {
  37. if ((!BAD_HANDLE(m_h)) && // A valid handle is kept by us
  38. (m_h != h)) // A new handle is different!
  39. {
  40. CloseHandle(m_h);
  41. }
  42. m_h = h;
  43. return(*this);
  44. }
  45. // Every kind of a handle needs different closing.
  46. virtual
  47. ~CAutoHandle()
  48. {
  49. if (!BAD_HANDLE(m_h))
  50. {
  51. CloseHandle(m_h);
  52. m_h = NULL;
  53. }
  54. }
  55. // Behave like a handle
  56. operator HANDLE() const
  57. {
  58. return m_h;
  59. }
  60. // Allow access to the actual memory of the handle.
  61. HANDLE* operator &()
  62. {
  63. Assert(BAD_HANDLE(m_h));
  64. return &m_h;
  65. }
  66. HANDLE Detach()
  67. {
  68. HANDLE h = m_h;
  69. m_h = NULL;
  70. return h;
  71. }
  72. protected:
  73. // The handle.
  74. HANDLE m_h;
  75. private:
  76. CAutoHandle(CAutoHandle&);
  77. CAutoHandle& operator=(CAutoHandle&);
  78. };
  79. ////////////////////////////////////////////////////////////////////////////////
  80. //
  81. // CAutoChangeNotificationHandle class definition
  82. //
  83. ////////////////////////////////////////////////////////////////////////////////
  84. class CAutoChangeNotificationHandle :public CAutoHandle
  85. {
  86. public:
  87. // Constructor
  88. CAutoChangeNotificationHandle(HANDLE h = NULL)
  89. :CAutoHandle(h){};
  90. // These operators are not derived and therefore must be reimplemented.
  91. CAutoChangeNotificationHandle& operator=(HANDLE h)
  92. {
  93. m_h = h;
  94. return(*this);
  95. }
  96. // The proper closing.
  97. virtual
  98. ~CAutoChangeNotificationHandle()
  99. {
  100. if (!BAD_HANDLE(m_h))
  101. {
  102. FindCloseChangeNotification(m_h);
  103. m_h = NULL;
  104. }
  105. }
  106. private:
  107. CAutoChangeNotificationHandle(CAutoChangeNotificationHandle&);
  108. operator=(CAutoChangeNotificationHandle&);
  109. };
  110. #endif /* AUTOHNDL_H */