co_usb
Loading...
Searching...
No Matches
service.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/capy/ex/execution_context.hpp>
4#include <libusb.h>
5#include <memory>
6#include <optional>
7#include <stop_token>
8#include <thread>
9#include <utility>
10
12{
13
20struct handler_service : public boost::capy::execution_context::service
21{
22 using handler_fn_t = void (*)(libusb_context *, std::stop_token);
23 handler_service(boost::capy::execution_context &ctx);
24
31 template <std::invocable<libusb_context *, std::stop_token> HandlerFn>
32 void start_thread (std::shared_ptr<libusb_context> ctx, HandlerFn &&handler_fn)
33 {
34 m_ctx = ctx;
35 m_handler_thread = std::jthread{
36 [ctx = ctx.get(), handler_fn = std::forward<HandlerFn>(handler_fn)] (std::stop_token st)
37 { handler_fn(ctx, st); }};
38 }
39
40 std::stop_source stop_source();
41
42 ~handler_service() override;
43 void shutdown() override;
44
45 private:
46 std::shared_ptr<libusb_context> m_ctx;
47 std::optional<std::jthread> m_handler_thread;
48};
49
50} // namespace co_usb::detail
Definition service.hpp:12
use_service
Definition context.hpp:16
Service for handling libusb events.
Definition service.hpp:21
std::stop_source stop_source()
Definition service.cpp:9
void shutdown() override
Definition service.cpp:22
void start_thread(std::shared_ptr< libusb_context > ctx, HandlerFn &&handler_fn)
Creates a default handler thread.
Definition service.hpp:32
void(*)(libusb_context *, std::stop_token) handler_fn_t
Definition service.hpp:22
~handler_service() override
Definition service.cpp:18