src/corosio/src/stream_file.cpp

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