1#ifndef UVW_EMITTER_INCLUDE_H
2#define UVW_EMITTER_INCLUDE_H
10#include <unordered_map>
14#include "type_info.hpp"
24 template<
typename Type,
typename = std::enable_if_t<std::is_
integral_v<Type>>>
25 explicit error_event(Type val) noexcept
26 : ec{
static_cast<int>(val)} {}
49 const char *
what() const noexcept;
58 const
char *
name() const noexcept;
70 explicit operator
bool() const noexcept;
82template<typename Elem, typename... Event>
85 template<
typename Type>
86 using listener_t = std::function<void(Type &, Elem &)>;
89 template<
typename Type>
90 const auto &handler()
const noexcept {
91 return std::get<listener_t<Type>>(handlers);
94 template<
typename Type>
95 auto &handler()
noexcept {
96 return std::get<listener_t<Type>>(handlers);
100 template<
typename Type>
101 void publish(Type event) {
102 if(
auto &listener = handler<Type>(); listener) {
103 listener(event, *
static_cast<Elem *
>(
this));
109 static_assert(std::is_base_of_v<
emitter<Elem, Event...>, Elem>);
122 template<
typename Type>
123 void on(listener_t<Type> f) {
124 handler<Type>() = std::move(f);
128 template<
typename Type>
130 handler<Type>() =
nullptr;
144 template<
typename Type>
145 bool has() const noexcept {
146 return static_cast<bool>(handler<Type>());
150 std::tuple<listener_t<error_event>, listener_t<Event>...> handlers{};
156# include "emitter.cpp"
Event emitter base class.
bool has() const noexcept
Checks if there is a listener registered for the specific event.
void on(listener_t< Type > f)
Registers a long-lived listener with the event emitter.
void reset() noexcept
Disconnects the listener for the given event type.
void reset() noexcept
Disconnects all listeners.
static int translate(int sys) noexcept
Returns the libuv error code equivalent to the given platform dependent error code.
int code() const noexcept
Gets the underlying error code, that is an error constant of libuv.
const char * what() const noexcept
Returns the error message for the given error code.
const char * name() const noexcept
Returns the error name for the given error code.