Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / asio / detail / winrt_utils.hpp
1 //
2 // detail/winrt_utils.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 <memory>
27 #include <robuffer.h>
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>
34
35 #include <boost/asio/detail/push_options.hpp>
36
37 namespace boost {
38 namespace asio {
39 namespace detail {
40 namespace winrt_utils {
41
42 inline Platform::String^ string(const char* from)
43 {
44   std::wstring tmp(from, from + std::strlen(from));
45   return ref new Platform::String(tmp.c_str());
46 }
47
48 inline Platform::String^ string(const std::string& from)
49 {
50   std::wstring tmp(from.begin(), from.end());
51   return ref new Platform::String(tmp.c_str());
52 }
53
54 inline std::string string(Platform::String^ from)
55 {
56   std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
57   return converter.to_bytes(from->Data());
58 }
59
60 inline Platform::String^ string(unsigned short from)
61 {
62   return string(std::to_string(from));
63 }
64
65 template <typename T>
66 inline Platform::String^ string(const T& from)
67 {
68   return string(from.to_string());
69 }
70
71 inline int integer(Platform::String^ from)
72 {
73   return _wtoi(from->Data());
74 }
75
76 template <typename T>
77 inline Windows::Networking::HostName^ host_name(const T& from)
78 {
79   return ref new Windows::Networking::HostName((string)(from));
80 }
81
82 template <typename ConstBufferSequence>
83 inline Windows::Storage::Streams::IBuffer^ buffer_dup(
84     const ConstBufferSequence& buffers)
85 {
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;
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