1
// TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011, 2015 - TortoiseSVN
4 // Copyright (C) 2015-2016, 2019-2020 - 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.
23 template <typename type
>
24 struct CDefaultHandleNull
26 static constexpr type
DefaultHandle()
32 struct CDefaultHandleInvalid
34 static constexpr HANDLE
DefaultHandle()
36 return INVALID_HANDLE_VALUE
;
42 * Helper classes for handles.
44 template <typename HandleType
,
45 template <class> class CloseFunction
,
46 typename NullType
= CDefaultHandleNull
<HandleType
>>
51 : m_Handle(NullType::DefaultHandle())
55 CSmartHandle(HandleType
&& h
)
60 CSmartHandle(CSmartHandle
&& h
)
61 : m_Handle(h
.Detach())
65 CSmartHandle
& operator=(CSmartHandle
&& h
)
71 HandleType
& operator=(HandleType
&& h
)
89 HandleType p
= m_Handle
;
90 m_Handle
= NullType::DefaultHandle();
95 operator HandleType() const
100 HandleType
* GetPointer()
105 operator bool() const
112 return m_Handle
!= NullType::DefaultHandle();
122 CSmartHandle(const HandleType
&) = delete;
123 CSmartHandle(const CSmartHandle
&) = delete;
124 HandleType
& operator=(const HandleType
&) = delete;
125 CSmartHandle
& operator=(const CSmartHandle
&) = delete;
130 if (m_Handle
!= NullType::DefaultHandle())
132 const bool b
= CloseFunction
<HandleType
>::Close(m_Handle
);
133 m_Handle
= NullType::DefaultHandle();
143 template <typename T
>
146 static bool Close(T handle
)
148 return !!::CloseHandle(handle
);
154 template <typename T
>
157 static bool Close(T handle
)
159 return RegCloseKey(handle
) == ERROR_SUCCESS
;
164 template <typename T
>
167 static bool Close(T handle
)
169 return !!::FreeLibrary(handle
);
174 template <typename T
>
175 struct CCloseViewOfFile
177 static bool Close(T handle
)
179 return !!::UnmapViewOfFile(handle
);
183 template <typename T
>
184 struct CCloseFindFile
186 static bool Close(T handle
)
188 return !!::FindClose(handle
);
192 template <typename T
>
195 static bool Close(T handle
)
197 return !!fclose(handle
);
201 template <typename T
>
202 struct CCloseThemeData
204 static bool Close(T hTheme
)
206 return !!::CloseThemeData(hTheme
);
210 template <typename T
>
213 static bool Close(T handle
)
215 return !!DestroyIcon(handle
);
219 // Client code (definitions of standard Windows handles).
220 typedef CSmartHandle
<HANDLE
, CCloseHandle
> CAutoGeneralHandle
;
221 typedef CSmartHandle
<HKEY
, CCloseRegKey
> CAutoRegKey
;
222 typedef CSmartHandle
<PVOID
, CCloseViewOfFile
> CAutoViewOfFile
;
223 typedef CSmartHandle
<HMODULE
, CCloseLibrary
> CAutoLibrary
;
224 typedef CSmartHandle
<HANDLE
, CCloseHandle
, CDefaultHandleInvalid
> CAutoFile
;
225 typedef CSmartHandle
<HANDLE
, CCloseFindFile
, CDefaultHandleInvalid
> CAutoFindFile
;
226 typedef CSmartHandle
<FILE*, CCloseFILE
> CAutoFILE
;
227 typedef CSmartHandle
<HTHEME
, CCloseThemeData
> CAutoThemeData
;
228 typedef CSmartHandle
<HICON
, CCloseIcon
> CAutoIcon
;
235 HANDLE h = reinterpret_cast<HANDLE>(1);
236 CAutoFile hFile = h; // C2280
237 CAutoFile hFile2 = std::move(h); // OK
238 // OK, uses move semantics
239 CAutoFile hFile3 = CreateFile(L"c:\\test.txt", GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
240 CAutoFile hFile4 = hFile3; // C2280
241 CAutoFile hFile5 = std::move(hFile3); // OK