src/corosio/src/posix_stream_descriptor.cpp

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