Add Core::CancellableEventBase

- Added Core::CancellableEventBase
- Updated Core::EventManager to immediately stop event dispatch if one of listeners set it as cancelled
This commit is contained in:
izawartka 2026-08-07 22:09:29 +02:00
parent 4087241cdc
commit 92061c2fa8
2 changed files with 28 additions and 0 deletions

View File

@ -2,6 +2,8 @@
namespace Core
{
class CancellableEventBase;
template<typename EventT, typename = void>
struct EventHasInit : std::false_type {};
@ -20,10 +22,32 @@ namespace Core
template<typename EventT>
static constexpr bool EventHasDeinit_v = EventHasDeinit<EventT>::value;
template<typename EventT>
using EventIsCancellable = std::is_base_of<CancellableEventBase, EventT>;
template<typename EventT>
constexpr bool EventIsCancellable_v = EventIsCancellable<EventT>::value;
// Base class for all events. Not to be used directly.
class EventBase
{
protected:
EventBase() = default;
virtual ~EventBase() = default;
};
// Base class for events that can be cancelled. Not to be used directly.
class CancellableEventBase : public EventBase
{
protected:
CancellableEventBase() = default;
virtual ~CancellableEventBase() = default;
public:
virtual void Cancel() final { m_cancelled = true; }
virtual bool IsCancelled() const final { return m_cancelled; }
private:
bool m_cancelled = false;
};
}

View File

@ -42,6 +42,10 @@ namespace Core {
for (auto& wrapper : m_listeners) {
wrapper.listener(event);
if constexpr (EventIsCancellable_v<EventT>) {
if (event.IsCancelled()) break;
}
if (wrapper.isToBeRemoved) {
spdlog::debug("EventManager: Skipping listener id {} marked for removal for event type: {}", wrapper.id, typeid(EventT).name());
continue;