src/corosio/src/stream_file.cpp
95.2% Lines (99/0/104)
100.0% List of functions (20/0/20)
Functions (20)
Function
Calls
Lines
Blocks
boost::corosio::stream_file::~stream_file()
:23
85x
100.0%
100.0%
boost::corosio::stream_file::stream_file(boost::capy::execution_context&)
:28
73x
100.0%
100.0%
boost::corosio::stream_file::open(std::filesystem::__cxx11::path const&, boost::corosio::file_base::flags)
:38
67x
100.0%
100.0%
boost::corosio::stream_file::open(std::filesystem::__cxx11::path const&, boost::corosio::file_base::flags, std::error_code&)
:49
67x
100.0%
100.0%
boost::corosio::stream_file::close()
:62
99x
100.0%
100.0%
boost::corosio::stream_file::cancel()
:70
4x
100.0%
100.0%
boost::corosio::stream_file::native_handle() const
:78
4x
100.0%
100.0%
boost::corosio::stream_file::size() const
:92
10x
83.3%
88.0%
boost::corosio::stream_file::size(std::error_code&) const
:104
10x
100.0%
100.0%
boost::corosio::stream_file::resize(unsigned long)
:113
7x
83.3%
86.0%
boost::corosio::stream_file::resize(unsigned long, std::error_code&)
:124
7x
100.0%
100.0%
boost::corosio::stream_file::sync_data()
:133
5x
83.3%
86.0%
boost::corosio::stream_file::sync_data(std::error_code&)
:144
5x
100.0%
100.0%
boost::corosio::stream_file::sync_all()
:152
5x
83.3%
86.0%
boost::corosio::stream_file::sync_all(std::error_code&)
:163
5x
100.0%
100.0%
boost::corosio::stream_file::release()
:172
4x
100.0%
100.0%
boost::corosio::stream_file::release(std::error_code&)
:183
4x
100.0%
100.0%
boost::corosio::stream_file::assign(int)
:192
2x
80.0%
83.0%
boost::corosio::stream_file::seek(long, boost::corosio::file_base::seek_basis)
:200
21x
100.0%
100.0%
boost::corosio::stream_file::seek(long, boost::corosio::file_base::seek_basis, std::error_code&)
:212
21x
100.0%
100.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2026 Michael Vandeberg | ||
| 3 | // | ||
| 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) | ||
| 6 | // | ||
| 7 | // Official repository: https://github.com/cppalliance/corosio | ||
| 8 | // | ||
| 9 | |||
| 10 | #include <boost/corosio/stream_file.hpp> | ||
| 11 | #include <boost/corosio/detail/except.hpp> | ||
| 12 | #include <boost/corosio/detail/platform.hpp> | ||
| 13 | #include <system_error> | ||
| 14 | |||
| 15 | #if BOOST_COROSIO_HAS_IOCP | ||
| 16 | #include <boost/corosio/native/detail/iocp/win_file_service.hpp> | ||
| 17 | #else | ||
| 18 | #include <boost/corosio/detail/file_service.hpp> | ||
| 19 | #endif | ||
| 20 | |||
| 21 | namespace boost::corosio { | ||
| 22 | |||
| 23 | 85x | stream_file::~stream_file() | |
| 24 | { | ||
| 25 | 85x | close(); | |
| 26 | 85x | } | |
| 27 | |||
| 28 | 73x | stream_file::stream_file(capy::execution_context& ctx) | |
| 29 | #if BOOST_COROSIO_HAS_IOCP | ||
| 30 | : io_object(create_handle<detail::win_file_service>(ctx)) | ||
| 31 | #else | ||
| 32 | 73x | : io_object(create_handle<detail::file_service>(ctx)) | |
| 33 | #endif | ||
| 34 | { | ||
| 35 | 73x | } | |
| 36 | |||
| 37 | void | ||
| 38 | 67x | stream_file::open(std::filesystem::path const& path, file_base::flags mode) | |
| 39 | { | ||
| 40 | 67x | std::error_code ec; | |
| 41 | 67x | open(path, mode, ec); | |
| 42 | 67x | if (ec) | |
| 43 | { | ||
| 44 | 6x | detail::throw_system_error(ec, "stream_file::open"); | |
| 45 | } | ||
| 46 | 61x | } | |
| 47 | |||
| 48 | void | ||
| 49 | 67x | stream_file::open( | |
| 50 | std::filesystem::path const& path, | ||
| 51 | file_base::flags mode, | ||
| 52 | std::error_code& ec) | ||
| 53 | { | ||
| 54 | 67x | ec.clear(); | |
| 55 | 67x | if (is_open()) | |
| 56 | 2x | close(); | |
| 57 | 67x | auto& svc = static_cast<detail::file_service&>(h_.service()); | |
| 58 | 67x | ec = svc.open_file(get(), path, mode); | |
| 59 | 67x | } | |
| 60 | |||
| 61 | void | ||
| 62 | 99x | stream_file::close() | |
| 63 | { | ||
| 64 | 99x | if (!is_open()) | |
| 65 | 38x | return; | |
| 66 | 61x | h_.service().close(h_); | |
| 67 | } | ||
| 68 | |||
| 69 | void | ||
| 70 | 4x | stream_file::cancel() | |
| 71 | { | ||
| 72 | 4x | if (!is_open()) | |
| 73 | 2x | return; | |
| 74 | 2x | get().cancel(); | |
| 75 | } | ||
| 76 | |||
| 77 | native_handle_type | ||
| 78 | 4x | stream_file::native_handle() const noexcept | |
| 79 | { | ||
| 80 | 4x | if (!is_open()) | |
| 81 | { | ||
| 82 | #if BOOST_COROSIO_HAS_IOCP | ||
| 83 | return static_cast<native_handle_type>(~0ull); | ||
| 84 | #else | ||
| 85 | 2x | return -1; | |
| 86 | #endif | ||
| 87 | } | ||
| 88 | 2x | return get().native_handle(); | |
| 89 | } | ||
| 90 | |||
| 91 | std::uint64_t | ||
| 92 | 10x | stream_file::size() const | |
| 93 | { | ||
| 94 | 10x | std::error_code ec; | |
| 95 | 10x | std::uint64_t sz = size(ec); | |
| 96 | 8x | if (ec) | |
| 97 | { | ||
| 98 | ✗ | detail::throw_system_error(ec, "stream_file::size"); | |
| 99 | } | ||
| 100 | 8x | return sz; | |
| 101 | } | ||
| 102 | |||
| 103 | std::uint64_t | ||
| 104 | 10x | stream_file::size(std::error_code& ec) const | |
| 105 | { | ||
| 106 | 10x | ec.clear(); | |
| 107 | 10x | if (!is_open()) | |
| 108 | 2x | ec = make_error_code(std::errc::bad_file_descriptor); | |
| 109 | 10x | return get().size(); | |
| 110 | } | ||
| 111 | |||
| 112 | void | ||
| 113 | 7x | stream_file::resize(std::uint64_t new_size) | |
| 114 | { | ||
| 115 | 7x | std::error_code ec; | |
| 116 | 7x | resize(new_size, ec); | |
| 117 | 3x | if (ec) | |
| 118 | { | ||
| 119 | ✗ | detail::throw_system_error(ec, "stream_file::resize"); | |
| 120 | } | ||
| 121 | 3x | } | |
| 122 | |||
| 123 | void | ||
| 124 | 7x | stream_file::resize(std::uint64_t new_size, std::error_code& ec) | |
| 125 | { | ||
| 126 | 7x | ec.clear(); | |
| 127 | 7x | if (!is_open()) | |
| 128 | 2x | ec = make_error_code(std::errc::bad_file_descriptor); | |
| 129 | 7x | get().resize(new_size); | |
| 130 | 3x | } | |
| 131 | |||
| 132 | void | ||
| 133 | 5x | stream_file::sync_data() | |
| 134 | { | ||
| 135 | 5x | std::error_code ec; | |
| 136 | 5x | sync_data(ec); | |
| 137 | 3x | if (ec) | |
| 138 | { | ||
| 139 | ✗ | detail::throw_system_error(ec, "stream_file::sync_data"); | |
| 140 | } | ||
| 141 | 3x | } | |
| 142 | |||
| 143 | void | ||
| 144 | 5x | stream_file::sync_data(std::error_code& ec) | |
| 145 | { | ||
| 146 | 5x | if (!is_open()) | |
| 147 | 2x | ec = make_error_code(std::errc::bad_file_descriptor); | |
| 148 | 5x | get().sync_data(); | |
| 149 | 3x | } | |
| 150 | |||
| 151 | void | ||
| 152 | 5x | stream_file::sync_all() | |
| 153 | { | ||
| 154 | 5x | std::error_code ec; | |
| 155 | 5x | sync_all(ec); | |
| 156 | 3x | if (ec) | |
| 157 | { | ||
| 158 | ✗ | detail::throw_system_error(ec, "stream_file::sync_all"); | |
| 159 | } | ||
| 160 | 3x | } | |
| 161 | |||
| 162 | void | ||
| 163 | 5x | stream_file::sync_all(std::error_code& ec) | |
| 164 | { | ||
| 165 | 5x | ec.clear(); | |
| 166 | 5x | if (!is_open()) | |
| 167 | 2x | ec = make_error_code(std::errc::bad_file_descriptor); | |
| 168 | 5x | get().sync_all(); | |
| 169 | 3x | } | |
| 170 | |||
| 171 | native_handle_type | ||
| 172 | 4x | stream_file::release() | |
| 173 | { | ||
| 174 | 4x | std::error_code ec; | |
| 175 | 4x | native_handle_type h = release(ec); | |
| 176 | 4x | if (ec) | |
| 177 | { | ||
| 178 | 2x | detail::throw_system_error(ec, "stream_file::release"); | |
| 179 | } | ||
| 180 | 2x | return h; | |
| 181 | } | ||
| 182 | native_handle_type | ||
| 183 | 4x | stream_file::release(std::error_code& ec) | |
| 184 | { | ||
| 185 | 4x | ec.clear(); | |
| 186 | 4x | if (!is_open()) | |
| 187 | 2x | ec = make_error_code(std::errc::bad_file_descriptor); | |
| 188 | 4x | return get().release(); | |
| 189 | } | ||
| 190 | |||
| 191 | void | ||
| 192 | 2x | stream_file::assign(native_handle_type handle) | |
| 193 | { | ||
| 194 | 2x | if (is_open()) | |
| 195 | ✗ | close(); | |
| 196 | 2x | get().assign(handle); | |
| 197 | 2x | } | |
| 198 | |||
| 199 | std::uint64_t | ||
| 200 | 21x | stream_file::seek(std::int64_t offset, file_base::seek_basis origin) | |
| 201 | { | ||
| 202 | 21x | std::error_code ec; | |
| 203 | 21x | std::uint64_t pos = seek(offset, origin, ec); | |
| 204 | 15x | if (ec) | |
| 205 | { | ||
| 206 | 2x | detail::throw_system_error(ec, "stream_file::seek"); | |
| 207 | } | ||
| 208 | 13x | return pos; | |
| 209 | } | ||
| 210 | |||
| 211 | std::uint64_t | ||
| 212 | 21x | stream_file::seek( | |
| 213 | std::int64_t offset, file_base::seek_basis origin, std::error_code& ec) | ||
| 214 | { | ||
| 215 | 21x | ec.clear(); | |
| 216 | 21x | if (!is_open()) | |
| 217 | 2x | ec = make_error_code(std::errc::bad_file_descriptor); | |
| 218 | 21x | return get().seek(offset, origin); | |
| 219 | } | ||
| 220 | |||
| 221 | } // namespace boost::corosio | ||
| 222 |