src/corosio/src/posix_stream_descriptor.cpp

93.1% Lines (27 / 29) 100.0% Functions (7 / 7)
posix_stream_descriptor.cpp
f(x) Functions (7)
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/posix_stream_descriptor.hpp>
11 #include <boost/corosio/error.hpp>
12
13 #include <boost/corosio/detail/platform.hpp>
14
15 #if BOOST_COROSIO_POSIX
16
17 #include <boost/corosio/detail/descriptor_service.hpp>
18 #include <boost/corosio/detail/except.hpp>
19
20 #include "src/detail/use_backend_service.hpp"
21
22 namespace boost::corosio {
23
24 160x posix_stream_descriptor::~posix_stream_descriptor()
25 {
26 160x close();
27 160x }
28
29 159x posix_stream_descriptor::posix_stream_descriptor(capy::execution_context& ctx)
30 159x : io_object(handle(
31 ctx,
32 detail::use_backend_service<
33 detail::descriptor_service_of,
34 318x detail::descriptor_service>(ctx)))
35 {
36 159x }
37
38 std::error_code
39 171x posix_stream_descriptor::assign(native_handle_type fd) noexcept
40 {
41 171x if (is_open())
42 6x return make_error_code(error::already_open);
43 165x auto& svc = static_cast<detail::descriptor_service&>(h_.service());
44 165x return svc.assign_descriptor(get(), fd);
45 }
46
47 native_handle_type
48 9x posix_stream_descriptor::release()
49 {
50 9x if (!is_open())
51 3x detail::throw_system_error(
52 3x make_error_code(std::errc::bad_file_descriptor),
53 "posix_stream_descriptor::release");
54 6x return get().release_descriptor();
55 }
56
57 void
58 166x posix_stream_descriptor::close() noexcept
59 {
60 166x if (!is_open())
61 34x return;
62 132x h_.service().close(h_);
63 }
64
65 native_handle_type
66 15x posix_stream_descriptor::native_handle() const noexcept
67 {
68 15x if (!h_)
69 ✗ return -1;
70 15x return get().native_handle();
71 }
72
73 void
74 9x posix_stream_descriptor::cancel() noexcept
75 {
76 9x if (!is_open())
77 ✗ return;
78 9x get().cancel();
79 }
80
81 } // namespace boost::corosio
82
83 #endif // BOOST_COROSIO_POSIX
84