1 /** 2 * This file has no copyright assigned and is placed in the Public Domain. 3 * This file is part of the mingw-w64 runtime package. 4 * No warranty is given; refer to the file DISCLAIMER.PD within this package. 5 */ 6 7 #ifndef _WRL_COREWRAPPERS_H_ 8 #define _WRL_COREWRAPPERS_H_ 9 10 #include <windows.h> 11 #include <intsafe.h> 12 #include <winstring.h> 13 #include <roapi.h> 14 15 /* #include <wrl/def.h> */ 16 #include <wrl/internal.h> 17 18 namespace Microsoft { 19 namespace WRL { 20 namespace Details { 21 struct Dummy {}; 22 } 23 24 namespace Wrappers { 25 class HStringReference { 26 private: 27 void Init(const wchar_t* str, unsigned int len) { 28 HRESULT hres = ::WindowsCreateStringReference(str, len, &header_, &hstr_); 29 if (FAILED(hres)) 30 ::Microsoft::WRL::Details::RaiseException(hres); 31 } 32 33 HStringReference() : hstr_(nullptr) {} 34 35 public: 36 HStringReference(const wchar_t* str, unsigned int len) throw() : hstr_(nullptr) { 37 Init(str, len); 38 } 39 40 template<unsigned int sizeDest> 41 explicit HStringReference(wchar_t const (&str)[sizeDest]) throw() : hstr_(nullptr) { 42 Init(str, sizeDest - 1); 43 } 44 45 template <size_t sizeDest> 46 explicit HStringReference(wchar_t (&strRef)[sizeDest]) throw() { 47 const wchar_t *str = static_cast<const wchar_t*>(strRef); 48 Init(str, ::wcslen(str)); 49 } 50 51 template<typename T> 52 explicit HStringReference(const T &strRef) throw() : hstr_(nullptr) { 53 const wchar_t* str = static_cast<const wchar_t*>(strRef); 54 size_t len = ::wcslen(str); 55 if(static_cast<size_t>(static_cast<unsigned int>(len)) != len) 56 ::Microsoft::WRL::Details::RaiseException(INTSAFE_E_ARITHMETIC_OVERFLOW); 57 Init(str, len); 58 } 59 60 HStringReference(const HStringReference &other) throw() : hstr_(nullptr) { 61 unsigned int len = 0; 62 const wchar_t* value = other.GetRawBuffer(&len); 63 Init(value, len); 64 } 65 66 ~HStringReference() throw() { 67 hstr_ = nullptr; 68 } 69 70 HStringReference& operator=(const HStringReference &other) throw() { 71 unsigned int len = 0; 72 const wchar_t* value = other.GetRawBuffer(&len); 73 Init(value, len); 74 return *this; 75 } 76 77 HSTRING Get() const throw() { 78 return hstr_; 79 } 80 81 const wchar_t *GetRawBuffer(unsigned int *len) const { 82 return ::WindowsGetStringRawBuffer(hstr_, len); 83 } 84 85 HRESULT CopyTo(HSTRING *str) const throw() { 86 return ::WindowsDuplicateString(hstr_, str); 87 } 88 89 friend class HString; 90 91 protected: 92 HSTRING_HEADER header_; 93 HSTRING hstr_; 94 }; 95 96 class RoInitializeWrapper { 97 public: 98 RoInitializeWrapper(RO_INIT_TYPE flags) { 99 hres = ::Windows::Foundation::Initialize(flags); 100 } 101 102 ~RoInitializeWrapper() { 103 if(SUCCEEDED(hres)) 104 ::Windows::Foundation::Uninitialize(); 105 } 106 107 operator HRESULT() { 108 return hres; 109 } 110 private: 111 HRESULT hres; 112 }; 113 } 114 } 115 } 116 117 #endif 118