src/corosio/src/stream_file.cpp

100.0% Lines (53/0/53) 100.0% List of functions (13/0/13)
stream_file.cpp
f(x) Functions (13)
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
14 #if BOOST_COROSIO_HAS_IOCP
15 #include <boost/corosio/native/detail/iocp/win_file_service.hpp>
16 #else
17 #include <boost/corosio/detail/file_service.hpp>
18 #endif
19
20 namespace boost::corosio {
21
22 141x stream_file::~stream_file()
23 {
24 141x close();
25 141x }
26
27 120x stream_file::stream_file(capy::execution_context& ctx)
28 #if BOOST_COROSIO_HAS_IOCP
29 : io_object(create_handle<detail::win_file_service>(ctx))
30 #else
31 120x : io_object(create_handle<detail::file_service>(ctx))
32 #endif
33 {
34 120x }
35
36 std::error_code
37 108x stream_file::open(
38 std::filesystem::path const& path, file_base::flags mode) noexcept
39 {
40 108x if (is_open())
41 3x close();
42 108x auto& svc = static_cast<detail::file_service&>(h_.service());
43 108x return svc.open_file(get(), path, mode);
44 }
45
46 void
47 168x stream_file::close() noexcept
48 {
49 168x if (!is_open())
50 62x return;
51 106x h_.service().close(h_);
52 }
53
54 void
55 6x stream_file::cancel() noexcept
56 {
57 6x if (!is_open())
58 3x return;
59 3x get().cancel();
60 }
61
62 native_handle_type
63 6x stream_file::native_handle() const noexcept
64 {
65 6x if (!is_open())
66 {
67 #if BOOST_COROSIO_HAS_IOCP
68 return static_cast<native_handle_type>(~0ull);
69 #else
70 3x return -1;
71 #endif
72 }
73 3x return get().native_handle();
74 }
75
76 std::uint64_t
77 20x stream_file::size() const
78 {
79 20x if (!is_open())
80 3x detail::throw_system_error(
81 3x make_error_code(std::errc::bad_file_descriptor),
82 "stream_file::size");
83 17x return get().size();
84 }
85
86 std::error_code
87 13x stream_file::resize(std::uint64_t new_size) noexcept
88 {
89 13x if (!is_open())
90 3x return make_error_code(std::errc::bad_file_descriptor);
91 10x return get().resize(new_size);
92 }
93
94 std::error_code
95 10x stream_file::sync_data() noexcept
96 {
97 10x if (!is_open())
98 3x return make_error_code(std::errc::bad_file_descriptor);
99 7x return get().sync_data();
100 }
101
102 std::error_code
103 10x stream_file::sync_all() noexcept
104 {
105 10x if (!is_open())
106 3x return make_error_code(std::errc::bad_file_descriptor);
107 7x return get().sync_all();
108 }
109
110 native_handle_type
111 6x stream_file::release()
112 {
113 6x if (!is_open())
114 3x detail::throw_system_error(
115 3x make_error_code(std::errc::bad_file_descriptor),
116 "stream_file::release");
117 3x return get().release();
118 }
119
120 std::error_code
121 9x stream_file::assign(native_handle_type handle) noexcept
122 {
123 9x if (is_open())
124 3x close();
125 9x return get().assign(handle);
126 }
127
128 capy::io_result<std::uint64_t>
129 31x stream_file::seek(std::int64_t offset, file_base::seek_basis origin) noexcept
130 {
131 31x if (!is_open())
132 3x return {make_error_code(std::errc::bad_file_descriptor), 0};
133 28x return get().seek(offset, origin);
134 }
135
136 } // namespace boost::corosio
137