1 #ifndef UVW_EMITTER_INCLUDE_H
2 #define UVW_EMITTER_INCLUDE_H
25 template<
typename U,
typename = std::enable_if_t<std::is_
integral_v<U>>>
27 : ec{
static_cast<int>(val)}
51 const char *
what()
const noexcept;
60 const char *
name()
const noexcept;
66 int code()
const noexcept;
72 explicit operator bool()
const noexcept;
88 virtual ~BaseHandler() noexcept =
default;
89 virtual bool empty()
const noexcept = 0;
90 virtual void clear() noexcept = 0;
94 struct Handler final: BaseHandler {
95 using Listener = std::function<void(E &, T &)>;
96 using Element = std::pair<bool, Listener>;
97 using ListenerList = std::list<Element>;
98 using Connection =
typename ListenerList::iterator;
100 bool empty()
const noexcept
override {
101 auto pred = [](
auto &&element){
return element.first; };
103 return std::all_of(onceL.cbegin(), onceL.cend(), pred) &&
104 std::all_of(onL.cbegin(), onL.cend(), pred);
107 void clear() noexcept
override {
109 auto func = [](
auto &&element){ element.first =
true; };
110 std::for_each(onceL.begin(), onceL.end(), func);
111 std::for_each(onL.begin(), onL.end(), func);
119 return onceL.emplace(onceL.cend(),
false, std::move(f));
123 return onL.emplace(onL.cend(),
false, std::move(f));
130 auto pred = [](
auto &&element){
return element.first; };
131 onceL.remove_if(pred);
136 void publish(E event, T &ref) {
137 ListenerList currentL;
138 onceL.swap(currentL);
140 auto func = [&event, &ref](
auto &&element) {
141 return element.first ? void() : element.second(event, ref);
146 std::for_each(onL.rbegin(), onL.rend(), func);
147 std::for_each(currentL.rbegin(), currentL.rend(), func);
151 onL.remove_if([](
auto &&element){
return element.first; });
155 bool publishing{
false};
156 ListenerList onceL{};
160 static std::size_t next_type() noexcept {
161 static std::size_t counter = 0;
166 static std::size_t event_type() noexcept {
167 static std::size_t value = next_type();
172 Handler<E> & handler() noexcept {
173 std::size_t type = event_type<E>();
175 if(!(type < handlers.size())) {
176 handlers.resize(type+1);
179 if(!handlers[type]) {
180 handlers[type] = std::make_unique<Handler<E>>();
183 return static_cast<Handler<E>&
>(*handlers[type]);
188 void publish(E event) {
189 handler<E>().publish(std::move(event), *
static_cast<T*
>(
this));
194 using Listener =
typename Handler<E>::Listener;
205 template<
typename>
friend class Emitter;
211 Connection(
typename Handler<E>::Connection conn)
212 : Handler<E>::Connection{std::move(conn)}
220 static_assert(std::is_base_of_v<
Emitter<T>, T>);
239 Connection<E>
on(Listener<E> f) {
240 return handler<E>().on(std::move(f));
259 Connection<E>
once(Listener<E> f) {
260 return handler<E>().once(std::move(f));
268 void erase(Connection<E> conn) noexcept {
269 handler<E>().erase(std::move(conn));
277 handler<E>().clear();
284 std::for_each(handlers.begin(), handlers.end(),
285 [](
auto &&hdlr){ if(hdlr) { hdlr->clear(); } });
295 std::size_t type = event_type<E>();
297 return (!(type < handlers.size()) ||
299 static_cast<Handler<E>&
>(*handlers[type]).empty());
308 return std::all_of(handlers.cbegin(), handlers.cend(),
309 [](
auto &&hdlr){ return !hdlr || hdlr->empty(); });
313 std::vector<std::unique_ptr<BaseHandler>> handlers{};
321 #include "emitter.cpp"
324 #endif // UVW_EMITTER_INCLUDE_H