TLA Line data 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_STREAM_HPP
12 : #define BOOST_CAPY_IO_ANY_STREAM_HPP
13 :
14 : #include <boost/capy/detail/config.hpp>
15 : #include <boost/capy/concept/read_stream.hpp>
16 : #include <boost/capy/concept/write_stream.hpp>
17 : #include <boost/capy/io/any_read_stream.hpp>
18 : #include <boost/capy/io/any_write_stream.hpp>
19 :
20 : #include <concepts>
21 :
22 : namespace boost {
23 : namespace capy {
24 :
25 : /** Dispatches `read_some` and `write_some` through independent type-erased vtables.
26 :
27 : This class provides type erasure for any type satisfying both
28 : the @ref ReadStream and @ref WriteStream concepts, enabling
29 : runtime polymorphism for bidirectional I/O operations.
30 :
31 : Inherits from both @ref any_read_stream and @ref any_write_stream,
32 : providing `read_some` and `write_some` operations. Each base
33 : maintains its own cached awaitable storage, allowing concurrent
34 : read and write operations.
35 :
36 : The wrapper supports two construction modes:
37 : - **Owning**: Pass by value to transfer ownership. The wrapper
38 : allocates storage and owns the stream.
39 : - **Reference**: Pass a pointer to wrap without ownership. The
40 : pointed-to stream must outlive this wrapper.
41 :
42 : @par Implicit Conversion
43 : This class implicitly converts to `any_read_stream&` or
44 : `any_write_stream&`, allowing it to be passed to functions
45 : that accept only one capability. Moving from one base reference
46 : empties that base only; the wrapper is then unusable.
47 :
48 : @par Thread Safety
49 : Not thread-safe. Concurrent operations of the same type
50 : (two reads or two writes) are undefined behavior. One read
51 : and one write may be in flight simultaneously.
52 :
53 : @par Example
54 : @par !example example
55 :
56 :
57 : @see @ref any_read_stream, @ref any_write_stream, @ref ReadStream, @ref WriteStream
58 : */
59 : class any_stream
60 : : public any_read_stream
61 : , public any_write_stream
62 : {
63 : void* storage_ = nullptr;
64 : void* stream_ptr_ = nullptr;
65 : void (*destroy_)(void*) noexcept = nullptr;
66 :
67 : public:
68 : /** Destroy the wrapper.
69 :
70 : Ends any operation in flight, then destroys the owned stream
71 : (if any). Base class destructors release their cached awaitable
72 : storage.
73 : */
74 HIT 39 : ~any_stream()
75 : {
76 39 : any_read_stream::end_operation();
77 39 : any_write_stream::end_operation();
78 39 : if(storage_)
79 : {
80 7 : destroy_(stream_ptr_);
81 7 : ::operator delete(storage_);
82 : }
83 39 : }
84 :
85 : /** Construct a default instance.
86 :
87 : Constructs an empty wrapper. @ref has_value and `operator bool`
88 : report the empty state; calling `read_some` or `write_some`
89 : before the wrapper holds a stream is undefined behavior.
90 : */
91 : any_stream() = default;
92 :
93 : /** Copy construction is disabled.
94 :
95 : The awaitable caches are per-instance and cannot be shared.
96 :
97 : @param other The wrapper that would be copied.
98 : */
99 : any_stream(any_stream const& other) = delete;
100 :
101 : /** Copy assignment is disabled.
102 :
103 : The awaitable caches are per-instance and cannot be shared.
104 :
105 : @param other The wrapper that would be assigned from.
106 :
107 : @return A reference to `*this`.
108 : */
109 : any_stream& operator=(any_stream const& other) = delete;
110 :
111 : /** Construct by moving.
112 :
113 : Transfers ownership from both bases and the owned stream (if any).
114 :
115 : @param other The wrapper to move from.
116 : */
117 1 : any_stream(any_stream&& other) noexcept
118 1 : : any_read_stream(std::move(static_cast<any_read_stream&>(other)))
119 1 : , any_write_stream(std::move(static_cast<any_write_stream&>(other)))
120 1 : , storage_(std::exchange(other.storage_, nullptr))
121 1 : , stream_ptr_(std::exchange(other.stream_ptr_, nullptr))
122 2 : , destroy_(std::exchange(other.destroy_, nullptr))
123 : {
124 1 : }
125 :
126 : /** Assign by moving.
127 :
128 : Destroys any owned stream and releases existing resources,
129 : then transfers ownership from `other`.
130 :
131 : @param other The wrapper to move from.
132 : @return Reference to this wrapper.
133 : */
134 : any_stream&
135 4 : operator=(any_stream&& other) noexcept
136 : {
137 4 : if(this != &other)
138 : {
139 4 : any_read_stream::end_operation();
140 4 : any_write_stream::end_operation();
141 4 : if(storage_)
142 : {
143 3 : destroy_(stream_ptr_);
144 3 : ::operator delete(storage_);
145 : }
146 : static_cast<any_read_stream&>(*this) =
147 4 : std::move(static_cast<any_read_stream&>(other));
148 : static_cast<any_write_stream&>(*this) =
149 4 : std::move(static_cast<any_write_stream&>(other));
150 4 : storage_ = std::exchange(other.storage_, nullptr);
151 4 : stream_ptr_ = std::exchange(other.stream_ptr_, nullptr);
152 4 : destroy_ = std::exchange(other.destroy_, nullptr);
153 : }
154 4 : return *this;
155 : }
156 :
157 : /** Construct by taking ownership of a bidirectional stream.
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 `S` is nothrow move constructible; otherwise
164 : the storage may leak.
165 :
166 : @throws std::bad_alloc If allocation fails.
167 : @throws Any exception thrown by moving `s`.
168 :
169 : @param s The stream to take ownership of. Must satisfy both
170 : ReadStream and WriteStream concepts.
171 : */
172 : template<class S>
173 : requires ReadStream<S> && WriteStream<S> &&
174 : (!std::same_as<std::decay_t<S>, any_stream>)
175 10 : any_stream(S s)
176 10 : {
177 : struct guard {
178 : any_stream* self;
179 : void* ptr = nullptr;
180 : bool committed = false;
181 10 : ~guard() {
182 10 : if(!committed && ptr) {
183 : static_cast<S*>(ptr)->~S(); // LCOV_EXCL_LINE OOM rollback: only when the cached-awaitable allocation throws
184 : ::operator delete(self->storage_); // LCOV_EXCL_LINE OOM rollback: only when the cached-awaitable allocation throws
185 : self->storage_ = nullptr; // LCOV_EXCL_LINE OOM rollback: only when the cached-awaitable allocation throws
186 : }
187 10 : }
188 10 : } g{this};
189 :
190 10 : storage_ = ::operator new(sizeof(S));
191 10 : S* ptr = ::new(storage_) S(std::move(s));
192 10 : g.ptr = ptr;
193 10 : stream_ptr_ = ptr;
194 20 : destroy_ = +[](void* p) noexcept { static_cast<S*>(p)->~S(); };
195 :
196 : // Initialize bases with pointer (reference semantics)
197 10 : static_cast<any_read_stream&>(*this) = any_read_stream(ptr);
198 10 : static_cast<any_write_stream&>(*this) = any_write_stream(ptr);
199 :
200 10 : g.committed = true;
201 10 : }
202 :
203 : /** Construct by wrapping a bidirectional stream without ownership.
204 :
205 : Wraps the given stream by pointer. The stream must remain
206 : valid for the lifetime of this wrapper.
207 :
208 : @throws std::bad_alloc If allocating the awaitable storage fails.
209 :
210 : @param s Pointer to the stream to wrap. Must satisfy both
211 : ReadStream and WriteStream concepts.
212 : */
213 : template<class S>
214 : requires ReadStream<S> && WriteStream<S>
215 26 : any_stream(S* s)
216 : : any_read_stream(s)
217 26 : , any_write_stream(s)
218 : {
219 : // storage_ remains nullptr - no ownership
220 26 : }
221 :
222 : /** Check if the wrapper contains a valid stream.
223 :
224 : Both bases must be valid for the wrapper to be valid.
225 :
226 : @return `true` if wrapping a stream, `false` if default-constructed
227 : or moved-from.
228 : */
229 : bool
230 12 : has_value() const noexcept
231 : {
232 19 : return any_read_stream::has_value() &&
233 19 : any_write_stream::has_value();
234 : }
235 :
236 : /** Check if the wrapper contains a valid stream.
237 :
238 : Both bases must be valid for the wrapper to be valid.
239 :
240 : @return `true` if wrapping a stream, `false` if default-constructed
241 : or moved-from.
242 : */
243 : explicit
244 2 : operator bool() const noexcept
245 : {
246 2 : return has_value();
247 : }
248 : };
249 :
250 : } // namespace capy
251 : } // namespace boost
252 :
253 : #endif
|