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.

158 lines
3.6 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: ar.h
  6. //
  7. // Owner: YanL
  8. //
  9. // Description:
  10. //
  11. // auto_resource and dirived definitions
  12. //
  13. //
  14. //=======================================================================
  15. #pragma once
  16. //----------------------------------------------------------------------
  17. //
  18. // class auto_resource
  19. // This class eliminates need for cleanup when working with pointers and
  20. // handles of the different kind
  21. //
  22. // class _Rh - resource "handle"
  23. //
  24. // class _Rh_Traits - required traits for this recource "handle"
  25. // Should be define as:
  26. // struct _Rh_Traits {
  27. // static _Rh invalid() {}
  28. // static void release(_Rh rh) {}
  29. // };
  30. //
  31. //----------------------------------------------------------------------
  32. template< class _Rh, class _Rh_Traits >
  33. class auto_resource {
  34. public:
  35. auto_resource(_Rh rh = _Rh_Traits::invalid()) : _r(rh)
  36. {
  37. }
  38. ~auto_resource()
  39. {
  40. release();
  41. }
  42. void release()
  43. {
  44. if (valid())
  45. {
  46. _Rh_Traits::release(_r);
  47. _r = _Rh_Traits::invalid();
  48. }
  49. }
  50. const auto_resource& operator=(_Rh rh)
  51. {
  52. release();
  53. _r = rh;
  54. return *this;
  55. }
  56. operator _Rh() const
  57. {
  58. return _r;
  59. }
  60. _Rh* operator&()
  61. {
  62. release();
  63. return &_r;
  64. }
  65. bool valid() const
  66. {
  67. return (_Rh_Traits::invalid() != _r);
  68. }
  69. _Rh detach()
  70. {
  71. _Rh rh = _r;
  72. _r = _Rh_Traits::invalid();
  73. return rh;
  74. }
  75. protected:
  76. _Rh _r;
  77. private:
  78. // should not use copy constructor etc.
  79. auto_resource(const auto_resource&) {}
  80. const auto_resource& operator=(const auto_resource&) {}
  81. operator bool() const {}
  82. bool operator !() const {}
  83. };
  84. // handle to file has -1 as invalid
  85. struct auto_hfile_traits {
  86. static HANDLE invalid() { return INVALID_HANDLE_VALUE; }
  87. static void release(HANDLE h) { CloseHandle(h); }
  88. };
  89. typedef auto_resource< HANDLE, auto_hfile_traits > auto_hfile;
  90. // handle to all other kernel objects
  91. struct auto_handle_traits {
  92. static HANDLE invalid() { return 0; }
  93. static void release(HANDLE h) { CloseHandle(h); }
  94. };
  95. typedef auto_resource< HANDLE, auto_handle_traits > auto_handle;
  96. // handle to registry key (RegOpenKey, etc.)
  97. struct auto_hkey_traits {
  98. static HKEY invalid() { return (HKEY)0; }
  99. static void release(HKEY hkey) { RegCloseKey(hkey); }
  100. };
  101. typedef auto_resource<HKEY, auto_hkey_traits> auto_hkey;
  102. // handle to SetupDiXxxx registry key - invalid is INVALID_HANDLE_VALUE, not 0
  103. struct auto_hkeySetupDi_traits {
  104. static HKEY invalid() { return (HKEY) INVALID_HANDLE_VALUE; }
  105. static void release(HKEY hkey) { RegCloseKey(hkey); }
  106. };
  107. typedef auto_resource<HKEY, auto_hkeySetupDi_traits> auto_hkeySetupDi;
  108. // handle to load lib key
  109. struct auto_hlib_traits {
  110. static HINSTANCE invalid() { return 0; }
  111. static void release(HINSTANCE hlib) { FreeLibrary(hlib); }
  112. };
  113. typedef auto_resource< HINSTANCE, auto_hlib_traits > auto_hlib;
  114. // auto handle to HANDLE FindFirstFile
  115. struct auto_hfindfile_traits {
  116. static HANDLE invalid() { return INVALID_HANDLE_VALUE; }
  117. static void release(HANDLE hFindFile) { FindClose(hFindFile); }
  118. };
  119. typedef auto_resource< HANDLE, auto_hfindfile_traits > auto_hfindfile;
  120. // Pointer
  121. template< class _Ty >
  122. struct auto_pointer_traits {
  123. static _Ty* invalid() { return 0; }
  124. static void release(_Ty* p) { delete p; }
  125. };
  126. template< class _Ty >
  127. class auto_pointer : public auto_resource< _Ty*, auto_pointer_traits< _Ty > > {
  128. public:
  129. auto_pointer(_Ty* p = 0)
  130. : auto_resource<_Ty*, auto_pointer_traits< _Ty > >(p)
  131. {
  132. }
  133. const auto_pointer& operator=(_Ty* p)
  134. {
  135. auto_resource<_Ty*, auto_pointer_traits< _Ty > >::operator=(p);
  136. return *this;
  137. }
  138. _Ty* operator->() const
  139. {
  140. return _r;
  141. }
  142. };