src/corosio/src/detail/use_backend_service.hpp

87.5% Lines (7 / 8) 100.0% Functions (8 / 8)
use_backend_service.hpp
f(x) Functions (8)
Function Calls Lines Blocks
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&) :116 250x 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&) :116 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&) :116 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&) :116 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&) :116 199x 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&) :116 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&) :116 9072x 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&) :116 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_random_access_file.hpp>
46 #include <boost/corosio/native/detail/uring/uring_stream_file.hpp>
47 #include <boost/corosio/native/detail/uring/uring_types.hpp>
48 #endif
49
50 namespace boost::corosio::detail {
51
52 // One trait per protocol so each public translation unit references
53 // only its own protocol's services. Reachability then follows the
54 // protocols a program names: a program that never names a protocol
55 // links none of that protocol's service code, for any backend.
56
57 template<class Tag>
58 struct tcp_service_of
59 {
60 using type = typename Tag::tcp_service_type;
61 };
62
63 template<class Tag>
64 struct tcp_acceptor_service_of
65 {
66 using type = typename Tag::tcp_acceptor_service_type;
67 };
68
69 template<class Tag>
70 struct udp_service_of
71 {
72 using type = typename Tag::udp_service_type;
73 };
74
75 template<class Tag>
76 struct local_stream_service_of
77 {
78 using type = typename Tag::local_stream_service_type;
79 };
80
81 template<class Tag>
82 struct local_stream_acceptor_service_of
83 {
84 using type = typename Tag::local_stream_acceptor_service_type;
85 };
86
87 template<class Tag>
88 struct local_datagram_service_of
89 {
90 using type = typename Tag::local_datagram_service_type;
91 };
92
93 template<class Tag>
94 struct stream_file_service_of
95 {
96 using type = typename Tag::stream_file_service_type;
97 };
98
99 template<class Tag>
100 struct random_access_file_service_of
101 {
102 using type = typename Tag::random_access_file_service_type;
103 };
104
105 /** Get or create the backend-specific service registered under `Base`.
106
107 The concrete service type comes from the backend tag's alias,
108 selected by probing which scheduler the context was constructed
109 with. The probe runs only on the first use of a protocol; after
110 that the service is found under its abstract key directly.
111
112 @throws std::logic_error If the context has no backend installed.
113 */
114 template<template<class> class SvcOf, class Base>
115 Base&
116 11082x use_backend_service(capy::execution_context& ctx)
117 {
118 11082x if (auto* svc = ctx.find_service<Base>())
119 9416x return *svc;
120 #if BOOST_COROSIO_HAS_IOCP
121 if (ctx.find_service<win_scheduler>())
122 return ctx.use_service<typename SvcOf<iocp_t>::type>();
123 #endif
124 #if BOOST_COROSIO_HAS_EPOLL
125 1666x if (ctx.find_service<epoll_scheduler>())
126 925x return ctx.use_service<typename SvcOf<epoll_t>::type>();
127 #endif
128 #if BOOST_COROSIO_HAS_URING
129 if (ctx.find_service<uring_scheduler>())
130 return ctx.use_service<typename SvcOf<uring_t>::type>();
131 #endif
132 #if BOOST_COROSIO_HAS_KQUEUE
133 if (ctx.find_service<kqueue_scheduler>())
134 return ctx.use_service<typename SvcOf<kqueue_t>::type>();
135 #endif
136 #if BOOST_COROSIO_HAS_SELECT
137 741x if (ctx.find_service<select_scheduler>())
138 741x return ctx.use_service<typename SvcOf<select_t>::type>();
139 #endif
140 ✗ throw_logic_error("service not installed");
141 }
142
143 } // namespace boost::corosio::detail
144
145 #endif
146