uvw 3.4.0
Loading...
Searching...
No Matches
emitter.h
1#ifndef UVW_EMITTER_INCLUDE_H
2#define UVW_EMITTER_INCLUDE_H
3
4#include <cstddef>
5#include <cstdint>
6#include <functional>
7#include <list>
8#include <memory>
9#include <type_traits>
10#include <unordered_map>
11#include <utility>
12#include <uv.h>
13#include "config.h"
14#include "type_info.hpp"
15
16namespace uvw {
17
23struct error_event {
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)} {}
27
40 static int translate(int sys) noexcept;
41
49 const char *what() const noexcept;
50
58 const char *name() const noexcept;
59
64 int code() const noexcept;
65
70 explicit operator bool() const noexcept;
71
72private:
73 const int ec;
74};
75
82template<typename Elem, typename... Event>
83class emitter {
84public:
85 template<typename Type>
86 using listener_t = std::function<void(Type &, Elem &)>;
87
88private:
89 template<typename Type>
90 const auto &handler() const noexcept {
91 return std::get<listener_t<Type>>(handlers);
92 }
93
94 template<typename Type>
95 auto &handler() noexcept {
96 return std::get<listener_t<Type>>(handlers);
97 }
98
99protected:
100 template<typename Type>
101 void publish(Type event) {
102 if(auto &listener = handler<Type>(); listener) {
103 listener(event, *static_cast<Elem *>(this));
104 }
105 }
106
107public:
108 virtual ~emitter() noexcept {
109 static_assert(std::is_base_of_v<emitter<Elem, Event...>, Elem>);
110 }
111
122 template<typename Type>
123 void on(listener_t<Type> f) {
124 handler<Type>() = std::move(f);
125 }
126
128 template<typename Type>
129 void reset() noexcept {
130 handler<Type>() = nullptr;
131 }
132
134 void reset() noexcept {
136 (reset<Event>(), ...);
137 }
138
144 template<typename Type>
145 bool has() const noexcept {
146 return static_cast<bool>(handler<Type>());
147 }
148
149private:
150 std::tuple<listener_t<error_event>, listener_t<Event>...> handlers{};
151};
152
153} // namespace uvw
154
155#ifndef UVW_AS_LIB
156# include "emitter.cpp"
157#endif
158
159#endif // UVW_EMITTER_INCLUDE_H
Event emitter base class.
Definition emitter.h:83
bool has() const noexcept
Checks if there is a listener registered for the specific event.
Definition emitter.h:145
void on(listener_t< Type > f)
Registers a long-lived listener with the event emitter.
Definition emitter.h:123
void reset() noexcept
Disconnects the listener for the given event type.
Definition emitter.h:129
void reset() noexcept
Disconnects all listeners.
Definition emitter.h:134
uvw default namespace.
Definition async.h:8
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.