CPatch: New memory management
[TortoiseGit.git] / src / Utils / SmartHandle.h
blob271de5f94a7bbe42de688c630afcd9a679db6161
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.
20 #pragma once
23 /**
24 * \ingroup Utils
25 * Helper classes for handles.
27 template <typename HandleType,
28 template <class> class CloseFunction,
29 HandleType NULL_VALUE = nullptr>
30 class CSmartHandle
32 public:
33 CSmartHandle()
34 : m_Handle(NULL_VALUE)
38 CSmartHandle(HandleType&& h)
39 : m_Handle(h)
43 CSmartHandle(CSmartHandle&& h)
44 : m_Handle(h.Detach())
48 CSmartHandle& operator=(CSmartHandle&& h)
50 *this = h.Detach();
51 return *this;
54 HandleType& operator=(HandleType&& h)
56 if (m_Handle != h)
58 CleanUp();
59 m_Handle = h;
62 return m_Handle;
65 bool CloseHandle()
67 return CleanUp();
70 HandleType Detach()
72 HandleType p = m_Handle;
73 m_Handle = NULL_VALUE;
75 return p;
78 operator HandleType() const
80 return m_Handle;
83 HandleType * GetPointer()
85 return &m_Handle;
88 operator bool() const
90 return IsValid();
93 bool IsValid() const
95 return m_Handle != NULL_VALUE;
99 ~CSmartHandle()
101 CleanUp();
104 private:
105 CSmartHandle(const HandleType&) = delete;
106 CSmartHandle(const CSmartHandle&) = delete;
107 HandleType& operator=(const HandleType&) = delete;
108 CSmartHandle& operator=(const CSmartHandle&) = delete;
110 protected:
111 bool CleanUp()
113 if ( m_Handle != NULL_VALUE )
115 const bool b = CloseFunction<HandleType>::Close(m_Handle);
116 m_Handle = NULL_VALUE;
117 return b;
119 return false;
123 HandleType m_Handle;
126 template <typename T>
127 struct CCloseHandle
129 static bool Close(T handle)
131 return !!::CloseHandle(handle);
137 template <typename T>
138 struct CCloseRegKey
140 static bool Close(T handle)
142 return RegCloseKey(handle) == ERROR_SUCCESS;
147 template <typename T>
148 struct CCloseLibrary
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>
176 struct CCloseFILE
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;
195 void CompilerTests()
197 // compiler tests
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