1 //===- Win32/Win32.h - Common Win32 Include File ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines things specific to Win32 implementations.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic Win32 code that
16 //=== is guaranteed to work on *all* Win32 variants.
17 //===----------------------------------------------------------------------===//
19 // mingw-w64 tends to define it as 0x0502 in its headers.
22 // Require at least Windows XP(5.1) API.
23 #define _WIN32_WINNT 0x0501
24 #define _WIN32_IE 0x0600 // MinGW at it again.
25 #define WIN32_LEAN_AND_MEAN
27 #include "llvm/Config/config.h" // Get build system configuration settings
34 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
38 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
39 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
40 *ErrMsg = prefix + buffer;
45 template <typename HandleTraits>
47 typedef typename HandleTraits::handle_type handle_type;
50 ScopedHandle(const ScopedHandle &other); // = delete;
51 void operator=(const ScopedHandle &other); // = delete;
54 : Handle(HandleTraits::GetInvalid()) {}
56 explicit ScopedHandle(handle_type h)
60 if (HandleTraits::IsValid(Handle))
61 HandleTraits::Close(Handle);
65 handle_type t = Handle;
66 Handle = HandleTraits::GetInvalid();
70 ScopedHandle &operator=(handle_type h) {
71 if (HandleTraits::IsValid(Handle))
72 HandleTraits::Close(Handle);
77 // True if Handle is valid.
78 operator bool() const {
79 return HandleTraits::IsValid(Handle) ? true : false;
82 operator handle_type() const {
87 struct CommonHandleTraits {
88 typedef HANDLE handle_type;
90 static handle_type GetInvalid() {
91 return INVALID_HANDLE_VALUE;
94 static void Close(handle_type h) {
98 static bool IsValid(handle_type h) {
99 return h != GetInvalid();
103 struct JobHandleTraits : CommonHandleTraits {
104 static handle_type GetInvalid() {
109 struct CryptContextTraits : CommonHandleTraits {
110 typedef HCRYPTPROV handle_type;
112 static handle_type GetInvalid() {
116 static void Close(handle_type h) {
117 ::CryptReleaseContext(h, 0);
120 static bool IsValid(handle_type h) {
121 return h != GetInvalid();
125 struct FindHandleTraits : CommonHandleTraits {
126 static void Close(handle_type h) {
131 struct FileHandleTraits : CommonHandleTraits {};
133 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
134 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
135 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
136 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
137 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
141 class SmallVectorImpl;
144 typename SmallVectorImpl<T>::const_pointer
145 c_str(SmallVectorImpl<T> &str) {
150 } // end namespace llvm.