include/boost/corosio/native/native_random_access_file.hpp
93.8% Lines (30 / 32)
50.0% Branches (8 / 16)
100.0% Functions (26 / 26)
Functions (26)
Function
Calls
Lines
Branches
Blocks
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::~native_random_access_file()
:69
16x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::~native_random_access_file()
:69
16x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::get_impl()
:75
5x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::get_impl()
:75
5x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::native_read_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>&&)
:81
8x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::~native_read_at_awaitable()
:81
16x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::native_read_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>&&)
:81
8x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::~native_read_at_awaitable()
:81
16x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::native_read_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>&, unsigned long long, boost::capy::mutable_buffer)
:88
8x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::native_read_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::select_t{}>&, unsigned long long, boost::capy::mutable_buffer)
:88
8x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::dispatch(boost::capy::continuation&, boost::capy::executor_ref) const
:99
3x
100.0%
–
50.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_read_at_awaitable<boost::capy::mutable_buffer>::dispatch(boost::capy::continuation&, boost::capy::executor_ref) const
:99
3x
80.0%
50.0%
50.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::native_write_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_write_at_awaitable<boost::capy::const_buffer>&&)
:108
6x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::~native_write_at_awaitable()
:108
12x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::native_write_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_write_at_awaitable<boost::capy::const_buffer>&&)
:108
6x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::~native_write_at_awaitable()
:108
12x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::native_write_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>&, unsigned long long, boost::capy::const_buffer)
:115
6x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::native_write_at_awaitable(boost::corosio::native_random_access_file<boost::corosio::select_t{}>&, unsigned long long, boost::capy::const_buffer)
:115
6x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::dispatch(boost::capy::continuation&, boost::capy::executor_ref) const
:126
2x
100.0%
–
50.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_write_at_awaitable<boost::capy::const_buffer>::dispatch(boost::capy::continuation&, boost::capy::executor_ref) const
:126
2x
80.0%
50.0%
50.0%
boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::native_random_access_file(boost::capy::execution_context&)
:139
16x
100.0%
–
100.0%
boost::corosio::native_random_access_file<boost::corosio::select_t{}>::native_random_access_file(boost::capy::execution_context&)
:139
16x
100.0%
–
100.0%
auto boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::read_some_at<boost::capy::mutable_buffer>(unsigned long long, boost::capy::mutable_buffer const&)
:182
4x
100.0%
–
100.0%
auto boost::corosio::native_random_access_file<boost::corosio::select_t{}>::read_some_at<boost::capy::mutable_buffer>(unsigned long long, boost::capy::mutable_buffer const&)
:182
4x
100.0%
–
100.0%
auto boost::corosio::native_random_access_file<boost::corosio::kqueue_t{}>::write_some_at<boost::capy::const_buffer>(unsigned long long, boost::capy::const_buffer const&)
:198
3x
100.0%
–
100.0%
auto boost::corosio::native_random_access_file<boost::corosio::select_t{}>::write_some_at<boost::capy::const_buffer>(unsigned long long, boost::capy::const_buffer const&)
:198
3x
100.0%
–
100.0%
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2026 Steve Gerbino | |||
| 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/corosio | |||
| 9 | // | |||
| 10 | ||||
| 11 | #ifndef BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP | |||
| 12 | #define BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP | |||
| 13 | ||||
| 14 | #include <boost/corosio/random_access_file.hpp> | |||
| 15 | #include <boost/corosio/backend.hpp> | |||
| 16 | #include <boost/corosio/detail/op_base.hpp> | |||
| 17 | ||||
| 18 | #ifndef BOOST_COROSIO_MRDOCS | |||
| 19 | #if BOOST_COROSIO_HAS_EPOLL || BOOST_COROSIO_HAS_SELECT || \ | |||
| 20 | BOOST_COROSIO_HAS_KQUEUE | |||
| 21 | #include <boost/corosio/native/detail/posix/posix_random_access_file_service.hpp> | |||
| 22 | #endif | |||
| 23 | ||||
| 24 | #if BOOST_COROSIO_HAS_URING | |||
| 25 | #include <boost/corosio/native/detail/uring/uring_random_access_file.hpp> | |||
| 26 | #endif | |||
| 27 | ||||
| 28 | #if BOOST_COROSIO_HAS_IOCP | |||
| 29 | #include <boost/corosio/native/detail/iocp/win_random_access_file_service.hpp> | |||
| 30 | #endif | |||
| 31 | #endif // !BOOST_COROSIO_MRDOCS | |||
| 32 | ||||
| 33 | namespace boost::corosio { | |||
| 34 | ||||
| 35 | /** Reads and writes a file at arbitrary offsets, calling the backend directly. | |||
| 36 | ||||
| 37 | This class template inherits from @ref random_access_file. It | |||
| 38 | shadows `read_some_at` / `write_some_at` with versions that call the | |||
| 39 | backend implementation directly. The compiler can then inline | |||
| 40 | through the entire call chain. | |||
| 41 | ||||
| 42 | Non-async operations (`open`, `close`, `size`, `resize`, | |||
| 43 | `sync_data`, `sync_all`) remain unchanged and dispatch through | |||
| 44 | the compiled library. | |||
| 45 | ||||
| 46 | A `native_random_access_file` IS-A `random_access_file` and | |||
| 47 | can be passed to any function expecting `random_access_file&`, | |||
| 48 | in which case virtual dispatch is used transparently. | |||
| 49 | ||||
| 50 | @note On POSIX platforms, file I/O is dispatched to a thread pool | |||
| 51 | regardless of the chosen reactor backend. All three reactor tags | |||
| 52 | (`epoll`, `select`, `kqueue`) therefore resolve to the same | |||
| 53 | underlying implementation. The `Backend` template parameter | |||
| 54 | exists for API symmetry with @ref native_tcp_socket and friends. | |||
| 55 | The vtable savings are smaller relative to the thread-pool / | |||
| 56 | overlapped-I/O cost than they are for socket operations. | |||
| 57 | ||||
| 58 | @tparam Backend A backend tag value (e.g., `epoll`, `iocp`). | |||
| 59 | ||||
| 60 | @par Thread Safety | |||
| 61 | Same as @ref random_access_file. | |||
| 62 | ||||
| 63 | @par Example | |||
| 64 | @par !example native_random_access_file | |||
| 65 | ||||
| 66 | @see @ref random_access_file, @ref epoll_t, @ref iocp_t | |||
| 67 | */ | |||
| 68 | template<auto Backend> | |||
| 69 | class native_random_access_file : public random_access_file | |||
| 70 | { | |||
| 71 | using backend_type = decltype(Backend); | |||
| 72 | using impl_type = typename backend_type::random_access_file_type; | |||
| 73 | using service_type = typename backend_type::random_access_file_service_type; | |||
| 74 | ||||
| 75 | 10x | impl_type& get_impl() noexcept | ||
| 76 | { | |||
| 77 | 10x | return *static_cast<impl_type*>(h_.get()); | ||
| 78 | } | |||
| 79 | ||||
| 80 | template<class MutableBufferSequence> | |||
| 81 | struct native_read_at_awaitable | |||
| 82 | : detail::bytes_op_base<native_read_at_awaitable<MutableBufferSequence>> | |||
| 83 | { | |||
| 84 | native_random_access_file& self_; | |||
| 85 | std::uint64_t offset_; | |||
| 86 | MutableBufferSequence buffers_; | |||
| 87 | ||||
| 88 | 16x | native_read_at_awaitable( | ||
| 89 | native_random_access_file& self, | |||
| 90 | std::uint64_t offset, | |||
| 91 | MutableBufferSequence buffers) noexcept | |||
| 92 | 8x | : self_(self) | ||
| 93 | 8x | , offset_(offset) | ||
| 94 | 8x | , buffers_(std::move(buffers)) | ||
| 95 | 8x | { | ||
| 96 | 16x | } | ||
| 97 | ||||
| 98 | std::coroutine_handle<> | |||
| 99 | 6x | dispatch(capy::continuation& cont, capy::executor_ref ex) const | ||
| 100 | { | |||
| 101 |
4/8✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
|
12x | return self_.get_impl().read_some_at( | |
| 102 | 6x | offset_, cont, ex, buffers_, this->token_, &this->ec_, | ||
| 103 | 6x | &this->bytes_); | ||
| 104 | ✗ | } | ||
| 105 | }; | |||
| 106 | ||||
| 107 | template<class ConstBufferSequence> | |||
| 108 | struct native_write_at_awaitable | |||
| 109 | : detail::bytes_op_base<native_write_at_awaitable<ConstBufferSequence>> | |||
| 110 | { | |||
| 111 | native_random_access_file& self_; | |||
| 112 | std::uint64_t offset_; | |||
| 113 | ConstBufferSequence buffers_; | |||
| 114 | ||||
| 115 | 12x | native_write_at_awaitable( | ||
| 116 | native_random_access_file& self, | |||
| 117 | std::uint64_t offset, | |||
| 118 | ConstBufferSequence buffers) noexcept | |||
| 119 | 6x | : self_(self) | ||
| 120 | 6x | , offset_(offset) | ||
| 121 | 6x | , buffers_(std::move(buffers)) | ||
| 122 | 6x | { | ||
| 123 | 12x | } | ||
| 124 | ||||
| 125 | std::coroutine_handle<> | |||
| 126 | 4x | dispatch(capy::continuation& cont, capy::executor_ref ex) const | ||
| 127 | { | |||
| 128 |
4/8✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
|
8x | return self_.get_impl().write_some_at( | |
| 129 | 4x | offset_, cont, ex, buffers_, this->token_, &this->ec_, | ||
| 130 | 4x | &this->bytes_); | ||
| 131 | ✗ | } | ||
| 132 | }; | |||
| 133 | ||||
| 134 | public: | |||
| 135 | /** Construct a native random-access file from an execution context. | |||
| 136 | ||||
| 137 | @param ctx The execution context that owns this file. | |||
| 138 | */ | |||
| 139 | 32x | explicit native_random_access_file(capy::execution_context& ctx) | ||
| 140 | 16x | : random_access_file(handle(ctx, ctx.use_service<service_type>())) | ||
| 141 | 32x | { | ||
| 142 | 32x | } | ||
| 143 | ||||
| 144 | /** Construct a native random-access file from an executor. | |||
| 145 | ||||
| 146 | @param ex The executor whose context owns this file. | |||
| 147 | */ | |||
| 148 | template<class Ex> | |||
| 149 | requires(!std::same_as< | |||
| 150 | std::remove_cvref_t<Ex>, | |||
| 151 | native_random_access_file>) && | |||
| 152 | capy::Executor<Ex> | |||
| 153 | explicit native_random_access_file(Ex const& ex) | |||
| 154 | : native_random_access_file(ex.context()) | |||
| 155 | { | |||
| 156 | } | |||
| 157 | ||||
| 158 | /// Move construct. | |||
| 159 | native_random_access_file(native_random_access_file&&) noexcept = default; | |||
| 160 | ||||
| 161 | /// Move assign. | |||
| 162 | native_random_access_file& | |||
| 163 | operator=(native_random_access_file&&) noexcept = default; | |||
| 164 | ||||
| 165 | /// Copy construction is disabled; the handle is uniquely owned. | |||
| 166 | native_random_access_file(native_random_access_file const&) = delete; | |||
| 167 | /// Copy assignment is disabled; the handle is uniquely owned. | |||
| 168 | native_random_access_file& | |||
| 169 | operator=(native_random_access_file const&) = delete; | |||
| 170 | ||||
| 171 | /** Asynchronously read at the given offset. | |||
| 172 | ||||
| 173 | Calls the backend implementation directly, bypassing virtual | |||
| 174 | dispatch. Otherwise identical to @ref random_access_file::read_some_at. | |||
| 175 | ||||
| 176 | @param offset The byte offset to read at. | |||
| 177 | @param buffers The buffers to read into. | |||
| 178 | ||||
| 179 | @return An awaitable yielding the error code and the byte count read. | |||
| 180 | */ | |||
| 181 | template<capy::MutableBufferSequence MB> | |||
| 182 | 8x | [[nodiscard]] auto read_some_at(std::uint64_t offset, MB const& buffers) | ||
| 183 | { | |||
| 184 | 8x | return native_read_at_awaitable<MB>(*this, offset, buffers); | ||
| 185 | } | |||
| 186 | ||||
| 187 | /** Asynchronously write at the given offset. | |||
| 188 | ||||
| 189 | Calls the backend implementation directly, bypassing virtual | |||
| 190 | dispatch. Otherwise identical to @ref random_access_file::write_some_at. | |||
| 191 | ||||
| 192 | @param offset The byte offset to write at. | |||
| 193 | @param buffers The buffer data to write. | |||
| 194 | ||||
| 195 | @return An awaitable yielding the error code and the byte count written. | |||
| 196 | */ | |||
| 197 | template<capy::ConstBufferSequence CB> | |||
| 198 | 6x | [[nodiscard]] auto write_some_at(std::uint64_t offset, CB const& buffers) | ||
| 199 | { | |||
| 200 | 6x | return native_write_at_awaitable<CB>(*this, offset, buffers); | ||
| 201 | } | |||
| 202 | }; | |||
| 203 | ||||
| 204 | } // namespace boost::corosio | |||
| 205 | ||||
| 206 | #endif // BOOST_COROSIO_NATIVE_NATIVE_RANDOM_ACCESS_FILE_HPP | |||
| 207 |