11#ifndef LODESTONE_EVENT_H
12#define LODESTONE_EVENT_H
25 template <
typename... Args>
28 using Handler = std::function<void(Args...)>;
37 template <std::invocable<Args...> Func>
40 throw std::length_error(std::format(
"The maximum number of subscribers to this event has been reached ({}/{})", this->
m_listeners.size(),
MAX_SUBSCRIBERS));
44 std::forward<Func>(handler)
51 return std::erase_if(this->
m_listeners, [&
id](
const Subscriber &subscriber) {
52 return subscriber.id == id;
58 std::invoke(listener.handler, std::forward<Args>(args)...);
63 template <std::invocable<Args...> Func>
65 return this->
subscribe(std::forward<Func>(handler));
78 template <
typename... Args>
std::function< void(Args...)> Handler
Definition Event.h:28
FunctionHandle operator+=(Func &&handler)
Definition Event.h:64
FunctionHandle m_nextId
Definition Event.h:75
FunctionHandle subscribe(Func &&handler)
Definition Event.h:38
bool unsubscribe(FunctionHandle id)
Definition Event.h:50
void notify(Args... args)
Definition Event.h:56
bool operator-=(const FunctionHandle id)
Definition Event.h:68
std::vector< Subscriber > m_listeners
Definition Event.h:73
static constexpr FunctionHandle MAX_SUBSCRIBERS
Definition Event.h:30
Definition Cancellable.h:14
unsigned int FunctionHandle
Definition Event.h:23
Event< Cancellable &, Args... > CancellableEvent
Definition Event.h:79
Definition Cancellable.h:15
Handler handler
Definition Event.h:34
FunctionHandle id
Definition Event.h:33