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
)
63 m_Handle
= NULL_VALUE
;
73 HandleType
* GetPointer()
85 return m_Handle
!= NULL_VALUE
;
98 if ( m_Handle
!= NULL_VALUE
)
100 bool b
= Close(m_Handle
);
101 m_Handle
= NULL_VALUE
;
115 template <typename T
>
120 return !!::CloseHandle(handle
);
131 template <typename T
>
136 return RegCloseKey(handle
) == ERROR_SUCCESS
;
146 template <typename T
>
151 return !!::FreeLibrary(handle
);
161 template <typename T
>
162 struct CCloseViewOfFile
166 return !!::UnmapViewOfFile(handle
);
175 template <typename T
>
176 struct CCloseFindFile
180 return !!::FindClose(handle
);
190 // Client code (definitions of standard Windows handles).
191 typedef CSmartHandle
<HANDLE
, CCloseHandle
> CAutoGeneralHandle
;
192 typedef CSmartHandle
<HKEY
, CCloseRegKey
> CAutoRegKey
;
193 typedef CSmartHandle
<PVOID
, CCloseViewOfFile
> CAutoViewOfFile
;
194 typedef CSmartHandle
<HMODULE
, CCloseLibrary
> CAutoLibrary
;
195 typedef CSmartHandle
<HANDLE
, CCloseHandle
, INVALID_HANDLE_VALUE
> CAutoFile
;
196 typedef CSmartHandle
<HANDLE
, CCloseFindFile
, INVALID_HANDLE_VALUE
> CAutoFindFile
;