pipe.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2024 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_IO_PIPE_HPP
  8. #define BOOST_COBALT_IO_PIPE_HPP
  9. #include <boost/cobalt/io/detail/config.hpp>
  10. #include <boost/cobalt/config.hpp>
  11. #include <boost/cobalt/io/buffer.hpp>
  12. #include <boost/cobalt/io/ops.hpp>
  13. #include <boost/cobalt/io/stream.hpp>
  14. #include <boost/cobalt/op.hpp>
  15. #include <boost/asio/basic_readable_pipe.hpp>
  16. #include <boost/asio/basic_writable_pipe.hpp>
  17. #include <boost/system/result.hpp>
  18. namespace boost::cobalt::io
  19. {
  20. BOOST_COBALT_IO_DECL
  21. system::result<std::pair<struct readable_pipe, struct writable_pipe>> pipe(
  22. const executor & executor = this_thread::get_executor()
  23. );
  24. struct BOOST_SYMBOL_VISIBLE readable_pipe final : read_stream
  25. {
  26. using native_handle_type = asio::basic_readable_pipe<executor>::native_handle_type;
  27. BOOST_COBALT_IO_DECL readable_pipe(const cobalt::executor & executor = this_thread::get_executor());
  28. BOOST_COBALT_IO_DECL readable_pipe(native_handle_type native_file, const cobalt::executor & executor = this_thread::get_executor());
  29. BOOST_COBALT_IO_DECL readable_pipe(readable_pipe && sf) noexcept;
  30. BOOST_COBALT_IO_DECL system::result<void> assign(native_handle_type native_file);
  31. BOOST_COBALT_IO_DECL system::result<void> cancel();
  32. BOOST_COBALT_IO_DECL executor get_executor();
  33. BOOST_COBALT_IO_DECL bool is_open() const;
  34. BOOST_COBALT_IO_DECL system::result<void> close();
  35. BOOST_COBALT_IO_DECL native_handle_type native_handle();
  36. BOOST_COBALT_IO_DECL system::result<native_handle_type> release();
  37. [[nodiscard]] read_op read_some(mutable_buffer_sequence buffer)
  38. {
  39. return {buffer, this, initiate_read_some_};
  40. }
  41. private:
  42. BOOST_COBALT_IO_DECL static void initiate_read_some_(void *, mutable_buffer_sequence, boost::cobalt::completion_handler<system::error_code, std::size_t>);
  43. BOOST_COBALT_IO_DECL
  44. friend system::result<std::pair<struct readable_pipe, struct writable_pipe>> pipe(const cobalt::executor & executor);
  45. asio::basic_readable_pipe<executor> implementation_;
  46. };
  47. struct BOOST_SYMBOL_VISIBLE writable_pipe final : write_stream
  48. {
  49. using native_handle_type = asio::basic_writable_pipe<executor>::native_handle_type;
  50. BOOST_COBALT_IO_DECL writable_pipe(const cobalt::executor & executor = this_thread::get_executor());
  51. BOOST_COBALT_IO_DECL writable_pipe(native_handle_type native_file, const cobalt::executor & executor = this_thread::get_executor());
  52. BOOST_COBALT_IO_DECL writable_pipe(writable_pipe && sf) noexcept;
  53. BOOST_COBALT_IO_DECL system::result<void> assign(native_handle_type native_file);
  54. BOOST_COBALT_IO_DECL system::result<void> cancel();
  55. BOOST_COBALT_IO_DECL executor get_executor();
  56. BOOST_COBALT_IO_DECL bool is_open() const;
  57. [[nodiscard]] write_op write_some(const_buffer_sequence buffer)
  58. {
  59. return {buffer, this, initiate_write_some_};
  60. }
  61. BOOST_COBALT_IO_DECL system::result<void> close();
  62. BOOST_COBALT_IO_DECL native_handle_type native_handle();
  63. BOOST_COBALT_IO_DECL system::result<native_handle_type> release();
  64. private:
  65. BOOST_COBALT_IO_DECL static void initiate_write_some_(void *, const_buffer_sequence, boost::cobalt::completion_handler<system::error_code, std::size_t>);
  66. BOOST_COBALT_IO_DECL
  67. friend system::result<std::pair<struct readable_pipe, struct writable_pipe>> pipe(const cobalt::executor & executor);
  68. asio::basic_writable_pipe<executor> implementation_;
  69. };
  70. }
  71. namespace boost::process
  72. {
  73. template<typename T>
  74. struct is_readable_pipe;
  75. template<>
  76. struct is_readable_pipe<boost::cobalt::io::readable_pipe> : std::true_type
  77. {
  78. };
  79. template<typename T>
  80. struct is_writable_pipe;
  81. template<>
  82. struct is_writable_pipe<boost::cobalt::io::writable_pipe> : std::true_type
  83. {
  84. };
  85. }
  86. #endif //BOOST_COBALT_IO_PIPE_HPP