| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
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)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/cppalliance/capy
|
7 |
|
// Official repository: https://github.com/cppalliance/capy
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_CAPY_IO_ANY_READ_STREAM_HPP
|
10 |
|
#ifndef BOOST_CAPY_IO_ANY_READ_STREAM_HPP
|
| 11 |
|
#define BOOST_CAPY_IO_ANY_READ_STREAM_HPP
|
11 |
|
#define BOOST_CAPY_IO_ANY_READ_STREAM_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/capy/detail/config.hpp>
|
13 |
|
#include <boost/capy/detail/config.hpp>
|
| 14 |
|
#include <boost/capy/detail/await_suspend_helper.hpp>
|
14 |
|
#include <boost/capy/detail/await_suspend_helper.hpp>
|
| 15 |
|
#include <boost/capy/buffers.hpp>
|
15 |
|
#include <boost/capy/buffers.hpp>
|
| 16 |
|
#include <boost/capy/buffers/buffer_param.hpp>
|
16 |
|
#include <boost/capy/buffers/buffer_param.hpp>
|
| 17 |
|
#include <boost/capy/concept/io_awaitable.hpp>
|
17 |
|
#include <boost/capy/concept/io_awaitable.hpp>
|
| 18 |
|
#include <boost/capy/concept/read_stream.hpp>
|
18 |
|
#include <boost/capy/concept/read_stream.hpp>
|
| 19 |
|
#include <boost/capy/coro.hpp>
|
19 |
|
#include <boost/capy/coro.hpp>
|
| 20 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
20 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
| 21 |
|
#include <boost/capy/io_result.hpp>
|
21 |
|
#include <boost/capy/io_result.hpp>
|
| 22 |
|
|
22 |
|
|
| 23 |
|
|
23 |
|
|
| 24 |
|
|
24 |
|
|
| 25 |
|
|
25 |
|
|
| 26 |
|
|
26 |
|
|
| 27 |
|
|
27 |
|
|
| 28 |
|
|
28 |
|
|
| 29 |
|
|
29 |
|
|
| 30 |
|
|
30 |
|
|
| 31 |
|
|
31 |
|
|
| 32 |
|
|
32 |
|
|
| 33 |
|
|
33 |
|
|
| 34 |
|
|
34 |
|
|
| 35 |
|
/** Type-erased wrapper for any ReadStream.
|
35 |
|
/** Type-erased wrapper for any ReadStream.
|
| 36 |
|
|
36 |
|
|
| 37 |
|
This class provides type erasure for any type satisfying the
|
37 |
|
This class provides type erasure for any type satisfying the
|
| 38 |
|
@ref ReadStream concept, enabling runtime polymorphism for
|
38 |
|
@ref ReadStream concept, enabling runtime polymorphism for
|
| 39 |
|
read operations. It uses cached awaitable storage to achieve
|
39 |
|
read operations. It uses cached awaitable storage to achieve
|
| 40 |
|
zero steady-state allocation after construction.
|
40 |
|
zero steady-state allocation after construction.
|
| 41 |
|
|
41 |
|
|
| 42 |
|
The wrapper supports two construction modes:
|
42 |
|
The wrapper supports two construction modes:
|
| 43 |
|
- **Owning**: Pass by value to transfer ownership. The wrapper
|
43 |
|
- **Owning**: Pass by value to transfer ownership. The wrapper
|
| 44 |
|
allocates storage and owns the stream.
|
44 |
|
allocates storage and owns the stream.
|
| 45 |
|
- **Reference**: Pass a pointer to wrap without ownership. The
|
45 |
|
- **Reference**: Pass a pointer to wrap without ownership. The
|
| 46 |
|
pointed-to stream must outlive this wrapper.
|
46 |
|
pointed-to stream must outlive this wrapper.
|
| 47 |
|
|
47 |
|
|
| 48 |
|
@par Awaitable Preallocation
|
48 |
|
@par Awaitable Preallocation
|
| 49 |
|
The constructor preallocates storage for the type-erased awaitable.
|
49 |
|
The constructor preallocates storage for the type-erased awaitable.
|
| 50 |
|
This reserves all virtual address space at server startup
|
50 |
|
This reserves all virtual address space at server startup
|
| 51 |
|
so memory usage can be measured up front, rather than
|
51 |
|
so memory usage can be measured up front, rather than
|
| 52 |
|
allocating piecemeal as traffic arrives.
|
52 |
|
allocating piecemeal as traffic arrives.
|
| 53 |
|
|
53 |
|
|
| 54 |
|
|
54 |
|
|
| 55 |
|
Not thread-safe. Concurrent operations on the same wrapper
|
55 |
|
Not thread-safe. Concurrent operations on the same wrapper
|
| 56 |
|
|
56 |
|
|
| 57 |
|
|
57 |
|
|
| 58 |
|
|
58 |
|
|
| 59 |
|
|
59 |
|
|
| 60 |
|
// Owning - takes ownership of the stream
|
60 |
|
// Owning - takes ownership of the stream
|
| 61 |
|
any_read_stream stream(socket{ioc});
|
61 |
|
any_read_stream stream(socket{ioc});
|
| 62 |
|
|
62 |
|
|
| 63 |
|
// Reference - wraps without ownership
|
63 |
|
// Reference - wraps without ownership
|
| 64 |
|
|
64 |
|
|
| 65 |
|
any_read_stream stream(&sock);
|
65 |
|
any_read_stream stream(&sock);
|
| 66 |
|
|
66 |
|
|
| 67 |
|
mutable_buffer buf(data, size);
|
67 |
|
mutable_buffer buf(data, size);
|
| 68 |
|
auto [ec, n] = co_await stream.read_some(std::span(&buf, 1));
|
68 |
|
auto [ec, n] = co_await stream.read_some(std::span(&buf, 1));
|
| 69 |
|
|
69 |
|
|
| 70 |
|
|
70 |
|
|
| 71 |
|
@see any_write_stream, any_stream, ReadStream
|
71 |
|
@see any_write_stream, any_stream, ReadStream
|
| 72 |
|
|
72 |
|
|
| 73 |
|
|
73 |
|
|
| 74 |
|
|
74 |
|
|
| 75 |
|
|
75 |
|
|
| 76 |
|
|
76 |
|
|
| 77 |
|
|
77 |
|
|
| 78 |
|
|
78 |
|
|
| 79 |
|
|
79 |
|
|
| 80 |
|
|
80 |
|
|
| 81 |
|
|
81 |
|
|
| 82 |
|
vtable const* vt_ = nullptr;
|
82 |
|
vtable const* vt_ = nullptr;
|
| 83 |
|
void* cached_awaitable_ = nullptr;
|
83 |
|
void* cached_awaitable_ = nullptr;
|
| 84 |
|
void* storage_ = nullptr;
|
84 |
|
void* storage_ = nullptr;
|
| 85 |
|
awaitable_ops const* active_ops_ = nullptr;
|
85 |
|
awaitable_ops const* active_ops_ = nullptr;
|
| 86 |
|
|
86 |
|
|
| 87 |
|
|
87 |
|
|
| 88 |
|
|
88 |
|
|
| 89 |
|
|
89 |
|
|
| 90 |
|
Destroys the owned stream (if any) and releases the cached
|
90 |
|
Destroys the owned stream (if any) and releases the cached
|
| 91 |
|
|
91 |
|
|
| 92 |
|
|
92 |
|
|
| 93 |
|
|
93 |
|
|
| 94 |
|
|
94 |
|
|
| 95 |
|
|
95 |
|
|
| 96 |
|
|
96 |
|
|
| 97 |
|
Constructs an empty wrapper. Operations on a default-constructed
|
97 |
|
Constructs an empty wrapper. Operations on a default-constructed
|
| 98 |
|
wrapper result in undefined behavior.
|
98 |
|
wrapper result in undefined behavior.
|
| 99 |
|
|
99 |
|
|
| 100 |
|
any_read_stream() = default;
|
100 |
|
any_read_stream() = default;
|
| 101 |
|
|
101 |
|
|
| 102 |
|
|
102 |
|
|
| 103 |
|
|
103 |
|
|
| 104 |
|
The awaitable cache is per-instance and cannot be shared.
|
104 |
|
The awaitable cache is per-instance and cannot be shared.
|
| 105 |
|
|
105 |
|
|
| 106 |
|
any_read_stream(any_read_stream const&) = delete;
|
106 |
|
any_read_stream(any_read_stream const&) = delete;
|
| 107 |
|
any_read_stream& operator=(any_read_stream const&) = delete;
|
107 |
|
any_read_stream& operator=(any_read_stream const&) = delete;
|
| 108 |
|
|
108 |
|
|
| 109 |
|
|
109 |
|
|
| 110 |
|
|
110 |
|
|
| 111 |
|
Transfers ownership of the wrapped stream (if owned) and
|
111 |
|
Transfers ownership of the wrapped stream (if owned) and
|
| 112 |
|
cached awaitable storage from `other`. After the move, `other` is
|
112 |
|
cached awaitable storage from `other`. After the move, `other` is
|
| 113 |
|
in a default-constructed state.
|
113 |
|
in a default-constructed state.
|
| 114 |
|
|
114 |
|
|
| 115 |
|
@param other The wrapper to move from.
|
115 |
|
@param other The wrapper to move from.
|
| 116 |
|
|
116 |
|
|
| 117 |
|
any_read_stream(any_read_stream&& other) noexcept
|
117 |
|
any_read_stream(any_read_stream&& other) noexcept
|
| 118 |
|
: stream_(std::exchange(other.stream_, nullptr))
|
118 |
|
: stream_(std::exchange(other.stream_, nullptr))
|
| 119 |
|
, vt_(std::exchange(other.vt_, nullptr))
|
119 |
|
, vt_(std::exchange(other.vt_, nullptr))
|
| 120 |
|
, cached_awaitable_(std::exchange(other.cached_awaitable_, nullptr))
|
120 |
|
, cached_awaitable_(std::exchange(other.cached_awaitable_, nullptr))
|
| 121 |
|
, storage_(std::exchange(other.storage_, nullptr))
|
121 |
|
, storage_(std::exchange(other.storage_, nullptr))
|
| 122 |
|
, active_ops_(std::exchange(other.active_ops_, nullptr))
|
122 |
|
, active_ops_(std::exchange(other.active_ops_, nullptr))
|
| 123 |
|
|
123 |
|
|
| 124 |
|
|
124 |
|
|
| 125 |
|
|
125 |
|
|
| 126 |
|
/** Move assignment operator.
|
126 |
|
/** Move assignment operator.
|
| 127 |
|
|
127 |
|
|
| 128 |
|
Destroys any owned stream and releases existing resources,
|
128 |
|
Destroys any owned stream and releases existing resources,
|
| 129 |
|
then transfers ownership from `other`.
|
129 |
|
then transfers ownership from `other`.
|
| 130 |
|
|
130 |
|
|
| 131 |
|
@param other The wrapper to move from.
|
131 |
|
@param other The wrapper to move from.
|
| 132 |
|
@return Reference to this wrapper.
|
132 |
|
@return Reference to this wrapper.
|
| 133 |
|
|
133 |
|
|
| 134 |
|
|
134 |
|
|
| 135 |
|
operator=(any_read_stream&& other) noexcept;
|
135 |
|
operator=(any_read_stream&& other) noexcept;
|
| 136 |
|
|
136 |
|
|
| 137 |
|
/** Construct by taking ownership of a ReadStream.
|
137 |
|
/** Construct by taking ownership of a ReadStream.
|
| 138 |
|
|
138 |
|
|
| 139 |
|
Allocates storage and moves the stream into this wrapper.
|
139 |
|
Allocates storage and moves the stream into this wrapper.
|
| 140 |
|
The wrapper owns the stream and will destroy it.
|
140 |
|
The wrapper owns the stream and will destroy it.
|
| 141 |
|
|
141 |
|
|
| 142 |
|
@param s The stream to take ownership of.
|
142 |
|
@param s The stream to take ownership of.
|
| 143 |
|
|
143 |
|
|
| 144 |
|
|
144 |
|
|
| 145 |
|
requires (!std::same_as<std::decay_t<S>, any_read_stream>)
|
145 |
|
requires (!std::same_as<std::decay_t<S>, any_read_stream>)
|
| 146 |
|
|
146 |
|
|
| 147 |
|
|
147 |
|
|
| 148 |
|
/** Construct by wrapping a ReadStream without ownership.
|
148 |
|
/** Construct by wrapping a ReadStream without ownership.
|
| 149 |
|
|
149 |
|
|
| 150 |
|
Wraps the given stream by pointer. The stream must remain
|
150 |
|
Wraps the given stream by pointer. The stream must remain
|
| 151 |
|
valid for the lifetime of this wrapper.
|
151 |
|
valid for the lifetime of this wrapper.
|
| 152 |
|
|
152 |
|
|
| 153 |
|
@param s Pointer to the stream to wrap.
|
153 |
|
@param s Pointer to the stream to wrap.
|
| 154 |
|
|
154 |
|
|
| 155 |
|
|
155 |
|
|
| 156 |
|
|
156 |
|
|
| 157 |
|
|
157 |
|
|
| 158 |
|
/** Check if the wrapper contains a valid stream.
|
158 |
|
/** Check if the wrapper contains a valid stream.
|
| 159 |
|
|
159 |
|
|
| 160 |
|
@return `true` if wrapping a stream, `false` if default-constructed
|
160 |
|
@return `true` if wrapping a stream, `false` if default-constructed
|
| 161 |
|
|
161 |
|
|
| 162 |
|
|
162 |
|
|
| 163 |
|
|
163 |
|
|
| 164 |
|
has_value() const noexcept
|
164 |
|
has_value() const noexcept
|
| 165 |
|
|
165 |
|
|
| 166 |
|
return stream_ != nullptr;
|
166 |
|
return stream_ != nullptr;
|
| 167 |
|
|
167 |
|
|
| 168 |
|
|
168 |
|
|
| 169 |
|
/** Check if the wrapper contains a valid stream.
|
169 |
|
/** Check if the wrapper contains a valid stream.
|
| 170 |
|
|
170 |
|
|
| 171 |
|
@return `true` if wrapping a stream, `false` if default-constructed
|
171 |
|
@return `true` if wrapping a stream, `false` if default-constructed
|
| 172 |
|
|
172 |
|
|
| 173 |
|
|
173 |
|
|
| 174 |
|
|
174 |
|
|
| 175 |
|
operator bool() const noexcept
|
175 |
|
operator bool() const noexcept
|
| 176 |
|
|
176 |
|
|
| 177 |
|
|
177 |
|
|
| 178 |
|
|
178 |
|
|
| 179 |
|
|
179 |
|
|
| 180 |
|
/** Initiate an asynchronous read operation.
|
180 |
|
/** Initiate an asynchronous read operation.
|
| 181 |
|
|
181 |
|
|
| 182 |
|
Reads data into the provided buffer sequence. The operation
|
182 |
|
Reads data into the provided buffer sequence. The operation
|
| 183 |
|
completes when at least one byte has been read, or an error
|
183 |
|
completes when at least one byte has been read, or an error
|
| 184 |
|
|
184 |
|
|
| 185 |
|
|
185 |
|
|
| 186 |
|
@param buffers The buffer sequence to read into. Passed by
|
186 |
|
@param buffers The buffer sequence to read into. Passed by
|
| 187 |
|
value to ensure the sequence lives in the coroutine frame
|
187 |
|
value to ensure the sequence lives in the coroutine frame
|
| 188 |
|
across suspension points.
|
188 |
|
across suspension points.
|
| 189 |
|
|
189 |
|
|
| 190 |
|
@return An awaitable yielding `(error_code,std::size_t)`.
|
190 |
|
@return An awaitable yielding `(error_code,std::size_t)`.
|
| 191 |
|
|
191 |
|
|
| 192 |
|
|
192 |
|
|
| 193 |
|
The wrapper must contain a valid stream (`has_value() == true`).
|
193 |
|
The wrapper must contain a valid stream (`has_value() == true`).
|
| 194 |
|
|
194 |
|
|
| 195 |
|
template<MutableBufferSequence MB>
|
195 |
|
template<MutableBufferSequence MB>
|
| 196 |
|
|
196 |
|
|
| 197 |
|
|
197 |
|
|
| 198 |
|
|
198 |
|
|
| 199 |
|
|
199 |
|
|
| 200 |
|
/** Rebind to a new stream after move.
|
200 |
|
/** Rebind to a new stream after move.
|
| 201 |
|
|
201 |
|
|
| 202 |
|
Updates the internal pointer to reference a new stream object.
|
202 |
|
Updates the internal pointer to reference a new stream object.
|
| 203 |
|
Used by owning wrappers after move assignment when the owned
|
203 |
|
Used by owning wrappers after move assignment when the owned
|
| 204 |
|
object has moved to a new location.
|
204 |
|
object has moved to a new location.
|
| 205 |
|
|
205 |
|
|
| 206 |
|
@param new_stream The new stream to bind to. Must be the same
|
206 |
|
@param new_stream The new stream to bind to. Must be the same
|
| 207 |
|
type as the original stream.
|
207 |
|
type as the original stream.
|
| 208 |
|
|
208 |
|
|
| 209 |
|
@note Terminates if called with a stream of different type
|
209 |
|
@note Terminates if called with a stream of different type
|
| 210 |
|
|
210 |
|
|
| 211 |
|
|
211 |
|
|
| 212 |
|
|
212 |
|
|
| 213 |
|
|
213 |
|
|
| 214 |
|
rebind(S& new_stream) noexcept
|
214 |
|
rebind(S& new_stream) noexcept
|
| 215 |
|
|
215 |
|
|
| 216 |
|
if(vt_ != &vtable_for_impl<S>::value)
|
216 |
|
if(vt_ != &vtable_for_impl<S>::value)
|
| 217 |
|
|
217 |
|
|
| 218 |
|
|
218 |
|
|
| 219 |
|
|
219 |
|
|
| 220 |
|
|
220 |
|
|
| 221 |
|
|
221 |
|
|
| 222 |
|
//----------------------------------------------------------
|
222 |
|
//----------------------------------------------------------
|
| 223 |
|
|
223 |
|
|
| 224 |
|
struct any_read_stream::awaitable_ops
|
224 |
|
struct any_read_stream::awaitable_ops
|
| 225 |
|
|
225 |
|
|
| 226 |
|
bool (*await_ready)(void*);
|
226 |
|
bool (*await_ready)(void*);
|
| 227 |
|
coro (*await_suspend)(void*, coro, executor_ref, std::stop_token);
|
227 |
|
coro (*await_suspend)(void*, coro, executor_ref, std::stop_token);
|
| 228 |
|
io_result<std::size_t> (*await_resume)(void*);
|
228 |
|
io_result<std::size_t> (*await_resume)(void*);
|
| 229 |
|
void (*destroy)(void*) noexcept;
|
229 |
|
void (*destroy)(void*) noexcept;
|
| 230 |
|
|
230 |
|
|
| 231 |
|
|
231 |
|
|
| 232 |
|
struct any_read_stream::vtable
|
232 |
|
struct any_read_stream::vtable
|
| 233 |
|
|
233 |
|
|
| 234 |
|
void (*destroy)(void*) noexcept;
|
234 |
|
void (*destroy)(void*) noexcept;
|
| 235 |
|
std::size_t awaitable_size;
|
235 |
|
std::size_t awaitable_size;
|
| 236 |
|
std::size_t awaitable_align;
|
236 |
|
std::size_t awaitable_align;
|
| 237 |
|
awaitable_ops const* (*construct_awaitable)(
|
237 |
|
awaitable_ops const* (*construct_awaitable)(
|
| 238 |
|
|
238 |
|
|
| 239 |
|
|
239 |
|
|
| 240 |
|
std::span<mutable_buffer const> buffers);
|
240 |
|
std::span<mutable_buffer const> buffers);
|
| 241 |
|
|
241 |
|
|
| 242 |
|
|
242 |
|
|
| 243 |
|
|
243 |
|
|
| 244 |
|
struct any_read_stream::vtable_for_impl
|
244 |
|
struct any_read_stream::vtable_for_impl
|
| 245 |
|
|
245 |
|
|
| 246 |
|
using Awaitable = decltype(std::declval<S&>().read_some(
|
246 |
|
using Awaitable = decltype(std::declval<S&>().read_some(
|
| 247 |
|
std::span<mutable_buffer const>{}));
|
247 |
|
std::span<mutable_buffer const>{}));
|
| 248 |
|
|
248 |
|
|
| 249 |
|
|
249 |
|
|
| 250 |
|
do_destroy_impl(void* stream) noexcept
|
250 |
|
do_destroy_impl(void* stream) noexcept
|
| 251 |
|
|
251 |
|
|
| 252 |
|
static_cast<S*>(stream)->~S();
|
252 |
|
static_cast<S*>(stream)->~S();
|
| 253 |
|
|
253 |
|
|
| 254 |
|
|
254 |
|
|
| 255 |
|
static awaitable_ops const*
|
255 |
|
static awaitable_ops const*
|
| 256 |
|
construct_awaitable_impl(
|
256 |
|
construct_awaitable_impl(
|
| 257 |
|
|
257 |
|
|
| 258 |
|
|
258 |
|
|
| 259 |
|
std::span<mutable_buffer const> buffers)
|
259 |
|
std::span<mutable_buffer const> buffers)
|
| 260 |
|
|
260 |
|
|
| 261 |
|
auto& s = *static_cast<S*>(stream);
|
261 |
|
auto& s = *static_cast<S*>(stream);
|
| 262 |
|
::new(storage) Awaitable(s.read_some(buffers));
|
262 |
|
::new(storage) Awaitable(s.read_some(buffers));
|
| 263 |
|
|
263 |
|
|
| 264 |
|
static constexpr awaitable_ops ops = {
|
264 |
|
static constexpr awaitable_ops ops = {
|
| 265 |
|
|
265 |
|
|
| 266 |
|
return static_cast<Awaitable*>(p)->await_ready();
|
266 |
|
return static_cast<Awaitable*>(p)->await_ready();
|
| 267 |
|
|
267 |
|
|
| 268 |
|
+[](void* p, coro h, executor_ref ex, std::stop_token token) {
|
268 |
|
+[](void* p, coro h, executor_ref ex, std::stop_token token) {
|
| 269 |
|
return detail::call_await_suspend(
|
269 |
|
return detail::call_await_suspend(
|
| 270 |
|
static_cast<Awaitable*>(p), h, ex, token);
|
270 |
|
static_cast<Awaitable*>(p), h, ex, token);
|
| 271 |
|
|
271 |
|
|
| 272 |
|
|
272 |
|
|
| 273 |
|
return static_cast<Awaitable*>(p)->await_resume();
|
273 |
|
return static_cast<Awaitable*>(p)->await_resume();
|
| 274 |
|
|
274 |
|
|
| 275 |
|
|
275 |
|
|
| 276 |
|
static_cast<Awaitable*>(p)->~Awaitable();
|
276 |
|
static_cast<Awaitable*>(p)->~Awaitable();
|
| 277 |
|
|
277 |
|
|
| 278 |
|
|
278 |
|
|
| 279 |
|
|
279 |
|
|
| 280 |
|
|
280 |
|
|
| 281 |
|
|
281 |
|
|
| 282 |
|
static constexpr vtable value = {
|
282 |
|
static constexpr vtable value = {
|
| 283 |
|
|
283 |
|
|
| 284 |
|
|
284 |
|
|
| 285 |
|
|
285 |
|
|
| 286 |
|
&construct_awaitable_impl
|
286 |
|
&construct_awaitable_impl
|
| 287 |
|
|
287 |
|
|
| 288 |
|
|
288 |
|
|
| 289 |
|
|
289 |
|
|
| 290 |
|
//----------------------------------------------------------
|
290 |
|
//----------------------------------------------------------
|
| 291 |
|
|
291 |
|
|
| 292 |
|
|
292 |
|
|
| 293 |
|
any_read_stream::~any_read_stream()
|
293 |
|
any_read_stream::~any_read_stream()
|
| 294 |
|
|
294 |
|
|
| 295 |
|
|
295 |
|
|
| 296 |
|
|
296 |
|
|
| 297 |
|
|
297 |
|
|
| 298 |
|
::operator delete(storage_);
|
298 |
|
::operator delete(storage_);
|
| 299 |
|
|
299 |
|
|
| 300 |
|
|
300 |
|
|
| 301 |
|
::operator delete(cached_awaitable_);
|
301 |
|
::operator delete(cached_awaitable_);
|
| 302 |
|
|
302 |
|
|
| 303 |
|
|
303 |
|
|
| 304 |
|
|
304 |
|
|
| 305 |
|
any_read_stream::operator=(any_read_stream&& other) noexcept
|
305 |
|
any_read_stream::operator=(any_read_stream&& other) noexcept
|
| 306 |
|
|
306 |
|
|
| 307 |
|
|
307 |
|
|
| 308 |
|
|
308 |
|
|
| 309 |
|
|
309 |
|
|
| 310 |
|
|
310 |
|
|
| 311 |
|
|
311 |
|
|
| 312 |
|
::operator delete(storage_);
|
312 |
|
::operator delete(storage_);
|
| 313 |
|
|
313 |
|
|
| 314 |
|
|
314 |
|
|
| 315 |
|
::operator delete(cached_awaitable_);
|
315 |
|
::operator delete(cached_awaitable_);
|
| 316 |
|
stream_ = std::exchange(other.stream_, nullptr);
|
316 |
|
stream_ = std::exchange(other.stream_, nullptr);
|
| 317 |
|
vt_ = std::exchange(other.vt_, nullptr);
|
317 |
|
vt_ = std::exchange(other.vt_, nullptr);
|
| 318 |
|
cached_awaitable_ = std::exchange(other.cached_awaitable_, nullptr);
|
318 |
|
cached_awaitable_ = std::exchange(other.cached_awaitable_, nullptr);
|
| 319 |
|
storage_ = std::exchange(other.storage_, nullptr);
|
319 |
|
storage_ = std::exchange(other.storage_, nullptr);
|
| 320 |
|
active_ops_ = std::exchange(other.active_ops_, nullptr);
|
320 |
|
active_ops_ = std::exchange(other.active_ops_, nullptr);
|
| 321 |
|
|
321 |
|
|
| 322 |
|
|
322 |
|
|
| 323 |
|
|
323 |
|
|
| 324 |
|
|
324 |
|
|
| 325 |
|
|
325 |
|
|
| 326 |
|
requires (!std::same_as<std::decay_t<S>, any_read_stream>)
|
326 |
|
requires (!std::same_as<std::decay_t<S>, any_read_stream>)
|
| 327 |
|
any_read_stream::any_read_stream(S s)
|
327 |
|
any_read_stream::any_read_stream(S s)
|
| 328 |
|
: vt_(&vtable_for_impl<S>::value)
|
328 |
|
: vt_(&vtable_for_impl<S>::value)
|
| 329 |
|
|
329 |
|
|
| 330 |
|
|
330 |
|
|
| 331 |
|
|
331 |
|
|
| 332 |
|
|
332 |
|
|
| 333 |
|
|
333 |
|
|
| 334 |
|
if(!committed && self->storage_) {
|
334 |
|
if(!committed && self->storage_) {
|
| 335 |
|
self->vt_->destroy(self->stream_);
|
335 |
|
self->vt_->destroy(self->stream_);
|
| 336 |
|
::operator delete(self->storage_);
|
336 |
|
::operator delete(self->storage_);
|
| 337 |
|
self->storage_ = nullptr;
|
337 |
|
self->storage_ = nullptr;
|
| 338 |
|
|
338 |
|
|
| 339 |
|
|
339 |
|
|
| 340 |
|
|
340 |
|
|
| 341 |
|
|
341 |
|
|
| 342 |
|
|
342 |
|
|
| 343 |
|
storage_ = ::operator new(sizeof(S));
|
343 |
|
storage_ = ::operator new(sizeof(S));
|
| 344 |
|
stream_ = ::new(storage_) S(std::move(s));
|
344 |
|
stream_ = ::new(storage_) S(std::move(s));
|
| 345 |
|
|
345 |
|
|
| 346 |
|
// Preallocate the awaitable storage
|
346 |
|
// Preallocate the awaitable storage
|
| 347 |
|
cached_awaitable_ = ::operator new(vt_->awaitable_size);
|
347 |
|
cached_awaitable_ = ::operator new(vt_->awaitable_size);
|
| 348 |
|
|
348 |
|
|
| 349 |
|
|
349 |
|
|
| 350 |
|
|
350 |
|
|
| 351 |
|
|
351 |
|
|
| 352 |
|
|
352 |
|
|
| 353 |
|
any_read_stream::any_read_stream(S* s)
|
353 |
|
any_read_stream::any_read_stream(S* s)
|
| 354 |
|
|
354 |
|
|
| 355 |
|
, vt_(&vtable_for_impl<S>::value)
|
355 |
|
, vt_(&vtable_for_impl<S>::value)
|
| 356 |
|
|
356 |
|
|
| 357 |
|
// Preallocate the awaitable storage
|
357 |
|
// Preallocate the awaitable storage
|
| 358 |
|
cached_awaitable_ = ::operator new(vt_->awaitable_size);
|
358 |
|
cached_awaitable_ = ::operator new(vt_->awaitable_size);
|
| 359 |
|
|
359 |
|
|
| 360 |
|
|
360 |
|
|
| 361 |
|
//----------------------------------------------------------
|
361 |
|
//----------------------------------------------------------
|
| 362 |
|
|
362 |
|
|
| 363 |
|
template<MutableBufferSequence MB>
|
363 |
|
template<MutableBufferSequence MB>
|
| 364 |
|
|
364 |
|
|
| 365 |
|
any_read_stream::read_some(MB buffers)
|
365 |
|
any_read_stream::read_some(MB buffers)
|
| 366 |
|
|
366 |
|
|
| 367 |
|
|
367 |
|
|
| 368 |
|
|
368 |
|
|
| 369 |
|
|
369 |
|
|
| 370 |
|
|
370 |
|
|
| 371 |
|
|
371 |
|
|
| 372 |
|
|
372 |
|
|
| 373 |
|
await_ready() const noexcept
|
373 |
|
await_ready() const noexcept
|
| 374 |
|
|
374 |
|
|
| 375 |
|
|
375 |
|
|
| 376 |
|
|
376 |
|
|
| 377 |
|
|
377 |
|
|
| 378 |
|
|
378 |
|
|
| 379 |
|
await_suspend(coro h, executor_ref ex, std::stop_token token)
|
379 |
|
await_suspend(coro h, executor_ref ex, std::stop_token token)
|
| 380 |
|
|
380 |
|
|
| 381 |
|
// Construct the underlying awaitable into cached storage
|
381 |
|
// Construct the underlying awaitable into cached storage
|
| 382 |
|
self_->active_ops_ = self_->vt_->construct_awaitable(
|
382 |
|
self_->active_ops_ = self_->vt_->construct_awaitable(
|
| 383 |
|
|
383 |
|
|
| 384 |
|
self_->cached_awaitable_,
|
384 |
|
self_->cached_awaitable_,
|
| 385 |
|
|
385 |
|
|
| 386 |
|
|
386 |
|
|
| 387 |
|
// Check if underlying is immediately ready
|
387 |
|
// Check if underlying is immediately ready
|
| 388 |
|
if(self_->active_ops_->await_ready(self_->cached_awaitable_))
|
388 |
|
if(self_->active_ops_->await_ready(self_->cached_awaitable_))
|
| 389 |
|
|
389 |
|
|
| 390 |
|
|
390 |
|
|
| 391 |
|
// Forward to underlying awaitable
|
391 |
|
// Forward to underlying awaitable
|
| 392 |
|
return self_->active_ops_->await_suspend(
|
392 |
|
return self_->active_ops_->await_suspend(
|
| 393 |
|
self_->cached_awaitable_, h, ex, token);
|
393 |
|
self_->cached_awaitable_, h, ex, token);
|
| 394 |
|
|
394 |
|
|
| 395 |
|
|
395 |
|
|
| 396 |
|
|
396 |
|
|
| 397 |
|
|
397 |
|
|
| 398 |
|
|
398 |
|
|
| 399 |
|
|
399 |
|
|
| 400 |
|
|
400 |
|
|
| 401 |
|
|
401 |
|
|
| 402 |
|
self->active_ops_->destroy(self->cached_awaitable_);
|
402 |
|
self->active_ops_->destroy(self->cached_awaitable_);
|
| 403 |
|
self->active_ops_ = nullptr;
|
403 |
|
self->active_ops_ = nullptr;
|
| 404 |
|
|
404 |
|
|
| 405 |
|
|
405 |
|
|
| 406 |
|
return self_->active_ops_->await_resume(
|
406 |
|
return self_->active_ops_->await_resume(
|
| 407 |
|
self_->cached_awaitable_);
|
407 |
|
self_->cached_awaitable_);
|
| 408 |
|
|
408 |
|
|
| 409 |
|
|
409 |
|
|
| 410 |
|
return awaitable{this, buffer_param<MB>(buffers)};
|
410 |
|
return awaitable{this, buffer_param<MB>(buffers)};
|
| 411 |
|
|
411 |
|
|
| 412 |
|
|
412 |
|
|
| 413 |
|
|
413 |
|
|
| 414 |
|
|
414 |
|
|
| 415 |
|
|
415 |
|
|
| 416 |
|
|
416 |
|
|