2 // detail/winrt_utils.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_WINRT_UTILS_HPP
12 #define BOOST_ASIO_DETAIL_WINRT_UTILS_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
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>
34 #include <boost/asio/detail/push_options.hpp>
39 namespace winrt_utils {
41 inline Platform::String^ string(const char* from)
43 std::wstring tmp(from, from + std::strlen(from));
44 return ref new Platform::String(tmp.c_str());
47 inline Platform::String^ string(const std::string& from)
49 std::wstring tmp(from.begin(), from.end());
50 return ref new Platform::String(tmp.c_str());
53 inline std::string string(Platform::String^ from)
55 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
56 return converter.to_bytes(from->Data());
59 inline Platform::String^ string(unsigned short from)
61 return string(std::to_string(from));
65 inline Platform::String^ string(const T& from)
67 return string(from.to_string());
70 inline int integer(Platform::String^ from)
72 return _wtoi(from->Data());
76 inline Windows::Networking::HostName^ host_name(const T& from)
78 return ref new Windows::Networking::HostName((string)(from));
81 template <typename ConstBufferSequence>
82 inline Windows::Storage::Streams::IBuffer^ buffer_dup(
83 const ConstBufferSequence& buffers)
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;
92 byte* bytes = nullptr;
94 boost::asio::buffer_copy(boost::asio::buffer(bytes, size), buffers);
99 } // namespace winrt_utils
100 } // namespace detail
104 #include <boost/asio/detail/pop_options.hpp>
106 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
108 #endif // BOOST_ASIO_DETAIL_WINRT_UTILS_HPP