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