src/corosio/src/detail/use_backend_service.hpp

87.5% Lines (7 / 8) 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&) :123 47x 87.5% 92.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&) :123 256x 87.5% 92.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&) :123 241x 87.5% 92.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&) :123 94x 87.5% 92.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&) :123 286x 87.5% 92.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&) :123 205x 87.5% 92.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&) :123 625x 87.5% 92.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&) :123 9226x 87.5% 92.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&) :123 315x 87.5% 92.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
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 #ifndef BOOST_COROSIO_SRC_DETAIL_USE_BACKEND_SERVICE_HPP
11 #define BOOST_COROSIO_SRC_DETAIL_USE_BACKEND_SERVICE_HPP
12
13 #include <boost/corosio/backend.hpp>
14 #include <boost/corosio/detail/except.hpp>
15 #include <boost/corosio/detail/platform.hpp>
16
17 #include <boost/capy/ex/execution_context.hpp>
18
19 // Each enabled backend's service definitions must be complete here:
20 // use_service<T> constructs T, and which T runs is decided at runtime.
21 // Only the services a translation unit actually instantiates generate
22 // code; completeness alone costs nothing at link time.
23 #if BOOST_COROSIO_HAS_IOCP
24 #include <boost/corosio/native/detail/iocp/win_file_service.hpp>
25 #include <boost/corosio/native/detail/iocp/win_local_stream_acceptor_service.hpp>
26 #include <boost/corosio/native/detail/iocp/win_random_access_file_service.hpp>
27 #include <boost/corosio/native/detail/iocp/win_scheduler.hpp>
28 #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp>
29 #include <boost/corosio/native/detail/iocp/win_udp_service.hpp>
30 #endif
31 #if BOOST_COROSIO_HAS_EPOLL
32 #include <boost/corosio/native/detail/epoll/epoll_types.hpp>
33 #endif
34 #if BOOST_COROSIO_HAS_KQUEUE
35 #include <boost/corosio/native/detail/kqueue/kqueue_types.hpp>
36 #endif
37 #if BOOST_COROSIO_HAS_SELECT
38 #include <boost/corosio/native/detail/select/select_types.hpp>
39 #endif
40 #if BOOST_COROSIO_POSIX
41 #include <boost/corosio/native/detail/posix/posix_random_access_file_service.hpp>
42 #include <boost/corosio/native/detail/posix/posix_stream_file_service.hpp>
43 #endif
44 #if BOOST_COROSIO_HAS_URING
45 #include <boost/corosio/native/detail/uring/uring_descriptor_service.hpp>
46 #include <boost/corosio/native/detail/uring/uring_random_access_file.hpp>
47 #include <boost/corosio/native/detail/uring/uring_stream_file.hpp>
48 #include <boost/corosio/native/detail/uring/uring_types.hpp>
49 #endif
50
51 namespace boost::corosio::detail {
52
53 // One trait per protocol so each public translation unit references
54 // only its own protocol's services. Reachability then follows the
55 // protocols a program names: a program that never names a protocol
56 // links none of that protocol's service code, for any backend.
57
58 template<class Tag>
59 struct tcp_service_of
60 {
61 using type = typename Tag::tcp_service_type;
62 };
63
64 template<class Tag>
65 struct tcp_acceptor_service_of
66 {
67 using type = typename Tag::tcp_acceptor_service_type;
68 };
69
70 template<class Tag>
71 struct udp_service_of
72 {
73 using type = typename Tag::udp_service_type;
74 };
75
76 template<class Tag>
77 struct local_stream_service_of
78 {
79 using type = typename Tag::local_stream_service_type;
80 };
81
82 template<class Tag>
83 struct local_stream_acceptor_service_of
84 {
85 using type = typename Tag::local_stream_acceptor_service_type;
86 };
87
88 template<class Tag>
89 struct local_datagram_service_of
90 {
91 using type = typename Tag::local_datagram_service_type;
92 };
93
94 template<class Tag>
95 struct descriptor_service_of
96 {
97 using type = typename Tag::descriptor_service_type;
98 };
99
100 template<class Tag>
101 struct stream_file_service_of
102 {
103 using type = typename Tag::stream_file_service_type;
104 };
105
106 template<class Tag>
107 struct random_access_file_service_of
108 {
109 using type = typename Tag::random_access_file_service_type;
110 };
111
112 /** Get or create the backend-specific service registered under `Base`.
113
114 The concrete service type comes from the backend tag's alias,
115 selected by probing which scheduler the context was constructed
116 with. The probe runs only on the first use of a protocol; after
117 that the service is found under its abstract key directly.
118
119 @throws std::logic_error If the context has no backend installed.
120 */
121 template<template<class> class SvcOf, class Base>
122 Base&
123 11295x use_backend_service(capy::execution_context& ctx)
124 {
125 11295x if (auto* svc = ctx.find_service<Base>())
126 9572x return *svc;
127 #if BOOST_COROSIO_HAS_IOCP
128 if (ctx.find_service<win_scheduler>())
129 return ctx.use_service<typename SvcOf<iocp_t>::type>();
130 #endif
131 #if BOOST_COROSIO_HAS_EPOLL
132 1723x if (ctx.find_service<epoll_scheduler>())
133 954x return ctx.use_service<typename SvcOf<epoll_t>::type>();
134 #endif
135 #if BOOST_COROSIO_HAS_URING
136 if (ctx.find_service<uring_scheduler>())
137 return ctx.use_service<typename SvcOf<uring_t>::type>();
138 #endif
139 #if BOOST_COROSIO_HAS_KQUEUE
140 if (ctx.find_service<kqueue_scheduler>())
141 return ctx.use_service<typename SvcOf<kqueue_t>::type>();
142 #endif
143 #if BOOST_COROSIO_HAS_SELECT
144 769x if (ctx.find_service<select_scheduler>())
145 769x return ctx.use_service<typename SvcOf<select_t>::type>();
146 #endif
147 ✗ throw_logic_error("service not installed");
148 }
149
150 } // namespace boost::corosio::detail
151
152 #endif
153