Navigate selection history via log jump up/down button
[TortoiseGit.git] / src / Utils / SmartHandle.h
blob4167bbdab3aee5803d21d6ab12fd8d85ef797277
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #pragma once
22 /**
23 * \ingroup Utils
24 * Helper classes for handles.
26 template <typename HandleType,
27 template <class> class CloseFunction,
28 HandleType NULL_VALUE = NULL>
29 class CSmartHandle : public CloseFunction<HandleType>
31 public:
32 CSmartHandle()
34 m_Handle = NULL_VALUE;
37 CSmartHandle(HandleType h)
39 m_Handle = h;
42 HandleType operator=(HandleType h)
44 if (m_Handle != h)
46 CleanUp();
47 m_Handle = h;
50 return(*this);
53 bool CloseHandle()
55 return CleanUp();
58 HandleType Detach()
60 HandleType p;
62 p = m_Handle;
63 m_Handle = NULL_VALUE;
65 return p;
68 operator HandleType()
70 return m_Handle;
73 HandleType * GetPointer()
75 return &m_Handle;
78 operator bool()
80 return IsValid();
83 bool IsValid()
85 return m_Handle != NULL_VALUE;
89 ~CSmartHandle()
91 CleanUp();
95 protected:
96 bool CleanUp()
98 if ( m_Handle != NULL_VALUE )
100 bool b = Close(m_Handle);
101 m_Handle = NULL_VALUE;
102 return b;
104 return false;
108 HandleType m_Handle;
111 class CEmptyClass
115 template <typename T>
116 struct CCloseHandle
118 bool Close(T handle)
120 return !!::CloseHandle(handle);
123 protected:
124 ~CCloseHandle()
131 template <typename T>
132 struct CCloseRegKey
134 bool Close(T handle)
136 return RegCloseKey(handle) == ERROR_SUCCESS;
139 protected:
140 ~CCloseRegKey()
146 template <typename T>
147 struct CCloseLibrary
149 bool Close(T handle)
151 return !!::FreeLibrary(handle);
154 protected:
155 ~CCloseLibrary()
161 template <typename T>
162 struct CCloseViewOfFile
164 bool Close(T handle)
166 return !!::UnmapViewOfFile(handle);
169 protected:
170 ~CCloseViewOfFile()
175 template <typename T>
176 struct CCloseFindFile
178 bool Close(T handle)
180 return !!::FindClose(handle);
183 protected:
184 ~CCloseFindFile()
190 // Client code (definitions of standard Windows handles).
191 typedef CSmartHandle<HANDLE, CCloseHandle> CAutoGeneralHandle;
192 typedef CSmartHandle<HKEY, CCloseRegKey> CAutoRegKey;
193 typedef CSmartHandle<PVOID, CCloseViewOfFile> CAutoViewOfFile;
194 typedef CSmartHandle<HMODULE, CCloseLibrary> CAutoLibrary;
195 typedef CSmartHandle<HANDLE, CCloseHandle, INVALID_HANDLE_VALUE> CAutoFile;
196 typedef CSmartHandle<HANDLE, CCloseFindFile, INVALID_HANDLE_VALUE> CAutoFindFile;