4#include <boost/capy/concept/executor.hpp>
5#include <boost/capy/ex/this_coro.hpp>
6#include <boost/capy/task.hpp>
8#include <libusb-1.0/libusb.h>
32template <use_service Service = use_service::yes>
struct context;
37 template <boost::capy::Executor Exec,
38 std::invocable<libusb_context *, std::stop_token> HandlerFn>
39 void init (Exec &&exec, HandlerFn &&handler_fn)
42 auto r = libusb_init(&ctx);
43 if (r != LIBUSB_SUCCESS)
45 throw std::runtime_error{
"Cannot initialize libusb"};
47 m_ctx = {ctx, [] (libusb_context *ctx) { libusb_exit(ctx); }};
60 template <boost::capy::Executor
Exec,
61 std::invocable<libusb_context *, std::stop_token>
HandlerFn>
64 init(std::forward<Exec>(
exec), std::forward<HandlerFn>(
handler_fn));
67 template <
typename R, boost::capy::Executor
Exec,
68 std::invocable<libusb_context *, std::stop_token>
HandlerFn>
69 requires std::ranges::range<R> && std::same_as<std::ranges::range_value_t<R>,
libusb_option>
72 init(std::forward<Exec>(
exec), std::forward<HandlerFn>(
handler_fn));
97 return m_ss.get_token();
101 std::shared_ptr<libusb_context> m_ctx;
102 std::stop_source m_ss;
113 template <
typename R>
114 requires std::ranges::range<R> && std::same_as<std::ranges::range_value_t<R>,
libusb_option>
Definition context.hpp:14
void default_handler(libusb_context *ctx, std::stop_token st)
default handler function for the service
Definition context.cpp:24
use_service
Definition context.hpp:17
context(context const &)=delete
context & operator=(context const &)=delete
context(R &&options)
Definition context.hpp:115
auto * get() const noexcept
Definition context.hpp:132
context(context &&)=delete
context & operator=(context &&)=delete
auto * get() const noexcept
Definition context.hpp:85
context & operator=(context const &)=delete
auto get_token()
Definition context.hpp:95
context(Exec &&exec)
Definition context.hpp:55
context(context &&)=delete
auto request_stop()
Definition context.hpp:90
context(Exec &&exec, HandlerFn &&handler_fn)
Definition context.hpp:62
context(R &&options, Exec &&exec, HandlerFn &&handler_fn)
Definition context.hpp:70
context(context const &)=delete
context & operator=(context &&)=delete
libusb_context wrapper
Definition context.hpp:32
Service for handling libusb events.
Definition service.hpp:21
std::stop_source stop_source()
Definition service.cpp:10
void start_thread(std::shared_ptr< libusb_context > ctx, HandlerFn &&handler_fn)
Creates a default handler thread.
Definition service.hpp:32