winrt_utils.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // detail/winrt_utils.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 <memory>
  22. #include <robuffer.h>
  23. #include <windows.storage.streams.h>
  24. #include <wrl/implements.h>
  25. #include <boost/asio/buffer.hpp>
  26. #include <boost/system/error_code.hpp>
  27. #include <boost/asio/detail/addressof.hpp>
  28. #include <boost/asio/detail/socket_ops.hpp>
  29. #include <boost/asio/detail/push_options.hpp>
  30. namespace boost {
  31. namespace asio {
  32. namespace detail {
  33. namespace winrt_utils {
  34. inline Platform::String^ string(const char* from)
  35. {
  36. std::wstring tmp(from, from + std::strlen(from));
  37. return ref new Platform::String(tmp.c_str());
  38. }
  39. inline Platform::String^ string(const std::string& from)
  40. {
  41. std::wstring tmp(from.begin(), from.end());
  42. return ref new Platform::String(tmp.c_str());
  43. }
  44. inline std::string string(Platform::String^ from)
  45. {
  46. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  47. return converter.to_bytes(from->Data());
  48. }
  49. inline Platform::String^ string(unsigned short from)
  50. {
  51. return string(std::to_string(from));
  52. }
  53. template <typename T>
  54. inline Platform::String^ string(const T& from)
  55. {
  56. return string(from.to_string());
  57. }
  58. inline int integer(Platform::String^ from)
  59. {
  60. return _wtoi(from->Data());
  61. }
  62. template <typename T>
  63. inline Windows::Networking::HostName^ host_name(const T& from)
  64. {
  65. return ref new Windows::Networking::HostName((string)(from));
  66. }
  67. template <typename ConstBufferSequence>
  68. inline Windows::Storage::Streams::IBuffer^ buffer_dup(
  69. const ConstBufferSequence& buffers)
  70. {
  71. using Microsoft::WRL::ComPtr;
  72. std::size_t size = boost::asio::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