1 //===-- AutoHandle.h --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_lldb_Host_windows_AutoHandle_h_
10 #define LLDB_lldb_Host_windows_AutoHandle_h_
11 
12 #include "lldb/Host/windows/windows.h"
13 
14 namespace lldb_private {
15 
16 class AutoHandle {
17 public:
18   AutoHandle(HANDLE handle, HANDLE invalid_value = INVALID_HANDLE_VALUE)
m_handle(handle)19       : m_handle(handle), m_invalid_value(invalid_value) {}
20 
~AutoHandle()21   ~AutoHandle() {
22     if (m_handle != m_invalid_value)
23       ::CloseHandle(m_handle);
24   }
25 
IsValid()26   bool IsValid() const { return m_handle != m_invalid_value; }
27 
get()28   HANDLE get() const { return m_handle; }
29 
30 private:
31   HANDLE m_handle;
32   HANDLE m_invalid_value;
33 };
34 }
35 
36 #endif
37