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 107x posix_stream_descriptor::~posix_stream_descriptor()
25 {
26 107x close();
27 107x }
28
29 107x posix_stream_descriptor::posix_stream_descriptor(capy::execution_context& ctx)
30 107x : io_object(handle(
31 ctx,
32 detail::use_backend_service<
33 detail::descriptor_service_of,
34 214x detail::descriptor_service>(ctx)))
35 {
36 107x }
37
38 std::error_code
39 115x posix_stream_descriptor::assign(native_handle_type fd) noexcept
40 {
41 115x if (is_open())
42 4x return make_error_code(error::already_open);
43 111x auto& svc = static_cast<detail::descriptor_service&>(h_.service());
44 111x return svc.assign_descriptor(get(), fd);
45 }
46
47 native_handle_type
48 6x posix_stream_descriptor::release()
49 {
50 6x if (!is_open())
51 2x detail::throw_system_error(
52 2x make_error_code(std::errc::bad_file_descriptor),
53 "posix_stream_descriptor::release");
54 4x return get().release_descriptor();
55 }
56
57 void
58 111x posix_stream_descriptor::close() noexcept
59 {
60 111x if (!is_open())
61 23x return;
62 88x h_.service().close(h_);
63 }
64
65 native_handle_type
66 10x posix_stream_descriptor::native_handle() const noexcept
67 {
68 10x if (!h_)
69 ✗ return -1;
70 10x return get().native_handle();
71 }
72
73 void
74 7x posix_stream_descriptor::cancel() noexcept
75 {
76 7x if (!is_open())
77 ✗ return;
78 7x get().cancel();
79 }
80
81 } // namespace boost::corosio
82
83 #endif // BOOST_COROSIO_POSIX
84