Update editorconfig
[TortoiseGit.git] / src / Utils / SmartHandle.h
blob4560611022b7a9b248dbaa3836ee505c89280e2f
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011, 2015 - 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.
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()
35 m_Handle = NULL_VALUE;
38 CSmartHandle(HandleType h)
40 m_Handle = h;
43 HandleType operator=(HandleType h)
45 if (m_Handle != h)
47 CleanUp();
48 m_Handle = h;
51 return(*this);
54 bool CloseHandle()
56 return CleanUp();
59 HandleType Detach()
61 HandleType p = m_Handle;
62 m_Handle = NULL_VALUE;
64 return p;
67 operator HandleType() const
69 return m_Handle;
72 HandleType * GetPointer()
74 return &m_Handle;
77 operator bool() const
79 return IsValid();
82 bool IsValid() const
84 return m_Handle != NULL_VALUE;
88 ~CSmartHandle()
90 CleanUp();
93 private:
94 CSmartHandle(const CSmartHandle&) = delete;
95 CSmartHandle& operator=(const CSmartHandle&) = delete;
97 protected:
98 bool CleanUp()
100 if ( m_Handle != NULL_VALUE )
102 const bool b = CloseFunction<HandleType>::Close(m_Handle);
103 m_Handle = NULL_VALUE;
104 return b;
106 return false;
110 HandleType m_Handle;
113 template <typename T>
114 struct CCloseHandle
116 static bool Close(T handle)
118 return !!::CloseHandle(handle);
124 template <typename T>
125 struct CCloseRegKey
127 static bool Close(T handle)
129 return RegCloseKey(handle) == ERROR_SUCCESS;
134 template <typename T>
135 struct CCloseLibrary
137 static bool Close(T handle)
139 return !!::FreeLibrary(handle);
144 template <typename T>
145 struct CCloseViewOfFile
147 static bool Close(T handle)
149 return !!::UnmapViewOfFile(handle);
153 template <typename T>
154 struct CCloseFindFile
156 static bool Close(T handle)
158 return !!::FindClose(handle);
162 template <typename T>
163 struct CCloseFILE
165 static bool Close(T handle)
167 return !!fclose(handle);
172 // Client code (definitions of standard Windows handles).
173 typedef CSmartHandle<HANDLE, CCloseHandle> CAutoGeneralHandle;
174 typedef CSmartHandle<HKEY, CCloseRegKey> CAutoRegKey;
175 typedef CSmartHandle<PVOID, CCloseViewOfFile> CAutoViewOfFile;
176 typedef CSmartHandle<HMODULE, CCloseLibrary> CAutoLibrary;
177 typedef CSmartHandle<HANDLE, CCloseHandle, INVALID_HANDLE_VALUE> CAutoFile;
178 typedef CSmartHandle<HANDLE, CCloseFindFile, INVALID_HANDLE_VALUE> CAutoFindFile;
179 typedef CSmartHandle<FILE*, CCloseFILE> CAutoFILE;