2 // detail/winrt_utils.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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)
28 #include <windows.storage.streams.h>
29 #include <wrl/implements.h>
30 #include <boost/asio/buffer.hpp>
31 #include <boost/system/error_code.hpp>
32 #include <boost/asio/detail/addressof.hpp>
33 #include <boost/asio/detail/socket_ops.hpp>
35 #include <boost/asio/detail/push_options.hpp>
40 namespace winrt_utils {
42 inline Platform::String^ string(const char* from)
44 std::wstring tmp(from, from + std::strlen(from));
45 return ref new Platform::String(tmp.c_str());
48 inline Platform::String^ string(const std::string& from)
50 std::wstring tmp(from.begin(), from.end());
51 return ref new Platform::String(tmp.c_str());
54 inline std::string string(Platform::String^ from)
56 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
57 return converter.to_bytes(from->Data());
60 inline Platform::String^ string(unsigned short from)
62 return string(std::to_string(from));
66 inline Platform::String^ string(const T& from)
68 return string(from.to_string());
71 inline int integer(Platform::String^ from)
73 return _wtoi(from->Data());
77 inline Windows::Networking::HostName^ host_name(const T& from)
79 return ref new Windows::Networking::HostName((string)(from));
82 template <typename ConstBufferSequence>
83 inline Windows::Storage::Streams::IBuffer^ buffer_dup(
84 const ConstBufferSequence& buffers)
86 using Microsoft::WRL::ComPtr;
87 std::size_t size = boost::asio::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