ops.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Copyright (c) 2025 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_COBALT_OPS_HPP
  8. #define BOOST_COBALT_OPS_HPP
  9. #include <boost/cobalt/io/detail/config.hpp>
  10. #include <boost/cobalt/config.hpp>
  11. #include <boost/cobalt/detail/handler.hpp>
  12. #include <boost/cobalt/io/buffer.hpp>
  13. #include <boost/cobalt/op.hpp>
  14. #include <boost/cobalt/result.hpp>
  15. #include <optional>
  16. namespace boost::cobalt::io
  17. {
  18. struct BOOST_COBALT_IO_DECL write_op final : op<system::error_code, std::size_t>
  19. {
  20. const_buffer_sequence buffer;
  21. using implementation_t = void(void*, const_buffer_sequence, completion_handler<system::error_code, std::size_t>);
  22. using try_implementation_t = void(void*, const_buffer_sequence, handler<system::error_code, std::size_t>);
  23. BOOST_COBALT_MSVC_NOINLINE
  24. write_op(const_buffer_sequence buffer,
  25. void * this_,
  26. implementation_t *implementation,
  27. try_implementation_t * try_implementation = nullptr)
  28. : buffer(buffer), this_(this_),
  29. implementation_(implementation),
  30. try_implementation_(try_implementation)
  31. {}
  32. void initiate(completion_handler<system::error_code, std::size_t> handler) final
  33. {
  34. implementation_(this_, buffer, std::move(handler));
  35. }
  36. void ready(handler<system::error_code, std::size_t> handler) final
  37. {
  38. if (try_implementation_)
  39. try_implementation_(this_, buffer, std::move(handler));
  40. }
  41. ~write_op() = default;
  42. private:
  43. void *this_;
  44. implementation_t *implementation_;
  45. try_implementation_t * try_implementation_;
  46. };
  47. struct BOOST_COBALT_IO_DECL read_op final : op<system::error_code, std::size_t>
  48. {
  49. mutable_buffer_sequence buffer;
  50. using implementation_t = void(void*, mutable_buffer_sequence, completion_handler<system::error_code, std::size_t>);
  51. using try_implementation_t = void(void*, mutable_buffer_sequence, handler<system::error_code, std::size_t>);
  52. BOOST_COBALT_MSVC_NOINLINE
  53. read_op(mutable_buffer_sequence buffer,
  54. void * this_,
  55. implementation_t *implementation,
  56. try_implementation_t * try_implementation = nullptr)
  57. : buffer(buffer), this_(this_),
  58. implementation_(implementation),
  59. try_implementation_(try_implementation)
  60. {}
  61. void initiate(completion_handler<system::error_code, std::size_t> handler) final
  62. {
  63. implementation_(this_, buffer, std::move(handler));
  64. }
  65. void ready(handler<system::error_code, std::size_t> handler) final
  66. {
  67. if (try_implementation_)
  68. try_implementation_(this_, buffer, std::move(handler));
  69. }
  70. ~read_op() = default;
  71. private:
  72. void *this_;
  73. implementation_t *implementation_;
  74. try_implementation_t * try_implementation_;
  75. };
  76. struct BOOST_COBALT_IO_DECL write_at_op final : op<system::error_code, std::size_t>
  77. {
  78. std::uint64_t offset;
  79. const_buffer_sequence buffer;
  80. using implementation_t = void(void*, std::uint64_t, const_buffer_sequence, completion_handler<system::error_code, std::size_t>);
  81. using try_implementation_t = void(void*, std::uint64_t, const_buffer_sequence, handler<system::error_code, std::size_t>);
  82. BOOST_COBALT_MSVC_NOINLINE
  83. write_at_op(std::uint64_t offset,
  84. const_buffer_sequence buffer,
  85. void * this_,
  86. implementation_t *implementation,
  87. try_implementation_t * try_implementation = nullptr)
  88. : offset(offset), buffer(buffer), this_(this_),
  89. implementation_(implementation),
  90. try_implementation_(try_implementation)
  91. {}
  92. void initiate(completion_handler<system::error_code, std::size_t> handler) final
  93. {
  94. implementation_(this_, offset, buffer, std::move(handler));
  95. }
  96. void ready(handler<system::error_code, std::size_t> handler) final
  97. {
  98. if (try_implementation_)
  99. try_implementation_(this_, offset, buffer, std::move(handler));
  100. }
  101. ~write_at_op() = default;
  102. private:
  103. void *this_;
  104. implementation_t *implementation_;
  105. try_implementation_t * try_implementation_;
  106. };
  107. struct BOOST_COBALT_IO_DECL read_at_op final : op<system::error_code, std::size_t>
  108. {
  109. std::uint64_t offset;
  110. mutable_buffer_sequence buffer;
  111. using implementation_t = void(void*, std::uint64_t, mutable_buffer_sequence, completion_handler<system::error_code, std::size_t>);
  112. using try_implementation_t = void(void*, std::uint64_t, mutable_buffer_sequence, handler<system::error_code, std::size_t>);
  113. BOOST_COBALT_MSVC_NOINLINE
  114. read_at_op(std::uint64_t offset,
  115. mutable_buffer_sequence buffer,
  116. void * this_,
  117. implementation_t *implementation,
  118. try_implementation_t * try_implementation = nullptr)
  119. : offset(offset), buffer(buffer), this_(this_),
  120. implementation_(implementation),
  121. try_implementation_(try_implementation)
  122. {}
  123. void initiate(completion_handler<system::error_code, std::size_t> handler) final
  124. {
  125. implementation_(this_, offset, buffer, std::move(handler));
  126. }
  127. void ready(handler<system::error_code, std::size_t> handler) final
  128. {
  129. if (try_implementation_)
  130. try_implementation_(this_, offset, buffer, std::move(handler));
  131. }
  132. ~read_at_op() = default;
  133. private:
  134. void *this_;
  135. implementation_t *implementation_;
  136. try_implementation_t * try_implementation_;
  137. };
  138. struct BOOST_COBALT_IO_DECL wait_op final : op<system::error_code>
  139. {
  140. using implementation_t = void(void*, completion_handler<system::error_code>);
  141. using try_implementation_t = void(void*, handler<system::error_code>);
  142. BOOST_COBALT_MSVC_NOINLINE
  143. wait_op(void * this_,
  144. implementation_t *implementation,
  145. try_implementation_t * try_implementation = nullptr)
  146. : this_(this_),
  147. implementation_(implementation),
  148. try_implementation_(try_implementation)
  149. {}
  150. void initiate(completion_handler<system::error_code> handler) final
  151. {
  152. implementation_(this_, std::move(handler));
  153. }
  154. void ready(handler<system::error_code> handler) final
  155. {
  156. if (try_implementation_)
  157. try_implementation_(this_, std::move(handler));
  158. }
  159. ~wait_op() = default;
  160. private:
  161. void *this_;
  162. implementation_t *implementation_;
  163. try_implementation_t * try_implementation_;
  164. };
  165. }
  166. #endif //BOOST_COBALT_OPS_HPP