*.js files: keep the style consistent
[TortoiseGit.git] / src / Utils / SmartHandle.h
blob2b9d5a2396d005e4d1b92f2f15befc3f5c803314
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 void Detach()
60 m_Handle = NULL_VALUE;
63 operator HandleType()
65 return m_Handle;
68 HandleType * GetPointer()
70 return &m_Handle;
73 operator bool()
75 return IsValid();
78 bool IsValid()
80 return m_Handle != NULL_VALUE;
84 ~CSmartHandle()
86 CleanUp();
90 protected:
91 bool CleanUp()
93 if ( m_Handle != NULL_VALUE )
95 bool b = Close(m_Handle);
96 m_Handle = NULL_VALUE;
97 return b;
99 return false;
103 HandleType m_Handle;
106 class CEmptyClass
110 template <typename T>
111 struct CCloseHandle
113 bool Close(T handle)
115 return !!::CloseHandle(handle);
118 protected:
119 ~CCloseHandle()
126 template <typename T>
127 struct CCloseRegKey
129 bool Close(T handle)
131 return RegCloseKey(handle) == ERROR_SUCCESS;
134 protected:
135 ~CCloseRegKey()
141 template <typename T>
142 struct CCloseLibrary
144 bool Close(T handle)
146 return !!::FreeLibrary(handle);
149 protected:
150 ~CCloseLibrary()
156 template <typename T>
157 struct CCloseViewOfFile
159 bool Close(T handle)
161 return !!::UnmapViewOfFile(handle);
164 protected:
165 ~CCloseViewOfFile()
170 template <typename T>
171 struct CCloseFindFile
173 bool Close(T handle)
175 return !!::FindClose(handle);
178 protected:
179 ~CCloseFindFile()
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;