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