Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:48

0001 //
0002 // detail/winrt_utils.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_WINRT_UTILS_HPP
0012 #define BOOST_ASIO_DETAIL_WINRT_UTILS_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
0021 
0022 #include <codecvt>
0023 #include <cstdlib>
0024 #include <future>
0025 #include <locale>
0026 #include <robuffer.h>
0027 #include <windows.storage.streams.h>
0028 #include <wrl/implements.h>
0029 #include <boost/asio/buffer.hpp>
0030 #include <boost/system/error_code.hpp>
0031 #include <boost/asio/detail/memory.hpp>
0032 #include <boost/asio/detail/socket_ops.hpp>
0033 
0034 #include <boost/asio/detail/push_options.hpp>
0035 
0036 namespace boost {
0037 namespace asio {
0038 namespace detail {
0039 namespace winrt_utils {
0040 
0041 inline Platform::String^ string(const char* from)
0042 {
0043   std::wstring tmp(from, from + std::strlen(from));
0044   return ref new Platform::String(tmp.c_str());
0045 }
0046 
0047 inline Platform::String^ string(const std::string& from)
0048 {
0049   std::wstring tmp(from.begin(), from.end());
0050   return ref new Platform::String(tmp.c_str());
0051 }
0052 
0053 inline std::string string(Platform::String^ from)
0054 {
0055   std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
0056   return converter.to_bytes(from->Data());
0057 }
0058 
0059 inline Platform::String^ string(unsigned short from)
0060 {
0061   return string(std::to_string(from));
0062 }
0063 
0064 template <typename T>
0065 inline Platform::String^ string(const T& from)
0066 {
0067   return string(from.to_string());
0068 }
0069 
0070 inline int integer(Platform::String^ from)
0071 {
0072   return _wtoi(from->Data());
0073 }
0074 
0075 template <typename T>
0076 inline Windows::Networking::HostName^ host_name(const T& from)
0077 {
0078   return ref new Windows::Networking::HostName((string)(from));
0079 }
0080 
0081 template <typename ConstBufferSequence>
0082 inline Windows::Storage::Streams::IBuffer^ buffer_dup(
0083     const ConstBufferSequence& buffers)
0084 {
0085   using Microsoft::WRL::ComPtr;
0086   using boost::asio::buffer_size;
0087   std::size_t size = buffer_size(buffers);
0088   auto b = ref new Windows::Storage::Streams::Buffer(size);
0089   ComPtr<IInspectable> insp = reinterpret_cast<IInspectable*>(b);
0090   ComPtr<Windows::Storage::Streams::IBufferByteAccess> bacc;
0091   insp.As(&bacc);
0092   byte* bytes = nullptr;
0093   bacc->Buffer(&bytes);
0094   boost::asio::buffer_copy(boost::asio::buffer(bytes, size), buffers);
0095   b->Length = size;
0096   return b;
0097 }
0098 
0099 } // namespace winrt_utils
0100 } // namespace detail
0101 } // namespace asio
0102 } // namespace boost
0103 
0104 #include <boost/asio/detail/pop_options.hpp>
0105 
0106 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0107 
0108 #endif // BOOST_ASIO_DETAIL_WINRT_UTILS_HPP