corewrappers.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifndef _WRL_COREWRAPPERS_H_
  7. #define _WRL_COREWRAPPERS_H_
  8. #include <windows.h>
  9. #include <intsafe.h>
  10. #include <winstring.h>
  11. #include <roapi.h>
  12. /* #include <wrl/def.h> */
  13. #include <wrl/internal.h>
  14. namespace Microsoft {
  15. namespace WRL {
  16. namespace Details {
  17. struct Dummy {};
  18. }
  19. namespace Wrappers {
  20. class HStringReference {
  21. private:
  22. void Init(const wchar_t* str, unsigned int len) {
  23. HRESULT hres = ::WindowsCreateStringReference(str, len, &header_, &hstr_);
  24. if (FAILED(hres))
  25. ::Microsoft::WRL::Details::RaiseException(hres);
  26. }
  27. HStringReference() : hstr_(nullptr) {}
  28. public:
  29. HStringReference(const wchar_t* str, unsigned int len) throw() : hstr_(nullptr) {
  30. Init(str, len);
  31. }
  32. template<unsigned int sizeDest>
  33. explicit HStringReference(wchar_t const (&str)[sizeDest]) throw() : hstr_(nullptr) {
  34. Init(str, sizeDest - 1);
  35. }
  36. template <size_t sizeDest>
  37. explicit HStringReference(wchar_t (&strRef)[sizeDest]) throw() {
  38. const wchar_t *str = static_cast<const wchar_t*>(strRef);
  39. Init(str, ::wcslen(str));
  40. }
  41. template<typename T>
  42. explicit HStringReference(const T &strRef) throw() : hstr_(nullptr) {
  43. const wchar_t* str = static_cast<const wchar_t*>(strRef);
  44. size_t len = ::wcslen(str);
  45. if(static_cast<size_t>(static_cast<unsigned int>(len)) != len)
  46. ::Microsoft::WRL::Details::RaiseException(INTSAFE_E_ARITHMETIC_OVERFLOW);
  47. Init(str, len);
  48. }
  49. HStringReference(const HStringReference &other) throw() : hstr_(nullptr) {
  50. unsigned int len = 0;
  51. const wchar_t* value = other.GetRawBuffer(&len);
  52. Init(value, len);
  53. }
  54. ~HStringReference() throw() {
  55. hstr_ = nullptr;
  56. }
  57. HStringReference& operator=(const HStringReference &other) throw() {
  58. unsigned int len = 0;
  59. const wchar_t* value = other.GetRawBuffer(&len);
  60. Init(value, len);
  61. return *this;
  62. }
  63. HSTRING Get() const throw() {
  64. return hstr_;
  65. }
  66. const wchar_t *GetRawBuffer(unsigned int *len) const {
  67. return ::WindowsGetStringRawBuffer(hstr_, len);
  68. }
  69. HRESULT CopyTo(HSTRING *str) const throw() {
  70. return ::WindowsDuplicateString(hstr_, str);
  71. }
  72. friend class HString;
  73. protected:
  74. HSTRING_HEADER header_;
  75. HSTRING hstr_;
  76. };
  77. class RoInitializeWrapper {
  78. public:
  79. RoInitializeWrapper(RO_INIT_TYPE flags) {
  80. hres = ::Windows::Foundation::Initialize(flags);
  81. }
  82. ~RoInitializeWrapper() {
  83. if(SUCCEEDED(hres))
  84. ::Windows::Foundation::Uninitialize();
  85. }
  86. operator HRESULT() {
  87. return hres;
  88. }
  89. private:
  90. HRESULT hres;
  91. };
  92. }
  93. }
  94. }
  95. #endif