WPILibC++  unspecified
WindowsSupport.h
1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines things specific to Windows implementations. In addition to
11 // providing some helpers for working with win32 APIs, this header wraps
12 // <windows.h> with some portability macros. Always include WindowsSupport.h
13 // instead of including <windows.h> directly.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic Win32 code that
19 //=== is guaranteed to work on *all* Win32 variants.
20 //===----------------------------------------------------------------------===//
21 
22 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
23 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
24 
25 // mingw-w64 tends to define it as 0x0502 in its headers.
26 #undef _WIN32_WINNT
27 #undef _WIN32_IE
28 
29 // Require at least Windows 7 API.
30 #define _WIN32_WINNT 0x0601
31 #define _WIN32_IE 0x0800 // MinGW at it again. FIXME: verify if still needed.
32 #define WIN32_LEAN_AND_MEAN
33 #ifndef NOMINMAX
34 #define NOMINMAX
35 #endif
36 
37 #include "llvm/SmallVector.h"
38 #include "llvm/StringExtras.h"
39 #include "llvm/StringRef.h"
40 #include "llvm/Twine.h"
41 #include "llvm/Compiler.h"
42 #include <system_error>
43 #include <windows.h>
44 #include <cassert>
45 #include <string>
46 
51 inline bool RunningWindows8OrGreater() {
52  // Windows 8 is version 6.2, service pack 0.
53  OSVERSIONINFOEXW osvi = {};
54  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
55  osvi.dwMajorVersion = 6;
56  osvi.dwMinorVersion = 2;
57  osvi.wServicePackMajor = 0;
58 
59  DWORDLONG Mask = 0;
60  Mask = VerSetConditionMask(Mask, VER_MAJORVERSION, VER_GREATER_EQUAL);
61  Mask = VerSetConditionMask(Mask, VER_MINORVERSION, VER_GREATER_EQUAL);
62  Mask = VerSetConditionMask(Mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
63 
64  return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION |
65  VER_SERVICEPACKMAJOR,
66  Mask) != FALSE;
67 }
68 
69 inline bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix) {
70  if (!ErrMsg)
71  return true;
72  char *buffer = NULL;
73  DWORD LastError = GetLastError();
74  DWORD R = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
75  FORMAT_MESSAGE_FROM_SYSTEM |
76  FORMAT_MESSAGE_MAX_WIDTH_MASK,
77  NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
78  if (R)
79  *ErrMsg = prefix + ": " + buffer;
80  else
81  *ErrMsg = prefix + ": Unknown error";
82  *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
83 
84  LocalFree(buffer);
85  return R != 0;
86 }
87 
88 template <typename HandleTraits>
89 class ScopedHandle {
90  typedef typename HandleTraits::handle_type handle_type;
91  handle_type Handle;
92 
93  ScopedHandle(const ScopedHandle &other); // = delete;
94  void operator=(const ScopedHandle &other); // = delete;
95 public:
96  ScopedHandle()
97  : Handle(HandleTraits::GetInvalid()) {}
98 
99  explicit ScopedHandle(handle_type h)
100  : Handle(h) {}
101 
102  ~ScopedHandle() {
103  if (HandleTraits::IsValid(Handle))
104  HandleTraits::Close(Handle);
105  }
106 
107  handle_type take() {
108  handle_type t = Handle;
109  Handle = HandleTraits::GetInvalid();
110  return t;
111  }
112 
113  ScopedHandle &operator=(handle_type h) {
114  if (HandleTraits::IsValid(Handle))
115  HandleTraits::Close(Handle);
116  Handle = h;
117  return *this;
118  }
119 
120  // True if Handle is valid.
121  explicit operator bool() const {
122  return HandleTraits::IsValid(Handle) ? true : false;
123  }
124 
125  operator handle_type() const {
126  return Handle;
127  }
128 };
129 
131  typedef HANDLE handle_type;
132 
133  static handle_type GetInvalid() {
134  return INVALID_HANDLE_VALUE;
135  }
136 
137  static void Close(handle_type h) {
138  ::CloseHandle(h);
139  }
140 
141  static bool IsValid(handle_type h) {
142  return h != GetInvalid();
143  }
144 };
145 
147  static handle_type GetInvalid() {
148  return NULL;
149  }
150 };
151 
153  typedef HKEY handle_type;
154 
155  static handle_type GetInvalid() {
156  return NULL;
157  }
158 
159  static void Close(handle_type h) {
160  ::RegCloseKey(h);
161  }
162 
163  static bool IsValid(handle_type h) {
164  return h != GetInvalid();
165  }
166 };
167 
169  static void Close(handle_type h) {
170  ::FindClose(h);
171  }
172 };
173 
175 
181 
182 namespace llvm {
183 template <class T>
185 
186 template <class T>
187 typename SmallVectorImpl<T>::const_pointer
188 c_str(SmallVectorImpl<T> &str) {
189  str.push_back(0);
190  str.pop_back();
191  return str.data();
192 }
193 
194 namespace sys {
195 namespace path {
196 std::error_code widenPath(const Twine &Path8,
197  SmallVectorImpl<wchar_t> &Path16);
198 } // end namespace path
199 
200 namespace windows {
201 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
202 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
203  SmallVectorImpl<char> &utf8);
205 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
206  SmallVectorImpl<char> &utf8);
207 } // end namespace windows
208 } // end namespace sys
209 } // end namespace llvm.
210 
211 #endif
Definition: Path.inc:31
Definition: WindowsSupport.h:152
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Definition: WindowsSupport.h:89
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: WindowsSupport.h:184
Definition: WindowsSupport.h:146
Definition: WindowsSupport.h:168
Definition: WindowsSupport.h:130
Definition: WindowsSupport.h:174
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:134
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42