Plasma Engine  2.0
Loading...
Searching...
No Matches
Event.h
1#pragma once
2
3#include <Foundation/Containers/HybridArray.h>
4#include <Foundation/Threading/Lock.h>
5#include <Foundation/Threading/Mutex.h>
6#include <Foundation/Types/Delegate.h>
7
9using plEventSubscriptionID = plUInt32;
10
12enum class plEventType
13{
14 Default,
15 CopyOnBroadcast
16};
17
35template <typename EventData, typename MutexType, plEventType EventType>
37{
38protected:
40 plEventBase(plAllocator* pAllocator);
42
43public:
45 using Handler = plDelegate<void(EventData)>;
46
50 {
51 PL_DISALLOW_COPY_AND_ASSIGN(Unsubscriber);
52
53 public:
54 Unsubscriber() = default;
56 {
57 m_pEvent = other.m_pEvent;
58 m_SubscriptionID = other.m_SubscriptionID;
59 other.Clear();
60 }
62
63 void operator=(Unsubscriber&& other)
64 {
66
67 m_pEvent = other.m_pEvent;
68 m_SubscriptionID = other.m_SubscriptionID;
69 other.Clear();
70 }
71
74 {
75 if (m_SubscriptionID == 0)
76 return;
77
78 m_pEvent->RemoveEventHandler(m_SubscriptionID);
79 Clear();
80 }
81
83 bool IsSubscribed() const { return m_SubscriptionID != 0; }
84
87 void Clear()
88 {
89 m_pEvent = nullptr;
90 m_SubscriptionID = 0;
91 }
92
93 private:
94 friend class plEventBase<EventData, MutexType, EventType>;
95
96 const plEventBase<EventData, MutexType, EventType>* m_pEvent = nullptr;
97 plEventSubscriptionID m_SubscriptionID = 0;
98 };
99
101 enum
102 {
104 RecursionDepthSupported = (EventType == plEventType::Default || plConversionTest<MutexType, plNoMutex>::sameType == 1) ? 1 : 0,
105
110 };
111
115 void Broadcast(EventData pEventData, plUInt8 uiMaxRecursionDepth = MaxRecursionDepthDefault); // [tested]
116
120 plEventSubscriptionID AddEventHandler(Handler handler) const; // [tested]
121
125 void AddEventHandler(Handler handler, Unsubscriber& inout_unsubscriber) const; // [tested]
126
128 void RemoveEventHandler(const Handler& handler) const; // [tested]
129
134 void RemoveEventHandler(plEventSubscriptionID& inout_id) const;
135
137 bool HasEventHandler(const Handler& handler) const;
138
140 void Clear();
141
143 bool IsEmpty() const;
144
145 // it would be a problem if the plEvent moves in memory, for instance the Unsubscriber's would point to invalid memory
146 PL_DISALLOW_COPY_AND_ASSIGN(plEventBase);
147
148private:
149 // Used to detect recursive broadcasts and then throw asserts at you.
150 plUInt8 m_uiRecursionDepth = 0;
151 mutable plEventSubscriptionID m_NextSubscriptionID = 0;
152
153 mutable MutexType m_Mutex;
154
155#if PL_ENABLED(PL_COMPILE_FOR_DEVELOPMENT)
156 const void* m_pSelf = nullptr;
157#endif
158
159 struct HandlerData
160 {
161 Handler m_Handler;
162 plEventSubscriptionID m_SubscriptionID;
163 };
164
166 mutable plDynamicArray<HandlerData> m_EventHandlers;
167};
168
171{
172};
173
175template <typename EventData, typename MutexType = plNoMutex, typename AllocatorWrapper = plDefaultAllocatorWrapper, plEventType EventType = plEventType::Default>
176class plEvent : public plEventBase<EventData, MutexType, EventType>
177{
178public:
179 plEvent();
180 plEvent(plAllocator* pAllocator);
181};
182
183template <typename EventData, typename MutexType = plNoMutex, typename AllocatorWrapper = plDefaultAllocatorWrapper>
185
186#include <Foundation/Communication/Implementation/Event_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
Definition DynamicArray.h:81
An object that can be passed to plEvent::AddEventHandler to store the subscription information and au...
Definition Event.h:50
void Unsubscribe()
If the unsubscriber holds a valid subscription, it will be removed from the target plEvent.
Definition Event.h:73
void Clear()
Resets the unsubscriber. Use when the target plEvent may have been destroyed and automatic unsubscrip...
Definition Event.h:87
bool IsSubscribed() const
Checks whether this unsubscriber has a valid subscription.
Definition Event.h:83
This class propagates event information to registered event handlers.
Definition Event.h:37
@ MaxRecursionDepthDefault
Definition Event.h:109
@ RecursionDepthSupported
Whether the uiMaxRecursionDepth parameter to Broadcast() is supported in this implementation or not.
Definition Event.h:104
bool IsEmpty() const
Returns true, if no event handlers are registered.
Definition Event_inl.h:179
plEventBase(plAllocator *pAllocator)
Constructor.
Definition Event_inl.h:6
bool HasEventHandler(const Handler &handler) const
Checks whether an event handler has already been registered.
Definition Event_inl.h:156
void Clear()
Removes all registered event handlers.
Definition Event_inl.h:172
plEventSubscriptionID AddEventHandler(Handler handler) const
Adds a function as an event handler. All handlers will be notified in the order that they were regist...
Definition Event_inl.h:25
void RemoveEventHandler(const Handler &handler) const
Removes a previously registered handler. It is an error to remove a handler that was not registered.
Definition Event_inl.h:68
void Broadcast(EventData pEventData, plUInt8 uiMaxRecursionDepth=MaxRecursionDepthDefault)
This function will broadcast to all registered users, that this event has just happened....
Definition Event_inl.h:187
Definition Event.h:177
Static Conversion Test.
Definition TypeTraits.h:73
Can be used when plEvent is used without any additional data.
Definition Event.h:171