Drop useless comment marker
[TortoiseGit.git] / src / Utils / SmartHandle.h
blob05eeccb4627250b4a685d4f0f5393a4ad96ce079
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.
20 #pragma once
21 #include <Uxtheme.h>
23 template <typename type>
24 struct CDefaultHandleNull
26 static constexpr type DefaultHandle()
28 return nullptr;
32 struct CDefaultHandleInvalid
34 static constexpr HANDLE DefaultHandle()
36 return INVALID_HANDLE_VALUE;
40 /**
41 * \ingroup Utils
42 * Helper classes for handles.
44 template <typename HandleType,
45 template <class> class CloseFunction,
46 typename NullType = CDefaultHandleNull<HandleType>>
47 class CSmartHandle
49 public:
50 CSmartHandle()
51 : m_Handle(NullType::DefaultHandle())
55 CSmartHandle(HandleType&& h)
56 : m_Handle(h)
60 CSmartHandle(CSmartHandle&& h)
61 : m_Handle(h.Detach())
65 CSmartHandle& operator=(CSmartHandle&& h)
67 *this = h.Detach();
68 return *this;
71 HandleType& operator=(HandleType&& h)
73 if (m_Handle != h)
75 CleanUp();
76 m_Handle = h;
79 return m_Handle;
82 bool CloseHandle()
84 return CleanUp();
87 HandleType Detach()
89 HandleType p = m_Handle;
90 m_Handle = NullType::DefaultHandle();
92 return p;
95 operator HandleType() const
97 return m_Handle;
100 HandleType * GetPointer()
102 return &m_Handle;
105 operator bool() const
107 return IsValid();
110 bool IsValid() const
112 return m_Handle != NullType::DefaultHandle();
116 ~CSmartHandle()
118 CleanUp();
121 private:
122 CSmartHandle(const HandleType&) = delete;
123 CSmartHandle(const CSmartHandle&) = delete;
124 HandleType& operator=(const HandleType&) = delete;
125 CSmartHandle& operator=(const CSmartHandle&) = delete;
127 protected:
128 bool CleanUp()
130 if (m_Handle != NullType::DefaultHandle())
132 const bool b = CloseFunction<HandleType>::Close(m_Handle);
133 m_Handle = NullType::DefaultHandle();
134 return b;
136 return false;
140 HandleType m_Handle;
143 template <typename T>
144 struct CCloseHandle
146 static bool Close(T handle)
148 return !!::CloseHandle(handle);
154 template <typename T>
155 struct CCloseRegKey
157 static bool Close(T handle)
159 return RegCloseKey(handle) == ERROR_SUCCESS;
164 template <typename T>
165 struct CCloseLibrary
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>
193 struct CCloseFILE
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>
211 struct CCloseIcon
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;
231 void CompilerTests()
233 // compiler tests
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