buffer_sequence_adapter.ipp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // detail/impl/buffer_sequence_adapter.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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. #ifndef BOOST_ASIO_DETAIL_IMPL_BUFFER_SEQUENCE_ADAPTER_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_BUFFER_SEQUENCE_ADAPTER_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  17. #include <robuffer.h>
  18. #include <windows.storage.streams.h>
  19. #include <wrl/implements.h>
  20. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. class winrt_buffer_impl :
  26. public Microsoft::WRL::RuntimeClass<
  27. Microsoft::WRL::RuntimeClassFlags<
  28. Microsoft::WRL::RuntimeClassType::WinRtClassicComMix>,
  29. ABI::Windows::Storage::Streams::IBuffer,
  30. Windows::Storage::Streams::IBufferByteAccess>
  31. {
  32. public:
  33. explicit winrt_buffer_impl(const boost::asio::const_buffer& b)
  34. {
  35. bytes_ = const_cast<byte*>(static_cast<const byte*>(b.data()));
  36. length_ = b.size();
  37. capacity_ = b.size();
  38. }
  39. explicit winrt_buffer_impl(const boost::asio::mutable_buffer& b)
  40. {
  41. bytes_ = static_cast<byte*>(b.data());
  42. length_ = 0;
  43. capacity_ = b.size();
  44. }
  45. ~winrt_buffer_impl()
  46. {
  47. }
  48. STDMETHODIMP Buffer(byte** value)
  49. {
  50. *value = bytes_;
  51. return S_OK;
  52. }
  53. STDMETHODIMP get_Capacity(UINT32* value)
  54. {
  55. *value = capacity_;
  56. return S_OK;
  57. }
  58. STDMETHODIMP get_Length(UINT32 *value)
  59. {
  60. *value = length_;
  61. return S_OK;
  62. }
  63. STDMETHODIMP put_Length(UINT32 value)
  64. {
  65. if (value > capacity_)
  66. return E_INVALIDARG;
  67. length_ = value;
  68. return S_OK;
  69. }
  70. private:
  71. byte* bytes_;
  72. UINT32 length_;
  73. UINT32 capacity_;
  74. };
  75. void buffer_sequence_adapter_base::init_native_buffer(
  76. buffer_sequence_adapter_base::native_buffer_type& buf,
  77. const boost::asio::mutable_buffer& buffer)
  78. {
  79. std::memset(&buf, 0, sizeof(native_buffer_type));
  80. Microsoft::WRL::ComPtr<IInspectable> insp
  81. = Microsoft::WRL::Make<winrt_buffer_impl>(buffer);
  82. buf = reinterpret_cast<Windows::Storage::Streams::IBuffer^>(insp.Get());
  83. }
  84. void buffer_sequence_adapter_base::init_native_buffer(
  85. buffer_sequence_adapter_base::native_buffer_type& buf,
  86. const boost::asio::const_buffer& buffer)
  87. {
  88. std::memset(&buf, 0, sizeof(native_buffer_type));
  89. Microsoft::WRL::ComPtr<IInspectable> insp
  90. = Microsoft::WRL::Make<winrt_buffer_impl>(buffer);
  91. Platform::Object^ buf_obj = reinterpret_cast<Platform::Object^>(insp.Get());
  92. buf = reinterpret_cast<Windows::Storage::Streams::IBuffer^>(insp.Get());
  93. }
  94. } // namespace detail
  95. } // namespace asio
  96. } // namespace boost
  97. #include <boost/asio/detail/pop_options.hpp>
  98. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  99. #endif // BOOST_ASIO_DETAIL_IMPL_BUFFER_SEQUENCE_ADAPTER_IPP