1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011 - 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
= NULL
>
30 class CSmartHandle
: public CloseFunction
<HandleType
>
35 m_Handle
= NULL_VALUE
;
38 CSmartHandle(HandleType h
)
43 HandleType
operator=(HandleType h
)
64 m_Handle
= NULL_VALUE
;
74 HandleType
* GetPointer()
86 return m_Handle
!= NULL_VALUE
;
96 CSmartHandle(const CSmartHandle
& that
);
97 CSmartHandle
& operator=(const CSmartHandle
& that
);
102 if ( m_Handle
!= NULL_VALUE
)
104 bool b
= Close(m_Handle
);
105 m_Handle
= NULL_VALUE
;
119 template <typename T
>
124 return !!::CloseHandle(handle
);
135 template <typename T
>
140 return RegCloseKey(handle
) == ERROR_SUCCESS
;
150 template <typename T
>
155 return !!::FreeLibrary(handle
);
165 template <typename T
>
166 struct CCloseViewOfFile
170 return !!::UnmapViewOfFile(handle
);
179 template <typename T
>
180 struct CCloseFindFile
184 return !!::FindClose(handle
);
194 // Client code (definitions of standard Windows handles).
195 typedef CSmartHandle
<HANDLE
, CCloseHandle
> CAutoGeneralHandle
;
196 typedef CSmartHandle
<HKEY
, CCloseRegKey
> CAutoRegKey
;
197 typedef CSmartHandle
<PVOID
, CCloseViewOfFile
> CAutoViewOfFile
;
198 typedef CSmartHandle
<HMODULE
, CCloseLibrary
> CAutoLibrary
;
199 typedef CSmartHandle
<HANDLE
, CCloseHandle
, INVALID_HANDLE_VALUE
> CAutoFile
;
200 typedef CSmartHandle
<HANDLE
, CCloseFindFile
, INVALID_HANDLE_VALUE
> CAutoFindFile
;