1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011, 2015 - TortoiseSVN
4 // Copyright (C) 2015 - TortoiseGit
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Helper classes for handles.
27 template <typename HandleType
,
28 template <class> class CloseFunction
,
29 HandleType NULL_VALUE
= nullptr>
35 m_Handle
= NULL_VALUE
;
38 CSmartHandle(HandleType h
)
43 HandleType
operator=(HandleType h
)
61 HandleType p
= m_Handle
;
62 m_Handle
= NULL_VALUE
;
67 operator HandleType() const
72 HandleType
* GetPointer()
84 return m_Handle
!= NULL_VALUE
;
94 CSmartHandle(const CSmartHandle
&) = delete;
95 CSmartHandle
& operator=(const CSmartHandle
&) = delete;
100 if ( m_Handle
!= NULL_VALUE
)
102 const bool b
= CloseFunction
<HandleType
>::Close(m_Handle
);
103 m_Handle
= NULL_VALUE
;
113 template <typename T
>
116 static bool Close(T handle
)
118 return !!::CloseHandle(handle
);
124 template <typename T
>
127 static bool Close(T handle
)
129 return RegCloseKey(handle
) == ERROR_SUCCESS
;
134 template <typename T
>
137 static bool Close(T handle
)
139 return !!::FreeLibrary(handle
);
144 template <typename T
>
145 struct CCloseViewOfFile
147 static bool Close(T handle
)
149 return !!::UnmapViewOfFile(handle
);
153 template <typename T
>
154 struct CCloseFindFile
156 static bool Close(T handle
)
158 return !!::FindClose(handle
);
162 template <typename T
>
165 static bool Close(T handle
)
167 return !!fclose(handle
);
172 // Client code (definitions of standard Windows handles).
173 typedef CSmartHandle
<HANDLE
, CCloseHandle
> CAutoGeneralHandle
;
174 typedef CSmartHandle
<HKEY
, CCloseRegKey
> CAutoRegKey
;
175 typedef CSmartHandle
<PVOID
, CCloseViewOfFile
> CAutoViewOfFile
;
176 typedef CSmartHandle
<HMODULE
, CCloseLibrary
> CAutoLibrary
;
177 typedef CSmartHandle
<HANDLE
, CCloseHandle
, INVALID_HANDLE_VALUE
> CAutoFile
;
178 typedef CSmartHandle
<HANDLE
, CCloseFindFile
, INVALID_HANDLE_VALUE
> CAutoFindFile
;
179 typedef CSmartHandle
<FILE*, CCloseFILE
> CAutoFILE
;