CommitDlg: Update empty file list message
[TortoiseGit.git] / src / Utils / SmartHandle.h
blob75b7c2046ab285a9c3b7397f6778f9b13fb170f4
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011 - 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 = NULL>
30 class CSmartHandle : public CloseFunction<HandleType>
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;
63 p = m_Handle;
64 m_Handle = NULL_VALUE;
66 return p;
69 operator HandleType()
71 return m_Handle;
74 HandleType * GetPointer()
76 return &m_Handle;
79 operator bool()
81 return IsValid();
84 bool IsValid()
86 return m_Handle != NULL_VALUE;
90 ~CSmartHandle()
92 CleanUp();
95 private:
96 CSmartHandle(const CSmartHandle& that);
97 CSmartHandle& operator=(const CSmartHandle& that);
99 protected:
100 bool CleanUp()
102 if ( m_Handle != NULL_VALUE )
104 bool b = Close(m_Handle);
105 m_Handle = NULL_VALUE;
106 return b;
108 return false;
112 HandleType m_Handle;
115 class CEmptyClass
119 template <typename T>
120 struct CCloseHandle
122 bool Close(T handle)
124 return !!::CloseHandle(handle);
127 protected:
128 ~CCloseHandle()
135 template <typename T>
136 struct CCloseRegKey
138 bool Close(T handle)
140 return RegCloseKey(handle) == ERROR_SUCCESS;
143 protected:
144 ~CCloseRegKey()
150 template <typename T>
151 struct CCloseLibrary
153 bool Close(T handle)
155 return !!::FreeLibrary(handle);
158 protected:
159 ~CCloseLibrary()
165 template <typename T>
166 struct CCloseViewOfFile
168 bool Close(T handle)
170 return !!::UnmapViewOfFile(handle);
173 protected:
174 ~CCloseViewOfFile()
179 template <typename T>
180 struct CCloseFindFile
182 bool Close(T handle)
184 return !!::FindClose(handle);
187 protected:
188 ~CCloseFindFile()
194 // Client code (definitions of standard Windows handles).
195 typedef CSmartHandle<HANDLE, CCloseHandle> CAutoGeneralHandle;
196 typedef CSmartHandle<HKEY, CCloseRegKey> CAutoRegKey;
197 typedef CSmartHandle<PVOID, CCloseViewOfFile> CAutoViewOfFile;
198 typedef CSmartHandle<HMODULE, CCloseLibrary> CAutoLibrary;
199 typedef CSmartHandle<HANDLE, CCloseHandle, INVALID_HANDLE_VALUE> CAutoFile;
200 typedef CSmartHandle<HANDLE, CCloseFindFile, INVALID_HANDLE_VALUE> CAutoFindFile;