CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
EventDispatcher.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
12#include "Event.hpp"
13
15
16namespace CeresEngine {
17
21 public:
23 virtual ~AbstractEntityEventDispatcher() = default;
24 };
25
28 template<typename E> class EntityEventDispatcher final : public AbstractEntityEventDispatcher {
29 private:
31 UnsafeEvent<void(const Entity&, const E&)> mSignal;
32
33 public:
37 void emit(const Entity& entity, const E& event) const;
38
44 template<typename Subscriber> WeakEventConnection subscribe(Subscriber&& subscriber);
45 };
46
47} // namespace CeresEngine
48
49namespace CeresEngine {
50
51 template<typename E> void EntityEventDispatcher<E>::emit(const Entity& entity, const E& event) const { mSignal(entity, event); }
52
53 template<typename E> template<typename Subscriber> WeakEventConnection EntityEventDispatcher<E>::subscribe(Subscriber&& subscriber) {
54 return mSignal.connect(subscriber);
55 }
56
57} // namespace CeresEngine
The event dispatcher is a class responsible for forwarding events to their respective listeners.
Definition EventDispatcher.hpp:20
virtual ~AbstractEntityEventDispatcher()=default
Defaulted virtual destructor.
A type-safe event dispatcher implementation for events of type E.
Definition EventDispatcher.hpp:28
WeakEventConnection subscribe(Subscriber &&subscriber)
Subscribe a new subscriber of type Subscriber.
Definition EventDispatcher.hpp:53
void emit(const Entity &entity, const E &event) const
Emits a event.
Definition EventDispatcher.hpp:51
UnsafeEvent< void(const Entity &, const E &)> mSignal
A signal that effectively propagates the event.
Definition EventDispatcher.hpp:31
The base entity class.
Definition Entity.hpp:41
Base template for the event class.
Definition Event.hpp:27
Connection class.
Definition Event.hpp:44
Definition Application.hpp:19
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25