39 template <detail::EventHandler HandlerTy>
40 requires(!std::same_as<HandlerTy, event_handler_ref>)
41 event_handler_ref(HandlerTy &handler) : m_original(&handler), m_vtable(make_vtable(handler))
46 template <detail::EventHandler HandlerTy>
47 requires(!std::same_as<HandlerTy, event_handler_ref>)
50 if (m_original == &handler)
52 m_original = &handler;
53 m_vtable = make_vtable(handler);
59 : m_original(other.m_original), m_vtable(other.m_vtable)
70 m_original = other.m_original;
71 m_vtable = other.m_vtable;
77 : m_original(other.m_original), m_vtable(other.m_vtable)
79 other.m_original =
nullptr;
90 m_original = other.m_original;
91 m_vtable = other.m_vtable;
92 other.m_original =
nullptr;
101 inline auto valid () const noexcept ->
bool
103 return m_original !=
nullptr;
106 auto start (libusb_context *usb_ctx, std::stop_token
stop) ->
bool
112 return m_vtable.start_fn(m_original, usb_ctx, std::move(
stop));
115 auto ref () noexcept ->
void
121 return m_vtable.ref_fn(m_original);
130 return m_vtable.unref_fn(m_original);
139 return m_vtable.stop_fn(m_original);
145 using start_fn_t =
auto (*)(
void *, libusb_context *, std::stop_token) -> bool;
146 using ref_fn_t =
auto (*)(
void *) -> void;
147 using unref_fn_t =
auto (*)(
void *) -> void;
148 using stop_fn_t =
auto (*)(
void *) -> void;
150 start_fn_t start_fn{
nullptr};
151 ref_fn_t ref_fn{
nullptr};
152 unref_fn_t unref_fn{
nullptr};
153 stop_fn_t stop_fn{
nullptr};
156 template <detail::EventHandler HandlerTy>
157 requires(!std::same_as<HandlerTy, event_handler_ref>)
158 auto make_vtable (HandlerTy &handler) -> vtable
162 .start_fn = +[] (
void *orig, libusb_context *ctx, std::stop_token
stop) ->
bool
163 {
return static_cast<HandlerTy *
>(orig)->
start(ctx,
stop); },
164 .ref_fn = +[] (
void *orig) ->
void {
return static_cast<HandlerTy *
>(orig)->
ref(); },
165 .unref_fn = +[] (
void *orig) ->
void
166 {
return static_cast<HandlerTy *
>(orig)->
unref(); },
167 .stop_fn = +[] (
void *orig) ->
void {
return static_cast<HandlerTy *
>(orig)->
stop(); },
172 void *m_original{
nullptr};
176static_assert(detail::EventHandler<event_handler_ref>,
"Not a proper USB event handler");
Owning type-erased wrapper for EventHandler.
Specification and implementation of EventHandler concept.
Definition any_event_handler.hpp:15
Type-erased reference to any event handler.
Definition event_handler_ref.hpp:34
auto start(libusb_context *usb_ctx, std::stop_token stop) -> bool
Definition event_handler_ref.hpp:106
event_handler_ref(event_handler_ref const &other)
Definition event_handler_ref.hpp:58
event_handler_ref(HandlerTy &handler)
Definition event_handler_ref.hpp:41
auto ref() noexcept -> void
Definition event_handler_ref.hpp:115
auto stop() -> void
Definition event_handler_ref.hpp:133
auto unref() noexcept -> void
Definition event_handler_ref.hpp:124
event_handler_ref & operator=(event_handler_ref const &other)
Definition event_handler_ref.hpp:64
auto valid() const noexcept -> bool
Definition event_handler_ref.hpp:101
~event_handler_ref()
Definition event_handler_ref.hpp:96
event_handler_ref() noexcept
Definition event_handler_ref.hpp:35
event_handler_ref(event_handler_ref &&other) noexcept
Definition event_handler_ref.hpp:76
event_handler_ref & operator=(event_handler_ref &&other) noexcept
Definition event_handler_ref.hpp:82