Home | History | Annotate | Download | only in Windows
      1 //===- Win32/Win32.h - Common Win32 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 Win32 implementations.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 //===----------------------------------------------------------------------===//
     15 //=== WARNING: Implementation here must contain only generic Win32 code that
     16 //===          is guaranteed to work on *all* Win32 variants.
     17 //===----------------------------------------------------------------------===//
     18 
     19 // mingw-w64 tends to define it as 0x0502 in its headers.
     20 #undef _WIN32_WINNT
     21 
     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
     26 
     27 #include "llvm/Config/config.h" // Get build system configuration settings
     28 #include <windows.h>
     29 #include <shlobj.h>
     30 #include <cassert>
     31 #include <string>
     32 
     33 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
     34   if (!ErrMsg)
     35     return true;
     36   char *buffer = NULL;
     37   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
     38       NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
     39   *ErrMsg = prefix + buffer;
     40   LocalFree(buffer);
     41   return true;
     42 }
     43 
     44 class AutoHandle {
     45   HANDLE handle;
     46 
     47 public:
     48   AutoHandle(HANDLE h) : handle(h) {}
     49 
     50   ~AutoHandle() {
     51     if (handle)
     52       CloseHandle(handle);
     53   }
     54 
     55   operator HANDLE() {
     56     return handle;
     57   }
     58 
     59   AutoHandle &operator=(HANDLE h) {
     60     handle = h;
     61     return *this;
     62   }
     63 };
     64 
     65 template <class HandleType, uintptr_t InvalidHandle,
     66           class DeleterType, DeleterType D>
     67 class ScopedHandle {
     68   HandleType Handle;
     69 
     70 public:
     71   ScopedHandle() : Handle(InvalidHandle) {}
     72   ScopedHandle(HandleType handle) : Handle(handle) {}
     73 
     74   ~ScopedHandle() {
     75     if (Handle != HandleType(InvalidHandle))
     76       D(Handle);
     77   }
     78 
     79   HandleType take() {
     80     HandleType temp = Handle;
     81     Handle = HandleType(InvalidHandle);
     82     return temp;
     83   }
     84 
     85   operator HandleType() const { return Handle; }
     86 
     87   ScopedHandle &operator=(HandleType handle) {
     88     Handle = handle;
     89     return *this;
     90   }
     91 
     92   typedef void (*unspecified_bool_type)();
     93   static void unspecified_bool_true() {}
     94 
     95   // True if Handle is valid.
     96   operator unspecified_bool_type() const {
     97     return Handle == HandleType(InvalidHandle) ? 0 : unspecified_bool_true;
     98   }
     99 
    100   bool operator!() const {
    101     return Handle == HandleType(InvalidHandle);
    102   }
    103 };
    104 
    105 typedef ScopedHandle<HANDLE, uintptr_t(-1),
    106                       BOOL (WINAPI*)(HANDLE), ::FindClose>
    107   ScopedFindHandle;
    108 
    109 namespace llvm {
    110 template <class T>
    111 class SmallVectorImpl;
    112 
    113 template <class T>
    114 typename SmallVectorImpl<T>::const_pointer
    115 c_str(SmallVectorImpl<T> &str) {
    116   str.push_back(0);
    117   str.pop_back();
    118   return str.data();
    119 }
    120 } // end namespace llvm.
    121