include/boost/capy/io/any_write_stream.hpp

100.0% Lines (123 / 123) 63.4% Functions (64 / 101)
any_write_stream.hpp
f(x) Functions (101)
Function Calls Lines Blocks
boost::capy::any_write_stream::disown() :88 208x 100.0% 100.0% boost::capy::any_write_stream::any_write_stream() :108 10x 100.0% 100.0% boost::capy::any_write_stream::any_write_stream(boost::capy::any_write_stream&&) :136 2x 100.0% 100.0% boost::capy::any_write_stream::has_value() const :192 26x 100.0% 100.0% boost::capy::any_write_stream::operator bool() const :203 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::mock_stream>::do_destroy_impl(void*) :297 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::parking_write_stream>::do_destroy_impl(void*) :297 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::pending_write_stream>::do_destroy_impl(void*) :297 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::ready_write_stream>::do_destroy_impl(void*) :297 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::throwing_move_write_stream>::do_destroy_impl(void*) :297 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::owned_parking_stream>::do_destroy_impl(void*) :297 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::do_destroy_impl(void*) :297 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::write_stream>::do_destroy_impl(void*) :297 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::mock_stream>::construct_awaitable_impl(void*, void*, std::span<boost::capy::const_buffer const, 18446744073709551615ul>) :303 10x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::parking_write_stream>::construct_awaitable_impl(void*, void*, std::span<boost::capy::const_buffer const, 18446744073709551615ul>) :303 4x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::pending_write_stream>::construct_awaitable_impl(void*, void*, std::span<boost::capy::const_buffer const, 18446744073709551615ul>) :303 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::ready_write_stream>::construct_awaitable_impl(void*, void*, std::span<boost::capy::const_buffer const, 18446744073709551615ul>) :303 1x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::throwing_move_write_stream>::construct_awaitable_impl(void*, void*, std::span<boost::capy::const_buffer const, 18446744073709551615ul>) :303 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::owned_parking_stream>::construct_awaitable_impl(void*, void*, std::span<boost::capy::const_buffer const, 18446744073709551615ul>) :303 2x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::construct_awaitable_impl(void*, void*, std::span<boost::capy::const_buffer const, 18446744073709551615ul>) :303 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::write_stream>::construct_awaitable_impl(void*, void*, std::span<boost::capy::const_buffer const, 18446744073709551615ul>) :303 63x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::mock_stream>::{lambda(void*)#10}::operator()(void*) const :314 10x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::parking_write_stream>::{lambda(void*)#16}::operator()(void*) const :314 4x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::pending_write_stream>::{lambda(void*)#13}::operator()(void*) const :314 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::ready_write_stream>::{lambda(void*)#10}::operator()(void*) const :314 1x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::throwing_move_write_stream>::{lambda(void*)#7}::operator()(void*) const :314 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::owned_parking_stream>::{lambda(void*)#16}::operator()(void*) const :314 2x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::{lambda(void*)#10}::operator()(void*) const :314 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::{lambda(void*)#13}::operator()(void*) const :314 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::write_stream>::{lambda(void*)#4}::operator()(void*) const :314 63x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::mock_stream>::{lambda(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*)#4}::operator()(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const :317 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::parking_write_stream>::{lambda(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*)#6}::operator()(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const :317 4x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::pending_write_stream>::{lambda(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*)#5}::operator()(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const :317 2x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::ready_write_stream>::{lambda(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*)#4}::operator()(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const :317 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::throwing_move_write_stream>::{lambda(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*)#3}::operator()(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const :317 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::owned_parking_stream>::{lambda(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*)#6}::operator()(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const :317 2x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::{lambda(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*)#4}::operator()(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const :317 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::{lambda(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*)#5}::operator()(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const :317 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::write_stream>::{lambda(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*)#2}::operator()(void*, std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) const :317 63x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::mock_stream>::{lambda(void*)#11}::operator()(void*) const :321 10x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::parking_write_stream>::{lambda(void*)#17}::operator()(void*) const :321 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::pending_write_stream>::{lambda(void*)#14}::operator()(void*) const :321 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::ready_write_stream>::{lambda(void*)#11}::operator()(void*) const :321 1x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::throwing_move_write_stream>::{lambda(void*)#8}::operator()(void*) const :321 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::owned_parking_stream>::{lambda(void*)#17}::operator()(void*) const :321 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::{lambda(void*)#11}::operator()(void*) const :321 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::{lambda(void*)#14}::operator()(void*) const :321 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::write_stream>::{lambda(void*)#5}::operator()(void*) const :321 63x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::mock_stream>::{lambda(void*)#12}::operator()(void*) const :324 10x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::parking_write_stream>::{lambda(void*)#18}::operator()(void*) const :324 4x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::pending_write_stream>::{lambda(void*)#15}::operator()(void*) const :324 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::ready_write_stream>::{lambda(void*)#12}::operator()(void*) const :324 1x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::(anonymous namespace)::throwing_move_write_stream>::{lambda(void*)#9}::operator()(void*) const :324 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::owned_parking_stream>::{lambda(void*)#18}::operator()(void*) const :324 2x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::{lambda(void*)#12}::operator()(void*) const :324 3x 100.0% 100.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::stream>::{lambda(void*)#15}::operator()(void*) const :324 0 0.0% 0.0% boost::capy::any_write_stream::vtable_for_impl<boost::capy::test::write_stream>::{lambda(void*)#6}::operator()(void*) const :324 63x 100.0% 100.0% boost::capy::any_write_stream::end_operation() :334 187x 100.0% 100.0% boost::capy::any_write_stream::~any_write_stream() :345 125x 100.0% 100.0% boost::capy::any_write_stream::operator=(boost::capy::any_write_stream&&) :358 19x 100.0% 100.0% boost::capy::any_write_stream::any_write_stream<boost::capy::(anonymous namespace)::throwing_move_write_stream>(boost::capy::(anonymous namespace)::throwing_move_write_stream) :382 1x 75.0% 77.0% boost::capy::any_write_stream::any_write_stream<boost::capy::test::write_stream>(boost::capy::test::write_stream) :382 3x 100.0% 80.0% boost::capy::any_write_stream::any_write_stream<boost::capy::(anonymous namespace)::mock_stream>(boost::capy::(anonymous namespace)::mock_stream*) :409 25x 100.0% 100.0% boost::capy::any_write_stream::any_write_stream<boost::capy::(anonymous namespace)::parking_write_stream>(boost::capy::(anonymous namespace)::parking_write_stream*) :409 4x 100.0% 100.0% boost::capy::any_write_stream::any_write_stream<boost::capy::(anonymous namespace)::pending_write_stream>(boost::capy::(anonymous namespace)::pending_write_stream*) :409 3x 100.0% 100.0% boost::capy::any_write_stream::any_write_stream<boost::capy::(anonymous namespace)::ready_write_stream>(boost::capy::(anonymous namespace)::ready_write_stream*) :409 1x 100.0% 100.0% boost::capy::any_write_stream::any_write_stream<boost::capy::owned_parking_stream>(boost::capy::owned_parking_stream*) :409 6x 100.0% 100.0% boost::capy::any_write_stream::any_write_stream<boost::capy::test::stream>(boost::capy::test::stream*) :409 5x 100.0% 100.0% boost::capy::any_write_stream::any_write_stream<boost::capy::test::write_stream>(boost::capy::test::write_stream*) :409 61x 100.0% 100.0% auto boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<boost::capy::const_buffer> >(boost::capy::detail::slice_of<boost::capy::const_buffer>) :419 2x 100.0% 100.0% auto boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> > >(boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> >) :419 1x 100.0% 100.0% auto boost::capy::any_write_stream::write_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer) :419 0 0.0% 0.0% auto boost::capy::any_write_stream::write_some<std::array<boost::capy::const_buffer, 3ul> >(std::array<boost::capy::const_buffer, 3ul>) :419 0 0.0% 0.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<boost::capy::const_buffer> >(boost::capy::detail::slice_of<boost::capy::const_buffer>)::awaitable::~awaitable() :431 4x 50.0% 60.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> > >(boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> >)::awaitable::~awaitable() :431 2x 50.0% 60.0% boost::capy::any_write_stream::write_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer)::awaitable::~awaitable() :431 0 0.0% 0.0% boost::capy::any_write_stream::write_some<std::array<boost::capy::const_buffer, 3ul> >(std::array<boost::capy::const_buffer, 3ul>)::awaitable::~awaitable() :431 0 0.0% 0.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<boost::capy::const_buffer> >(boost::capy::detail::slice_of<boost::capy::const_buffer>)::awaitable::awaitable(boost::capy::any_write_stream*, boost::capy::detail::slice_of<boost::capy::const_buffer> const&) :441 2x 100.0% 100.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> > >(boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> >)::awaitable::awaitable(boost::capy::any_write_stream*, boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> > const&) :441 1x 100.0% 100.0% boost::capy::any_write_stream::write_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer)::awaitable::awaitable(boost::capy::any_write_stream*, boost::capy::mutable_buffer const&) :441 0 0.0% 0.0% boost::capy::any_write_stream::write_some<std::array<boost::capy::const_buffer, 3ul> >(std::array<boost::capy::const_buffer, 3ul>)::awaitable::awaitable(boost::capy::any_write_stream*, std::array<boost::capy::const_buffer, 3ul> const&) :441 0 0.0% 0.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<boost::capy::const_buffer> >(boost::capy::detail::slice_of<boost::capy::const_buffer>)::awaitable::awaitable(awaitable&&) :449 2x 85.7% 83.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> > >(boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> >)::awaitable::awaitable(awaitable&&) :449 1x 85.7% 83.0% boost::capy::any_write_stream::write_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer)::awaitable::awaitable(awaitable&&) :449 0 0.0% 0.0% boost::capy::any_write_stream::write_some<std::array<boost::capy::const_buffer, 3ul> >(std::array<boost::capy::const_buffer, 3ul>)::awaitable::awaitable(awaitable&&) :449 0 0.0% 0.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<boost::capy::const_buffer> >(boost::capy::detail::slice_of<boost::capy::const_buffer>)::awaitable::await_ready() :459 2x 91.7% 83.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> > >(boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> >)::awaitable::await_ready() :459 1x 91.7% 83.0% boost::capy::any_write_stream::write_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer)::awaitable::await_ready() :459 0 0.0% 0.0% boost::capy::any_write_stream::write_some<std::array<boost::capy::const_buffer, 3ul> >(std::array<boost::capy::const_buffer, 3ul>)::awaitable::await_ready() :459 0 0.0% 0.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<boost::capy::const_buffer> >(boost::capy::detail::slice_of<boost::capy::const_buffer>)::awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) :478 2x 100.0% 100.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> > >(boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> >)::awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) :478 1x 100.0% 100.0% boost::capy::any_write_stream::write_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer)::awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) :478 0 0.0% 0.0% boost::capy::any_write_stream::write_some<std::array<boost::capy::const_buffer, 3ul> >(std::array<boost::capy::const_buffer, 3ul>)::awaitable::await_suspend(std::__n4861::coroutine_handle<void>, boost::capy::await_context const*) :478 0 0.0% 0.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<boost::capy::const_buffer> >(boost::capy::detail::slice_of<boost::capy::const_buffer>)::awaitable::await_resume() :485 2x 66.7% 58.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> > >(boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> >)::awaitable::await_resume() :485 1x 66.7% 58.0% boost::capy::any_write_stream::write_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer)::awaitable::await_resume() :485 0 0.0% 0.0% boost::capy::any_write_stream::write_some<std::array<boost::capy::const_buffer, 3ul> >(std::array<boost::capy::const_buffer, 3ul>)::awaitable::await_resume() :485 0 0.0% 0.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<boost::capy::const_buffer> >(boost::capy::detail::slice_of<boost::capy::const_buffer>)::awaitable::await_resume()::guard::~guard() :491 2x 100.0% 100.0% boost::capy::any_write_stream::write_some<boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> > >(boost::capy::detail::slice_of<std::array<boost::capy::const_buffer, 5ul> >)::awaitable::await_resume()::guard::~guard() :491 1x 100.0% 100.0% boost::capy::any_write_stream::write_some<boost::capy::mutable_buffer>(boost::capy::mutable_buffer)::awaitable::await_resume()::guard::~guard() :491 0 0.0% 0.0% boost::capy::any_write_stream::write_some<std::array<boost::capy::const_buffer, 3ul> >(std::array<boost::capy::const_buffer, 3ul>)::awaitable::await_resume()::guard::~guard() :491 0 0.0% 0.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2026 Michael Vandeberg
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/capy
9 //
10
11 #ifndef BOOST_CAPY_IO_ANY_WRITE_STREAM_HPP
12 #define BOOST_CAPY_IO_ANY_WRITE_STREAM_HPP
13
14 #include <boost/capy/detail/config.hpp>
15 #include <boost/capy/detail/await_suspend_helper.hpp>
16 #include <boost/capy/buffers.hpp>
17 #include <boost/capy/detail/buffer_array.hpp>
18 #include <boost/capy/concept/context_awaitable.hpp>
19 #include <boost/capy/concept/write_stream.hpp>
20 #include <coroutine>
21 #include <boost/capy/ex/await_context.hpp>
22 #include <boost/capy/results.hpp>
23
24 #include <concepts>
25 #include <coroutine>
26 #include <cstddef>
27 #include <exception>
28 #include <new>
29 #include <span>
30 #include <stop_token>
31 #include <system_error>
32 #include <utility>
33
34 namespace boost {
35 namespace capy {
36
37 /** Dispatches `write_some` through a type-erased vtable, using preallocated awaitable storage.
38
39 This class provides type erasure for any type satisfying the
40 @ref WriteStream concept, enabling runtime polymorphism for
41 write operations. It uses cached awaitable storage to achieve
42 zero steady-state allocation after construction.
43
44 The wrapper supports two construction modes:
45 - **Owning**: Pass by value to transfer ownership. The wrapper
46 allocates storage and owns the stream.
47 - **Reference**: Pass a pointer to wrap without ownership. The
48 pointed-to stream must outlive this wrapper.
49
50 @par Awaitable Preallocation
51 The constructor allocates the storage for the type-erased awaitable
52 once, so operations do not allocate it piecemeal as traffic
53 arrives.
54
55 @par Immediate Completion
56 Operations complete immediately without suspending when the
57 buffer sequence is empty, or when the underlying stream's
58 awaitable reports readiness via `await_ready`.
59
60 @par Thread Safety
61 Not thread-safe. Concurrent operations on the same wrapper
62 are undefined behavior.
63
64 @par Example
65 @par !example example
66
67
68 @see @ref any_read_stream, @ref any_stream, @ref WriteStream
69 */
70 class any_write_stream
71 {
72 struct vtable;
73
74 template<WriteStream S>
75 struct vtable_for_impl;
76
77 // ordered for cache line coherence
78 void* stream_ = nullptr;
79 vtable const* vt_ = nullptr;
80 void* cached_awaitable_ = nullptr;
81 void* storage_ = nullptr;
82 bool awaitable_active_ = false;
83 // The flag of the awaitable whose operation is active, cleared
84 // when the stream ends that operation itself.
85 bool* owner_ = nullptr;
86
87 void
88 208x disown() noexcept
89 {
90 208x if(owner_)
91 6x *std::exchange(owner_, nullptr) = false;
92 208x }
93
94 public:
95 /** Destroy the wrapper.
96
97 Ends any operation in flight, then destroys the owned stream
98 (if any) and releases the cached awaitable storage.
99 */
100 ~any_write_stream();
101
102 /** Construct a default instance.
103
104 Constructs an empty wrapper. @ref has_value and `operator bool`
105 report the empty state; calling @ref write_some before the
106 wrapper holds a stream is undefined behavior.
107 */
108 10x any_write_stream() = default;
109
110 /** Copy construction is disabled.
111
112 The awaitable cache is per-instance and cannot be shared.
113
114 @param other The wrapper that would be copied.
115 */
116 any_write_stream(any_write_stream const& other) = delete;
117
118 /** Copy assignment is disabled.
119
120 The awaitable cache is per-instance and cannot be shared.
121
122 @param other The wrapper that would be assigned from.
123
124 @return A reference to `*this`.
125 */
126 any_write_stream& operator=(any_write_stream const& other) = delete;
127
128 /** Construct by moving.
129
130 Transfers ownership of the wrapped stream (if owned) and
131 cached awaitable storage from `other`. After the move, `other` is
132 in a default-constructed state.
133
134 @param other The wrapper to move from.
135 */
136 2x any_write_stream(any_write_stream&& other) noexcept
137 2x : stream_(std::exchange(other.stream_, nullptr))
138 2x , vt_(std::exchange(other.vt_, nullptr))
139 2x , cached_awaitable_(std::exchange(other.cached_awaitable_, nullptr))
140 2x , storage_(std::exchange(other.storage_, nullptr))
141 2x , awaitable_active_(std::exchange(other.awaitable_active_, false))
142 {
143 2x other.disown();
144 2x }
145
146 /** Assign by moving.
147
148 Destroys any owned stream and releases existing resources,
149 then transfers ownership from `other`.
150
151 @param other The wrapper to move from.
152 @return Reference to this wrapper.
153 */
154 any_write_stream&
155 operator=(any_write_stream&& other) noexcept;
156
157 /** Construct by taking ownership of a WriteStream.
158
159 Allocates storage and moves the stream into this wrapper.
160 The wrapper owns the stream and destroys it.
161
162 @par Exception Safety
163 Strong guarantee. If an allocation throws, nothing is leaked.
164
165 @throws std::bad_alloc If allocation fails.
166 @throws Any exception thrown by moving `s`.
167
168 @param s The stream to take ownership of.
169 */
170 template<WriteStream S>
171 requires (!std::same_as<std::decay_t<S>, any_write_stream>)
172 any_write_stream(S s);
173
174 /** Construct by wrapping a WriteStream without ownership.
175
176 Wraps the given stream by pointer. The stream must remain
177 valid for the lifetime of this wrapper.
178
179 @throws std::bad_alloc If allocating the awaitable storage fails.
180
181 @param s Pointer to the stream to wrap.
182 */
183 template<WriteStream S>
184 any_write_stream(S* s);
185
186 /** Check if the wrapper contains a valid stream.
187
188 @return `true` if wrapping a stream, `false` if default-constructed
189 or moved-from.
190 */
191 bool
192 26x has_value() const noexcept
193 {
194 26x return stream_ != nullptr;
195 }
196
197 /** Check if the wrapper contains a valid stream.
198
199 @return `true` if wrapping a stream, `false` if default-constructed
200 or moved-from.
201 */
202 explicit
203 3x operator bool() const noexcept
204 {
205 3x return has_value();
206 }
207
208 /** Asynchronously write some data from the buffer sequence.
209
210 Writes data from the provided buffer sequence. The operation
211 completes when at least one byte is written, or an error
212 occurs.
213
214 @pre The wrapper must contain a valid stream (`has_value() == true`).
215
216 @par Immediate Completion
217 The operation completes immediately without suspending
218 the calling coroutine when:
219 @li The buffer sequence is empty, returning `{error_code{}, 0}`.
220 @li The underlying stream's awaitable reports immediate
221 readiness via `await_ready`.
222
223 @par After an Error
224 A subsequent call is permitted. The wrapper forwards directly
225 to the underlying stream, imposing no stricter rule than
226 @ref WriteStream.
227
228 @note This is a partial operation and may not process the
229 entire buffer sequence. Use the composed @ref write algorithm
230 for guaranteed complete transfer.
231
232 @param buffers The buffer sequence containing data to write.
233 Passed by value to ensure the sequence lives in the
234 coroutine frame across suspension points.
235
236 @return An awaitable that await-returns `(error_code,std::size_t)`.
237 */
238 template<ConstBufferSequence CB>
239 auto
240 write_some(CB buffers);
241
242 protected:
243 /** End the operation in flight, if any.
244
245 Destroys the wrapped stream's active operation, which may refer
246 to the stream, so an owner must call this before destroying the
247 stream. The awaitable that started the operation no longer ends
248 it.
249 */
250 void
251 end_operation() noexcept;
252
253 /** Rebind to a new stream after move.
254
255 Updates the internal pointer to reference a new stream object,
256 for use when the stream object has moved to a new location.
257
258 @param new_stream The new stream to bind to. Must be the same
259 type as the original stream.
260
261 @note Terminates if called with a stream of different type
262 than the original.
263 */
264 template<WriteStream S>
265 void
266 rebind(S& new_stream) noexcept
267 {
268 if(vt_ != &vtable_for_impl<S>::value)
269 std::terminate();
270 stream_ = &new_stream;
271 }
272 };
273
274 struct any_write_stream::vtable
275 {
276 // ordered by call frequency for cache line coherence
277 void (*construct_awaitable)(
278 void* stream,
279 void* storage,
280 std::span<const_buffer const> buffers);
281 bool (*await_ready)(void*);
282 std::coroutine_handle<> (*await_suspend)(void*, std::coroutine_handle<>, await_context const*);
283 results<std::size_t> (*await_resume)(void*);
284 void (*destroy_awaitable)(void*) noexcept;
285 std::size_t awaitable_size;
286 std::size_t awaitable_align;
287 void (*destroy)(void*) noexcept;
288 };
289
290 template<WriteStream S>
291 struct any_write_stream::vtable_for_impl
292 {
293 using Awaitable = decltype(std::declval<S&>().write_some(
294 std::span<const_buffer const>{}));
295
296 static void
297 3x do_destroy_impl(void* stream) noexcept
298 {
299 3x static_cast<S*>(stream)->~S();
300 3x }
301
302 static void
303 86x construct_awaitable_impl(
304 void* stream,
305 void* storage,
306 std::span<const_buffer const> buffers)
307 {
308 86x auto& s = *static_cast<S*>(stream);
309 86x ::new(storage) Awaitable(s.write_some(buffers));
310 86x }
311
312 static constexpr vtable value = {
313 &construct_awaitable_impl,
314 86x +[](void* p) {
315 86x return static_cast<Awaitable*>(p)->await_ready();
316 },
317 74x +[](void* p, std::coroutine_handle<> h, await_context const* env) {
318 74x return detail::call_await_suspend(
319 74x static_cast<Awaitable*>(p), h, env);
320 },
321 77x +[](void* p) {
322 77x return static_cast<Awaitable*>(p)->await_resume();
323 },
324 95x +[](void* p) noexcept {
325 12x static_cast<Awaitable*>(p)->~Awaitable();
326 },
327 sizeof(Awaitable),
328 alignof(Awaitable),
329 &do_destroy_impl
330 };
331 };
332
333 inline void
334 187x any_write_stream::end_operation() noexcept
335 {
336 187x disown();
337 187x if(awaitable_active_)
338 {
339 6x vt_->destroy_awaitable(cached_awaitable_);
340 6x awaitable_active_ = false;
341 }
342 187x }
343
344 inline
345 125x any_write_stream::~any_write_stream()
346 {
347 125x end_operation();
348 125x if(cached_awaitable_)
349 101x ::operator delete(cached_awaitable_);
350 125x if(storage_)
351 {
352 2x vt_->destroy(stream_);
353 2x ::operator delete(storage_);
354 }
355 125x }
356
357 inline any_write_stream&
358 19x any_write_stream::operator=(any_write_stream&& other) noexcept
359 {
360 19x if(this != &other)
361 {
362 19x end_operation();
363 19x other.disown();
364 19x if(cached_awaitable_)
365 7x ::operator delete(cached_awaitable_);
366 19x if(storage_)
367 {
368 1x vt_->destroy(stream_);
369 1x ::operator delete(storage_);
370 }
371 19x stream_ = std::exchange(other.stream_, nullptr);
372 19x vt_ = std::exchange(other.vt_, nullptr);
373 19x cached_awaitable_ = std::exchange(other.cached_awaitable_, nullptr);
374 19x storage_ = std::exchange(other.storage_, nullptr);
375 19x awaitable_active_ = std::exchange(other.awaitable_active_, false);
376 }
377 19x return *this;
378 }
379
380 template<WriteStream S>
381 requires (!std::same_as<std::decay_t<S>, any_write_stream>)
382 4x any_write_stream::any_write_stream(S s)
383 4x : vt_(&vtable_for_impl<S>::value)
384 {
385 struct guard {
386 any_write_stream* self;
387 bool committed = false;
388 ~guard() {
389 if(!committed && self->storage_) {
390 if(self->stream_)
391 self->vt_->destroy(self->stream_); // LCOV_EXCL_LINE OOM rollback: only when the cached-awaitable allocation throws
392 ::operator delete(self->storage_);
393 self->storage_ = nullptr;
394 self->stream_ = nullptr;
395 }
396 }
397 4x } g{this};
398
399 4x storage_ = ::operator new(sizeof(S));
400 4x stream_ = ::new(storage_) S(std::move(s));
401
402 // Preallocate the awaitable storage
403 3x cached_awaitable_ = ::operator new(vt_->awaitable_size);
404
405 3x g.committed = true;
406 4x }
407
408 template<WriteStream S>
409 105x any_write_stream::any_write_stream(S* s)
410 105x : stream_(s)
411 105x , vt_(&vtable_for_impl<S>::value)
412 {
413 // Preallocate the awaitable storage
414 105x cached_awaitable_ = ::operator new(vt_->awaitable_size);
415 105x }
416
417 template<ConstBufferSequence CB>
418 auto
419 91x any_write_stream::write_some(CB buffers)
420 {
421 struct awaitable
422 {
423 any_write_stream* self_;
424 detail::const_buffer_array<detail::max_iovec_> ba_;
425 // Set while this awaitable, not another on the same stream,
426 // holds the stream's inner awaitable.
427 bool owns_ = false;
428
429 // A chain unwound while suspended here never reaches
430 // await_resume, so the inner awaitable is destroyed here.
431 178x ~awaitable()
432 {
433 178x if(owns_)
434 {
435 3x self_->vt_->destroy_awaitable(self_->cached_awaitable_);
436 3x self_->awaitable_active_ = false;
437 3x self_->owner_ = nullptr;
438 }
439 178x }
440
441 91x awaitable(
442 any_write_stream* self,
443 CB const& buffers) noexcept
444 91x : self_(self)
445 91x , ba_(buffers)
446 {
447 91x }
448
449 87x awaitable(awaitable&& other) noexcept
450 87x : self_(std::exchange(other.self_, nullptr))
451 87x , ba_(other.ba_)
452 87x , owns_(std::exchange(other.owns_, false))
453 {
454 87x if(owns_)
455 1x self_->owner_ = &owns_;
456 87x }
457
458 bool
459 90x await_ready()
460 {
461 // An empty write never touches the underlying stream
462 90x if(ba_.to_span().empty())
463 4x return true;
464
465 86x self_->vt_->construct_awaitable(
466 86x self_->stream_,
467 86x self_->cached_awaitable_,
468 86x ba_.to_span());
469 86x self_->awaitable_active_ = true;
470 86x owns_ = true;
471 86x self_->owner_ = &owns_;
472
473 172x return self_->vt_->await_ready(
474 86x self_->cached_awaitable_);
475 }
476
477 std::coroutine_handle<>
478 74x await_suspend(std::coroutine_handle<> h, await_context const* env)
479 {
480 74x return self_->vt_->await_suspend(
481 74x self_->cached_awaitable_, h, env);
482 }
483
484 [[nodiscard]] results<std::size_t>
485 81x await_resume()
486 {
487 81x if(!owns_)
488 4x return {std::error_code(), 0};
489 struct guard {
490 awaitable* a;
491 77x ~guard() {
492 77x a->self_->vt_->destroy_awaitable(
493 77x a->self_->cached_awaitable_);
494 77x a->self_->awaitable_active_ = false;
495 77x a->self_->owner_ = nullptr;
496 77x a->owns_ = false;
497 77x }
498 77x } g{this};
499 77x return self_->vt_->await_resume(
500 77x self_->cached_awaitable_);
501 77x }
502 };
503 91x return awaitable{this, buffers};
504 }
505
506 } // namespace capy
507 } // namespace boost
508
509 #endif
510