Lodestone.Common
Loading...
Searching...
No Matches
lodestone::common::event::Event< Args > Class Template Reference

#include <Event.h>

Collaboration diagram for lodestone::common::event::Event< Args >:

Classes

struct  Subscriber

Public Types

using Handler = std::function<void(Args...)>

Public Member Functions

template<std::invocable< Args... > Func>
FunctionHandle subscribe (Func &&handler)
bool unsubscribe (FunctionHandle id)
void notify (Args... args)
template<std::invocable< Args... > Func>
FunctionHandle operator+= (Func &&handler)
bool operator-= (const FunctionHandle id)

Static Public Attributes

static constexpr FunctionHandle MAX_SUBSCRIBERS = std::numeric_limits<FunctionHandle>::max()

Protected Attributes

std::vector< Subscriber > m_listeners
FunctionHandle m_nextId = 0

Member Typedef Documentation

◆ Handler

template<typename... Args>
using lodestone::common::event::Event< Args >::Handler = std::function<void(Args...)>

Member Function Documentation

◆ notify()

template<typename... Args>
void lodestone::common::event::Event< Args >::notify ( Args... args)
inline
56 {
57 for (Subscriber listener : this->m_listeners) {
59 }
60 }
Definition Event.h:26
std::vector< Subscriber > m_listeners
Definition Event.h:73

◆ operator+=()

template<typename... Args>
template<std::invocable< Args... > Func>
FunctionHandle lodestone::common::event::Event< Args >::operator+= ( Func && handler)
inline
64 {
66 }
FunctionHandle subscribe(Func &&handler)
Definition Event.h:38

◆ operator-=()

template<typename... Args>
bool lodestone::common::event::Event< Args >::operator-= ( const FunctionHandle id)
inline
68 {
69 return this->unsubscribe(id);
70 }
bool unsubscribe(FunctionHandle id)
Definition Event.h:50

◆ subscribe()

template<typename... Args>
template<std::invocable< Args... > Func>
FunctionHandle lodestone::common::event::Event< Args >::subscribe ( Func && handler)
inline
38 {
39 if (this->m_nextId == MAX_SUBSCRIBERS) // how the fuck do you manage this
40 throw std::length_error(std::format("The maximum number of subscribers to this event has been reached ({}/{})", this->m_listeners.size(), MAX_SUBSCRIBERS));
41
42 this->m_listeners.push_back(Subscriber {
43 this->m_nextId,
45 });
46
47 return this->m_nextId++;
48 }
FunctionHandle m_nextId
Definition Event.h:75
static constexpr FunctionHandle MAX_SUBSCRIBERS
Definition Event.h:30

◆ unsubscribe()

template<typename... Args>
bool lodestone::common::event::Event< Args >::unsubscribe ( FunctionHandle id)
inline
50 {
51 return std::erase_if(this->m_listeners, [&id](const Subscriber &subscriber) {
52 return subscriber.id == id;
53 }) > 0;
54 }

Member Data Documentation

◆ m_listeners

template<typename... Args>
std::vector<Subscriber> lodestone::common::event::Event< Args >::m_listeners
protected

◆ m_nextId

template<typename... Args>
FunctionHandle lodestone::common::event::Event< Args >::m_nextId = 0
protected

◆ MAX_SUBSCRIBERS

template<typename... Args>
FunctionHandle lodestone::common::event::Event< Args >::MAX_SUBSCRIBERS = std::numeric_limits<FunctionHandle>::max()
staticconstexpr

The documentation for this class was generated from the following file: