src/corosio/src/detail/use_backend_service.hpp

90.0% Lines (9 / 10) 100.0% Functions (9 / 9)
use_backend_service.hpp
f(x) Functions (9)
Function Calls Lines Blocks
boost::corosio::detail::descriptor_service& boost::corosio::detail::use_backend_service<boost::corosio::detail::descriptor_service_of, boost::corosio::detail::descriptor_service>(boost::capy::execution_context&) :145 159x 90.0% 94.0% boost::corosio::detail::file_service& boost::corosio::detail::use_backend_service<boost::corosio::detail::stream_file_service_of, boost::corosio::detail::file_service>(boost::capy::execution_context&) :145 765x 90.0% 94.0% boost::corosio::detail::local_datagram_service& boost::corosio::detail::use_backend_service<boost::corosio::detail::local_datagram_service_of, boost::corosio::detail::local_datagram_service>(boost::capy::execution_context&) :145 367x 90.0% 94.0% boost::corosio::detail::local_stream_acceptor_service& boost::corosio::detail::use_backend_service<boost::corosio::detail::local_stream_acceptor_service_of, boost::corosio::detail::local_stream_acceptor_service>(boost::capy::execution_context&) :145 148x 90.0% 94.0% boost::corosio::detail::local_stream_service& boost::corosio::detail::use_backend_service<boost::corosio::detail::local_stream_service_of, boost::corosio::detail::local_stream_service>(boost::capy::execution_context&) :145 444x 90.0% 94.0% boost::corosio::detail::random_access_file_service& boost::corosio::detail::use_backend_service<boost::corosio::detail::random_access_file_service_of, boost::corosio::detail::random_access_file_service>(boost::capy::execution_context&) :145 314x 90.0% 94.0% boost::corosio::detail::tcp_acceptor_service& boost::corosio::detail::use_backend_service<boost::corosio::detail::tcp_acceptor_service_of, boost::corosio::detail::tcp_acceptor_service>(boost::capy::execution_context&) :145 2124x 90.0% 94.0% boost::corosio::detail::tcp_service& boost::corosio::detail::use_backend_service<boost::corosio::detail::tcp_service_of, boost::corosio::detail::tcp_service>(boost::capy::execution_context&) :145 17536x 90.0% 94.0% boost::corosio::detail::udp_service& boost::corosio::detail::use_backend_service<boost::corosio::detail::udp_service_of, boost::corosio::detail::udp_service>(boost::capy::execution_context&) :145 432x 90.0% 94.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
3 // Copyright (c) 2026 Michael Vandeberg
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #ifndef BOOST_COROSIO_SRC_DETAIL_USE_BACKEND_SERVICE_HPP
12 #define BOOST_COROSIO_SRC_DETAIL_USE_BACKEND_SERVICE_HPP
13
14 #include <boost/corosio/backend.hpp>
15 #include <boost/corosio/detail/except.hpp>
16 #include <boost/corosio/detail/platform.hpp>
17
18 #include <boost/capy/ex/execution_context.hpp>
19
20 // Each enabled backend's service definitions must be complete here:
21 // use_service<T> constructs T, and which T runs is decided at runtime.
22 // Only the services a translation unit actually instantiates generate
23 // code; completeness alone costs nothing at link time.
24 #if BOOST_COROSIO_HAS_IOCP
25 #include <boost/corosio/native/detail/iocp/win_file_service.hpp>
26 #include <boost/corosio/native/detail/iocp/win_local_stream_acceptor_service.hpp>
27 #include <boost/corosio/native/detail/iocp/win_object_handle_service.hpp>
28 #include <boost/corosio/native/detail/iocp/win_random_access_file_service.hpp>
29 #include <boost/corosio/native/detail/iocp/win_random_access_handle_service.hpp>
30 #include <boost/corosio/native/detail/iocp/win_scheduler.hpp>
31 #include <boost/corosio/native/detail/iocp/win_stream_handle_service.hpp>
32 #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp>
33 #include <boost/corosio/native/detail/iocp/win_udp_service.hpp>
34 #endif
35 #if BOOST_COROSIO_HAS_EPOLL
36 #include <boost/corosio/native/detail/epoll/epoll_types.hpp>
37 #endif
38 #if BOOST_COROSIO_HAS_KQUEUE
39 #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp>
40 #endif
41 #if BOOST_COROSIO_HAS_SELECT
42 #include <boost/corosio/native/detail/select/select_types.hpp>
43 #endif
44 #if BOOST_COROSIO_POSIX
45 #include <boost/corosio/native/detail/posix/posix_random_access_file_service.hpp>
46 #include <boost/corosio/native/detail/posix/posix_stream_file_service.hpp>
47 #endif
48 #if BOOST_COROSIO_HAS_URING
49 #include <boost/corosio/native/detail/uring/uring_descriptor_service.hpp>
50 #include <boost/corosio/native/detail/uring/uring_random_access_file.hpp>
51 #include <boost/corosio/native/detail/uring/uring_stream_file.hpp>
52 #include <boost/corosio/native/detail/uring/uring_types.hpp>
53 #endif
54
55 namespace boost::corosio::detail {
56
57 // One trait per protocol so each public translation unit references
58 // only its own protocol's services. Reachability then follows the
59 // protocols a program names: a program that never names a protocol
60 // links none of that protocol's service code, for any backend.
61
62 template<class Tag>
63 struct tcp_service_of
64 {
65 using type = typename Tag::tcp_service_type;
66 };
67
68 template<class Tag>
69 struct tcp_acceptor_service_of
70 {
71 using type = typename Tag::tcp_acceptor_service_type;
72 };
73
74 template<class Tag>
75 struct udp_service_of
76 {
77 using type = typename Tag::udp_service_type;
78 };
79
80 template<class Tag>
81 struct local_stream_service_of
82 {
83 using type = typename Tag::local_stream_service_type;
84 };
85
86 template<class Tag>
87 struct local_stream_acceptor_service_of
88 {
89 using type = typename Tag::local_stream_acceptor_service_type;
90 };
91
92 template<class Tag>
93 struct local_datagram_service_of
94 {
95 using type = typename Tag::local_datagram_service_type;
96 };
97
98 template<class Tag>
99 struct descriptor_service_of
100 {
101 using type = typename Tag::stream_descriptor_service_type;
102 };
103
104 template<class Tag>
105 struct stream_file_service_of
106 {
107 using type = typename Tag::stream_file_service_type;
108 };
109
110 template<class Tag>
111 struct random_access_file_service_of
112 {
113 using type = typename Tag::random_access_file_service_type;
114 };
115
116 template<class Tag>
117 struct random_access_handle_service_of
118 {
119 using type = typename Tag::random_access_handle_service_type;
120 };
121
122 template<class Tag>
123 struct object_handle_service_of
124 {
125 using type = typename Tag::object_handle_service_type;
126 };
127
128 template<class Tag>
129 struct stream_handle_service_of
130 {
131 using type = typename Tag::stream_handle_service_type;
132 };
133
134 /** Get or create the backend-specific service registered under `Base`.
135
136 The concrete service type comes from the backend tag's alias,
137 selected by probing which scheduler the context was constructed
138 with. The probe runs only on the first use of a protocol; after
139 that the service is found under its abstract key directly.
140
141 @throws std::logic_error If the context has no backend installed.
142 */
143 template<template<class> class SvcOf, class Base>
144 Base&
145 22289x use_backend_service(capy::execution_context& ctx)
146 {
147 22289x if (auto* svc = ctx.find_service<Base>())
148 18736x return *svc;
149 #if BOOST_COROSIO_HAS_IOCP
150 if (ctx.find_service<win_scheduler>())
151 return ctx.use_service<typename SvcOf<iocp_t>::type>();
152 #endif
153 #if BOOST_COROSIO_HAS_EPOLL
154 3553x if (ctx.find_service<epoll_scheduler>())
155 1856x return ctx.use_service<typename SvcOf<epoll_t>::type>();
156 #endif
157 #if BOOST_COROSIO_HAS_URING
158 1697x if (ctx.find_service<uring_scheduler>())
159 674x return ctx.use_service<typename SvcOf<uring_t>::type>();
160 #endif
161 #if BOOST_COROSIO_HAS_KQUEUE
162 if (ctx.find_service<kqueue_scheduler>())
163 return ctx.use_service<typename SvcOf<kqueue_t>::type>();
164 #endif
165 #if BOOST_COROSIO_HAS_SELECT
166 1023x if (ctx.find_service<select_scheduler>())
167 1023x return ctx.use_service<typename SvcOf<select_t>::type>();
168 #endif
169 ✗ throw_logic_error("service not installed");
170 }
171
172 } // namespace boost::corosio::detail
173
174 #endif
175