26 template <
typename TEventArgs = EventArgs>
29 struct SubscriberEntry {
33 SubscriberEntry() : m_nToken(0), m_fnAction() {}
35 : m_nToken(nToken), m_fnAction(fnAction) {}
37 bool operator==(
const SubscriberEntry& other)
const {
38 return m_nToken == other.m_nToken;
51 : m_lstSubscribers(other.m_lstSubscribers), m_nNextToken(other.m_nNextToken) {}
55 : m_lstSubscribers(std::move(other.m_lstSubscribers)), m_nNextToken(other.m_nNextToken) {
56 other.m_nNextToken = 1;
62 m_lstSubscribers = other.m_lstSubscribers;
63 m_nNextToken = other.m_nNextToken;
71 m_lstSubscribers = std::move(other.m_lstSubscribers);
72 m_nNextToken = other.m_nNextToken;
73 other.m_nNextToken = 1;
86 size_t nAssignedToken = m_nNextToken++;
89 m_lstSubscribers.Add(SubscriberEntry(nAssignedToken, fnHandler));
90 return nAssignedToken;
94 template <
typename F,
typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, EventHandler> && !std::is_same_v<std::decay_t<F>, Action<const
void*, const TEventArgs&>> && std::is_invocable_v<std::decay_t<F>, const
void*, const TEventArgs&>>>
95 size_t Add(F&& fnHandler) {
100 template <
typename TClass>
101 size_t Add(TClass* pInstance,
void (TClass::*pMethod)(
const void*,
const TEventArgs&)) {
102 return Add([pInstance, pMethod](
const void* pSender,
const TEventArgs& e) {
103 if (pInstance !=
nullptr && pMethod !=
nullptr) {
104 (pInstance->*pMethod)(pSender, e);
111 return Add(fnHandler);
115 template <
typename F,
typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, EventHandler> && !std::is_same_v<std::decay_t<F>, Action<const
void*, const TEventArgs&>> && std::is_invocable_v<std::decay_t<F>, const
void*, const TEventArgs&>>>
117 return Add(std::forward<F>(fnHandler));
122 int iCount = other.m_lstSubscribers.
GetCount();
123 for (
int i = 0; i < iCount; ++i) {
124 Add(other.m_lstSubscribers[i].m_fnAction);
134 for (
int i = 0; i < m_lstSubscribers.GetCount(); ++i) {
135 if (m_lstSubscribers[i].m_nToken == nToken) {
136 m_lstSubscribers.RemoveAt(i);
150 m_lstSubscribers.Clear();
156 void Invoke(
const void* pSender,
const TEventArgs& e)
const {
158 int iCount = m_lstSubscribers.GetCount();
159 for (
int i = 0; i < iCount; ++i) {
160 m_lstSubscribers[i].m_fnAction.Invoke(pSender, e);
165 void operator()(
const void* pSender,
const TEventArgs& e)
const {
171 return m_lstSubscribers.GetCount() == 0;
176 return static_cast<size_t>(m_lstSubscribers.GetCount());
180 explicit operator bool()
const {
185 template <
typename TEventArgs = EventArgs>
Encapsulates a delegate method that has parameters and returns void.
Defines common cross-platform macros, export decorators, and fundamental types.
Represents the base class for classes that contain event data.
Represents a strongly typed list of objects that can be accessed by index mirroring ....
Base object class for DotNetDupe mirroring .NET System.Object.
Encapsulates a method that has parameters and does not return a value.
Represents a strongly typed list of objects accessible by index.
int GetCount() const
Gets the number of elements contained in the List.
Represents the method that will handle an event when the event provides data.
~EventHandler()=default
Destructor.
void operator()(const void *pSender, const TEventArgs &e) const
Invocation call operator.
EventHandler & operator=(EventHandler &&other) noexcept
Move assignment operator.
void Clear()
Removes all registered subscribers.
bool IsEmpty() const
Checks whether the multicast event handler has zero registered subscribers.
EventHandler & operator+=(const EventHandler &other)
Multicast addition operator. Appends all subscribers from another EventHandler.
size_t GetSubscriberCount() const
Gets the number of currently active subscribers.
EventHandler(const EventHandler &other)
Copy constructor. Copies all subscriber callbacks.
EventHandler(EventHandler &&other) noexcept
Move constructor. Transfers subscriber callbacks.
size_t Add(const Action< const void *, const TEventArgs & > &fnHandler)
Subscribes an Action delegate handler and returns a monotonic subscription token.
void Invoke(const void *pSender, const TEventArgs &e) const
Sequentially invokes all registered subscriber callbacks in FIFO order.
size_t Add(TClass *pInstance, void(TClass::*pMethod)(const void *, const TEventArgs &))
Subscribes a member method on an instance object.
size_t Add(F &&fnHandler)
Subscribes a generic callable object (lambda or function pointer).
bool Remove(size_t nToken)
Unsubscribes a subscriber matching the specified subscription token.
size_t operator+=(const Action< const void *, const TEventArgs & > &fnHandler)
Multicast addition operator. Subscribes an Action callback.
EventHandler()
Initializes a new empty multicast EventHandler instance.
size_t operator+=(F &&fnHandler)
Multicast addition operator. Subscribes a generic callable object.
bool operator-=(size_t nToken)
Multicast subtraction operator. Unsubscribes using a subscription token.
EventHandler & operator=(const EventHandler &other)
Copy assignment operator.