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 11 #ifndef BOOST_ASIO_DETAIL_WINRT_UTILS_HPP 12 #define BOOST_ASIO_DETAIL_WINRT_UTILS_HPP 13 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 15 # pragma once 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18 #include <boost/asio/detail/config.hpp> 19 20 #if defined(BOOST_ASIO_WINDOWS_RUNTIME) 21 22 #include <codecvt> 23 #include <cstdlib> 24 #include <future> 25 #include <locale> 26 #include <robuffer.h> 27 #include <windows.storage.streams.h> 28 #include <wrl/implements.h> 29 #include <boost/asio/buffer.hpp> 30 #include <boost/system/error_code.hpp> 31 #include <boost/asio/detail/memory.hpp> 32 #include <boost/asio/detail/socket_ops.hpp> 33 34 #include <boost/asio/detail/push_options.hpp> 35 36 namespace boost { 37 namespace asio { 38 namespace detail { 39 namespace winrt_utils { 40 41 inline Platform::String^ string(const char* from) 42 { 43 std::wstring tmp(from, from + std::strlen(from)); 44 return ref new Platform::String(tmp.c_str()); 45 } 46 47 inline Platform::String^ string(const std::string& from) 48 { 49 std::wstring tmp(from.begin(), from.end()); 50 return ref new Platform::String(tmp.c_str()); 51 } 52 53 inline std::string string(Platform::String^ from) 54 { 55 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; 56 return converter.to_bytes(from->Data()); 57 } 58 59 inline Platform::String^ string(unsigned short from) 60 { 61 return string(std::to_string(from)); 62 } 63 64 template <typename T> 65 inline Platform::String^ string(const T& from) 66 { 67 return string(from.to_string()); 68 } 69 70 inline int integer(Platform::String^ from) 71 { 72 return _wtoi(from->Data()); 73 } 74 75 template <typename T> 76 inline Windows::Networking::HostName^ host_name(const T& from) 77 { 78 return ref new Windows::Networking::HostName((string)(from)); 79 } 80 81 template <typename ConstBufferSequence> 82 inline Windows::Storage::Streams::IBuffer^ buffer_dup( 83 const ConstBufferSequence& buffers) 84 { 85 using Microsoft::WRL::ComPtr; 86 using boost::asio::buffer_size; 87 std::size_t size = buffer_size(buffers); 88 auto b = ref new Windows::Storage::Streams::Buffer(size); 89 ComPtr<IInspectable> insp = reinterpret_cast<IInspectable*>(b); 90 ComPtr<Windows::Storage::Streams::IBufferByteAccess> bacc; 91 insp.As(&bacc); 92 byte* bytes = nullptr; 93 bacc->Buffer(&bytes); 94 boost::asio::buffer_copy(boost::asio::buffer(bytes, size), buffers); 95 b->Length = size; 96 return b; 97 } 98 99 } // namespace winrt_utils 100 } // namespace detail 101 } // namespace asio 102 } // namespace boost 103 104 #include <boost/asio/detail/pop_options.hpp> 105 106 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME) 107 108 #endif // BOOST_ASIO_DETAIL_WINRT_UTILS_HPP 109