buffer_sequence_adapter.ipp 2.9 KB

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