winrt_utils.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // detail/winrt_utils.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_WINRT_UTILS_HPP
  11. #define BOOST_ASIO_DETAIL_WINRT_UTILS_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  17. #include <codecvt>
  18. #include <cstdlib>
  19. #include <future>
  20. #include <locale>
  21. #include <robuffer.h>
  22. #include <windows.storage.streams.h>
  23. #include <wrl/implements.h>
  24. #include <boost/asio/buffer.hpp>
  25. #include <boost/system/error_code.hpp>
  26. #include <boost/asio/detail/memory.hpp>
  27. #include <boost/asio/detail/socket_ops.hpp>
  28. #include <boost/asio/detail/push_options.hpp>
  29. namespace boost {
  30. namespace asio {
  31. namespace detail {
  32. namespace winrt_utils {
  33. inline Platform::String^ string(const char* from)
  34. {
  35. std::wstring tmp(from, from + std::strlen(from));
  36. return ref new Platform::String(tmp.c_str());
  37. }
  38. inline Platform::String^ string(const std::string& from)
  39. {
  40. std::wstring tmp(from.begin(), from.end());
  41. return ref new Platform::String(tmp.c_str());
  42. }
  43. inline std::string string(Platform::String^ from)
  44. {
  45. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  46. return converter.to_bytes(from->Data());
  47. }
  48. inline Platform::String^ string(unsigned short from)
  49. {
  50. return string(std::to_string(from));
  51. }
  52. template <typename T>
  53. inline Platform::String^ string(const T& from)
  54. {
  55. return string(from.to_string());
  56. }
  57. inline int integer(Platform::String^ from)
  58. {
  59. return _wtoi(from->Data());
  60. }
  61. template <typename T>
  62. inline Windows::Networking::HostName^ host_name(const T& from)
  63. {
  64. return ref new Windows::Networking::HostName((string)(from));
  65. }
  66. template <typename ConstBufferSequence>
  67. inline Windows::Storage::Streams::IBuffer^ buffer_dup(
  68. const ConstBufferSequence& buffers)
  69. {
  70. using Microsoft::WRL::ComPtr;
  71. using boost::asio::buffer_size;
  72. std::size_t size = buffer_size(buffers);
  73. auto b = ref new Windows::Storage::Streams::Buffer(size);
  74. ComPtr<IInspectable> insp = reinterpret_cast<IInspectable*>(b);
  75. ComPtr<Windows::Storage::Streams::IBufferByteAccess> bacc;
  76. insp.As(&bacc);
  77. byte* bytes = nullptr;
  78. bacc->Buffer(&bytes);
  79. boost::asio::buffer_copy(boost::asio::buffer(bytes, size), buffers);
  80. b->Length = size;
  81. return b;
  82. }
  83. } // namespace winrt_utils
  84. } // namespace detail
  85. } // namespace asio
  86. } // namespace boost
  87. #include <boost/asio/detail/pop_options.hpp>
  88. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  89. #endif // BOOST_ASIO_DETAIL_WINRT_UTILS_HPP