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 <wincrypt.h>
     30 #include <shlobj.h>
     31 #include <cassert>
     32 #include <string>
     33 
     34 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
     35   if (!ErrMsg)
     36     return true;
     37   char *buffer = NULL;
     38   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
     39       NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
     40   *ErrMsg = prefix + buffer;
     41   LocalFree(buffer);
     42   return true;
     43 }
     44 
     45 template <typename HandleTraits>
     46 class ScopedHandle {
     47   typedef typename HandleTraits::handle_type handle_type;
     48   handle_type Handle;
     49 
     50   ScopedHandle(const ScopedHandle &other); // = delete;
     51   void operator=(const ScopedHandle &other); // = delete;
     52 public:
     53   ScopedHandle()
     54     : Handle(HandleTraits::GetInvalid()) {}
     55 
     56   explicit ScopedHandle(handle_type h)
     57     : Handle(h) {}
     58 
     59   ~ScopedHandle() {
     60     if (HandleTraits::IsValid(Handle))
     61       HandleTraits::Close(Handle);
     62   }
     63 
     64   handle_type take() {
     65     handle_type t = Handle;
     66     Handle = HandleTraits::GetInvalid();
     67     return t;
     68   }
     69 
     70   ScopedHandle &operator=(handle_type h) {
     71     if (HandleTraits::IsValid(Handle))
     72       HandleTraits::Close(Handle);
     73     Handle = h;
     74     return *this;
     75   }
     76 
     77   // True if Handle is valid.
     78   operator bool() const {
     79     return HandleTraits::IsValid(Handle) ? true : false;
     80   }
     81 
     82   operator handle_type() const {
     83     return Handle;
     84   }
     85 };
     86 
     87 struct CommonHandleTraits {
     88   typedef HANDLE handle_type;
     89 
     90   static handle_type GetInvalid() {
     91     return INVALID_HANDLE_VALUE;
     92   }
     93 
     94   static void Close(handle_type h) {
     95     ::CloseHandle(h);
     96   }
     97 
     98   static bool IsValid(handle_type h) {
     99     return h != GetInvalid();
    100   }
    101 };
    102 
    103 struct JobHandleTraits : CommonHandleTraits {
    104   static handle_type GetInvalid() {
    105     return NULL;
    106   }
    107 };
    108 
    109 struct CryptContextTraits : CommonHandleTraits {
    110   typedef HCRYPTPROV handle_type;
    111 
    112   static handle_type GetInvalid() {
    113     return 0;
    114   }
    115 
    116   static void Close(handle_type h) {
    117     ::CryptReleaseContext(h, 0);
    118   }
    119 
    120   static bool IsValid(handle_type h) {
    121     return h != GetInvalid();
    122   }
    123 };
    124 
    125 struct FindHandleTraits : CommonHandleTraits {
    126   static void Close(handle_type h) {
    127     ::FindClose(h);
    128   }
    129 };
    130 
    131 struct FileHandleTraits : CommonHandleTraits {};
    132 
    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;
    138 
    139 namespace llvm {
    140 template <class T>
    141 class SmallVectorImpl;
    142 
    143 template <class T>
    144 typename SmallVectorImpl<T>::const_pointer
    145 c_str(SmallVectorImpl<T> &str) {
    146   str.push_back(0);
    147   str.pop_back();
    148   return str.data();
    149 }
    150 } // end namespace llvm.
    151