|
| 1 | +// |
| 2 | +// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 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 | +// Official repository: https://github.com/cppalliance/capy |
| 8 | +// |
| 9 | + |
| 10 | +#ifndef BOOST_BUFFERS_ANY_STREAM_HPP |
| 11 | +#define BOOST_BUFFERS_ANY_STREAM_HPP |
| 12 | + |
| 13 | +#include <boost/buffers/detail/config.hpp> |
| 14 | + |
| 15 | +#ifdef BOOST_BUFFERS_HAS_CORO |
| 16 | + |
| 17 | +#include <boost/buffers/buffer.hpp> |
| 18 | +#include <boost/capy/async_result.hpp> |
| 19 | +#include <boost/system/error_code.hpp> |
| 20 | +#include <cstdint> |
| 21 | +#include <memory> |
| 22 | + |
| 23 | +namespace boost { |
| 24 | +namespace buffers { |
| 25 | + |
| 26 | +/** Result of an asynchronous I/O operation. |
| 27 | +*/ |
| 28 | +struct io_result |
| 29 | +{ |
| 30 | + /** The error code, if any occurred. |
| 31 | + */ |
| 32 | + system::error_code ec; |
| 33 | + |
| 34 | + /** Number of bytes transferred. |
| 35 | + */ |
| 36 | + std::size_t bytes_transferred; |
| 37 | +}; |
| 38 | + |
| 39 | +/** Asynchronous result type for I/O operations. |
| 40 | +*/ |
| 41 | +using async_io_result = capy::async_result<io_result>; |
| 42 | + |
| 43 | +class any_stream |
| 44 | +{ |
| 45 | +public: |
| 46 | + struct impl |
| 47 | + { |
| 48 | + virtual ~impl() = default; |
| 49 | + |
| 50 | + virtual auto read_some( |
| 51 | + buffers::mutable_buffer) -> |
| 52 | + async_io_result = 0; |
| 53 | + |
| 54 | + virtual auto write_some( |
| 55 | + buffers::const_buffer) -> |
| 56 | + async_io_result = 0; |
| 57 | + }; |
| 58 | + |
| 59 | + /** Constructor |
| 60 | + @param p A pointer to the stream implementation. |
| 61 | + */ |
| 62 | + any_stream( |
| 63 | + std::shared_ptr<impl> p) noexcept |
| 64 | + : impl_(std::move(p)) |
| 65 | + { |
| 66 | + } |
| 67 | + |
| 68 | + /** Read some data into the buffer. |
| 69 | +
|
| 70 | + This coroutine reads some data into the buffer |
| 71 | + and returns the number of bytes read. |
| 72 | +
|
| 73 | + @param b The buffer to read into. |
| 74 | + @return The asynchronous result. |
| 75 | + */ |
| 76 | + auto read_some( |
| 77 | + buffers::mutable_buffer b) -> |
| 78 | + async_io_result |
| 79 | + { |
| 80 | + return impl_->read_some(b); |
| 81 | + } |
| 82 | + |
| 83 | + /** Write some data from the buffer. |
| 84 | + This coroutine writes some data from the buffer |
| 85 | + and returns the number of bytes written. |
| 86 | + @param b The buffer to write from. |
| 87 | + @return The asynchronous result. |
| 88 | + */ |
| 89 | + auto write_some( |
| 90 | + buffers::const_buffer b) -> |
| 91 | + async_io_result |
| 92 | + { |
| 93 | + return impl_->write_some(b); |
| 94 | + } |
| 95 | + |
| 96 | +private: |
| 97 | + std::shared_ptr<impl> impl_; |
| 98 | +}; |
| 99 | + |
| 100 | +} // buffers |
| 101 | +} // boost |
| 102 | + |
| 103 | +#endif |
| 104 | + |
| 105 | +#endif |
0 commit comments