1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011, 2015 - TortoiseSVN
4 // Copyright (C) 2015-2016 - 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>
34 : m_Handle(NULL_VALUE
)
38 CSmartHandle(HandleType
&& h
)
43 CSmartHandle(CSmartHandle
&& h
)
44 : m_Handle(h
.Detach())
48 CSmartHandle
& operator=(CSmartHandle
&& h
)
54 HandleType
& operator=(HandleType
&& h
)
72 HandleType p
= m_Handle
;
73 m_Handle
= NULL_VALUE
;
78 operator HandleType() const
83 HandleType
* GetPointer()
95 return m_Handle
!= NULL_VALUE
;
105 CSmartHandle(const HandleType
&) = delete;
106 CSmartHandle(const CSmartHandle
&) = delete;
107 HandleType
& operator=(const HandleType
&) = delete;
108 CSmartHandle
& operator=(const CSmartHandle
&) = delete;
113 if ( m_Handle
!= NULL_VALUE
)
115 const bool b
= CloseFunction
<HandleType
>::Close(m_Handle
);
116 m_Handle
= NULL_VALUE
;
126 template <typename T
>
129 static bool Close(T handle
)
131 return !!::CloseHandle(handle
);
137 template <typename T
>
140 static bool Close(T handle
)
142 return RegCloseKey(handle
) == ERROR_SUCCESS
;
147 template <typename T
>
150 static bool Close(T handle
)
152 return !!::FreeLibrary(handle
);
157 template <typename T
>
158 struct CCloseViewOfFile
160 static bool Close(T handle
)
162 return !!::UnmapViewOfFile(handle
);
166 template <typename T
>
167 struct CCloseFindFile
169 static bool Close(T handle
)
171 return !!::FindClose(handle
);
175 template <typename T
>
178 static bool Close(T handle
)
180 return !!fclose(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
;
192 typedef CSmartHandle
<FILE*, CCloseFILE
> CAutoFILE
;
199 HANDLE h = (HANDLE)1;
200 CAutoFile hFile = h; // C2280
201 CAutoFile hFile2 = std::move(h); // OK
202 // OK, uses move semantics
203 CAutoFile hFile3 = CreateFile(L"c:\\test.txt", GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
204 CAutoFile hFile4 = hFile3; // C2280
205 CAutoFile hFile5 = std::move(hFile3); // OK