1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Helper classes for handles.
26 template <typename HandleType
,
27 template <class> class CloseFunction
,
28 HandleType NULL_VALUE
= NULL
>
29 class CSmartHandle
: public CloseFunction
<HandleType
>
34 m_Handle
= NULL_VALUE
;
37 CSmartHandle(HandleType h
)
42 HandleType
operator=(HandleType h
)
60 m_Handle
= NULL_VALUE
;
68 HandleType
* GetPointer()
80 return m_Handle
!= NULL_VALUE
;
93 if ( m_Handle
!= NULL_VALUE
)
95 bool b
= Close(m_Handle
);
96 m_Handle
= NULL_VALUE
;
110 template <typename T
>
115 return !!::CloseHandle(handle
);
126 template <typename T
>
131 return RegCloseKey(handle
) == ERROR_SUCCESS
;
141 template <typename T
>
146 return !!::FreeLibrary(handle
);
156 template <typename T
>
157 struct CCloseViewOfFile
161 return !!::UnmapViewOfFile(handle
);
170 template <typename T
>
171 struct CCloseFindFile
175 return !!::FindClose(handle
);
185 // Client code (definitions of standard Windows handles).
186 typedef CSmartHandle
<HANDLE
, CCloseHandle
> CAutoGeneralHandle
;
187 typedef CSmartHandle
<HKEY
, CCloseRegKey
> CAutoRegKey
;
188 typedef CSmartHandle
<PVOID
, CCloseViewOfFile
> CAutoViewOfFile
;
189 typedef CSmartHandle
<HMODULE
, CCloseLibrary
> CAutoLibrary
;
190 typedef CSmartHandle
<HANDLE
, CCloseHandle
, INVALID_HANDLE_VALUE
> CAutoFile
;
191 typedef CSmartHandle
<HANDLE
, CCloseFindFile
, INVALID_HANDLE_VALUE
> CAutoFindFile
;