co_usb
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1
6#pragma once
7
11#include <libusb.h>
12#include <memory_resource>
13#include <system_error>
14
15namespace co_usb::detail
16{
17
22
23} // namespace co_usb::detail
24
25namespace co_usb::ev
26{
27
61struct context
62{
63 auto get_token () const noexcept
64 {
65 return m_srv_ptr->get_token();
66 }
67
69 {
70 m_srv_ptr->request_stop();
71 }
72
74 {
75 return m_srv_ptr->handler();
76 }
77
78 auto usb_context () -> libusb_context *
79 {
80 return m_srv_ptr->usb_context();
81 }
82
83 private:
84 friend auto ::co_usb::detail::raw_context(::co_usb::ev::detail::handler_service *srv);
85
86 explicit context (detail::handler_service *srv_ptr) noexcept : m_srv_ptr(srv_ptr)
87 {
88 }
89
90 detail::handler_service *m_srv_ptr;
91};
92
93} // namespace co_usb::ev
94
95namespace co_usb
96{
97
98namespace detail
99{
100
102{
103 return ev::context{srv};
104}
105
106} // namespace detail
107
118template <ev::detail::EventHandler HandlerTy, ::co_usb::detail::ErrorProtocol<ev::context> ErrorTy,
119 typename... Args>
120inline auto make_context (boost::capy::executor_ref exec, std::pmr::memory_resource *memres,
121 ErrorTy &&errp, Args &&...args) -> ev::context
122{
123 boost::capy::execution_context &exec_ctx = exec.context();
124
125 auto &srv = exec_ctx.use_service<ev::detail::handler_service>();
126 std::error_code ec = srv.init_context();
127 if (ec)
128 {
129 return errp.template with_error<ev::context>(ec);
130 }
131 srv.emplace_handler<HandlerTy>(std::forward<Args>(args)..., memres);
132 srv.start();
133 return errp.template with_success<ev::context>(detail::raw_context(&srv));
134}
135
148template <ev::detail::EventHandler HandlerTy, typename... Args>
149inline auto make_context (boost::capy::executor_ref exec, std::pmr::memory_resource *memres,
150 Args &&...args) -> ev::context
151{
152 return make_context<HandlerTy, detail::as_exception_t, Args...>(exec, memres, as_exception(),
153 std::forward<Args>(args)...);
154}
155
168template <ev::detail::EventHandler HandlerTy, ::co_usb::detail::ErrorProtocol<ev::context> ErrorTy,
169 typename... Args>
170inline auto make_context (boost::capy::executor_ref exec, ErrorTy &&errp, Args &&...args)
171 -> ev::context
172{
173 return make_context<HandlerTy, ErrorTy, Args...>(exec, std::pmr::get_default_resource(),
174 std::forward<ErrorTy>(errp),
175 std::forward<Args>(args)...);
176}
177
190template <ev::detail::EventHandler HandlerTy, typename... Args>
191inline auto make_context (boost::capy::executor_ref exec, Args &&...args) -> ev::context
192{
193 return make_context<HandlerTy, detail::as_exception_t, Args...>(
194 exec, std::pmr::get_default_resource(), as_exception(), std::forward<Args>(args)...);
195}
196
209template <ev::detail::EventHandler HandlerTy, ::co_usb::detail::ErrorProtocol<ev::context> ErrorTy,
210 typename... Args>
211inline auto make_context_with_options (boost::capy::executor_ref exec,
212 std::span<const libusb_init_option> options,
213 std::pmr::memory_resource *memres, ErrorTy &&errp,
214 Args &&...args) -> ev::context
215{
216 boost::capy::execution_context &exec_ctx = exec.context();
217
218 auto &srv = exec_ctx.use_service<ev::detail::handler_service>();
219 std::error_code ec = srv.init_context(options);
220 if (ec)
221 {
222 return errp.template with_error<ev::context>(ec);
223 }
224 srv.emplace_handler<HandlerTy>(std::forward<Args>(args)..., memres);
225 srv.start();
226 return errp.template with_success<ev::context>(detail::raw_context(&srv));
227}
228
242template <ev::detail::EventHandler HandlerTy, typename... Args>
243inline auto make_context_with_options (boost::capy::executor_ref exec,
244 std::span<const libusb_init_option> options,
245 std::pmr::memory_resource *memres, Args &&...args)
246 -> ev::context
247{
248 return make_context_with_options<HandlerTy, detail::as_exception_t, Args...>(
249 exec, options, memres, as_exception(), std::forward<Args>(args)...);
250}
251
265template <ev::detail::EventHandler HandlerTy, ::co_usb::detail::ErrorProtocol<ev::context> ErrorTy,
266 typename... Args>
267inline auto make_context_with_options (boost::capy::executor_ref exec,
268 std::span<const libusb_init_option> options, ErrorTy &&errp,
269 Args &&...args) -> ev::context
270{
271 return make_context_with_options<HandlerTy, ErrorTy, Args...>(
272 exec, options, std::pmr::get_default_resource(), std::forward<ErrorTy>(errp),
273 std::forward<Args>(args)...);
274}
275
276template <ev::detail::EventHandler HandlerTy, typename... Args>
277
291inline auto make_context_with_options (boost::capy::executor_ref exec,
292 std::span<const libusb_init_option> options, Args &&...args)
293 -> ev::context
294{
295 return make_context_with_options<HandlerTy, detail::as_exception_t, Args...>(
296 exec, options, std::pmr::get_default_resource(), as_exception(),
297 std::forward<Args>(args)...);
298}
299
300} // namespace co_usb
Utilities for creating error protocols.
Capy execution_context service wrapping a libusb event handler.
Definition flag_type.hpp:6
auto raw_context(::co_usb::ev::detail::handler_service *srv)
Proxy accessor to context ctor.
Definition context.hpp:101
Definition any_event_handler.hpp:15
Definition flag_type.hpp:6
constexpr auto as_exception() noexcept -> detail::as_exception_t
Definition error_protocol.hpp:13
A co_usb context referencing event handler service.
Definition context.hpp:62
auto handler() -> event_handler_ref
Definition context.hpp:73
auto request_stop()
Definition context.hpp:68
auto get_token() const noexcept
Definition context.hpp:63
auto usb_context() -> libusb_context *
Definition context.hpp:78
Execution context service that owns a libusb context and an event handler.
Definition handler_service.hpp:74
auto get_token() const noexcept -> std::stop_token
Get the stop token used for coordinating shutdown.
Definition handler_service.hpp:180
auto handler() -> event_handler_ref
Get a reference to the stored event handler.
Definition handler_service.hpp:201
auto usb_context() -> libusb_context *
Access the underlying libusb context.
Definition handler_service.hpp:230
auto request_stop()
Request stop for the event handler.
Definition handler_service.hpp:191
Type-erased reference to any event handler.
Definition event_handler_ref.hpp:34