EnTT 4.0.0
Loading...
Searching...
No Matches
emitter.hpp
1#ifndef ENTT_SIGNAL_EMITTER_HPP
2#define ENTT_SIGNAL_EMITTER_HPP
3
4#include "../container/dense_map.hpp"
5#include "../core/compressed_pair.hpp"
6#include "../core/fwd.hpp"
7#include "../core/type_info.hpp"
8#include "../stl/functional.hpp"
9#include "../stl/type_traits.hpp"
10#include "../stl/utility.hpp"
11#include "fwd.hpp"
12
13namespace entt {
14
34template<typename Derived, typename Allocator>
35class emitter {
36 using key_type = id_type;
37 using mapped_type = stl::function<void(void *)>;
38
39 using alloc_traits = stl::allocator_traits<Allocator>;
40 using container_allocator = alloc_traits::template rebind_alloc<stl::pair<const key_type, mapped_type>>;
41 using container_type = dense_map<key_type, mapped_type, stl::identity, stl::equal_to<>, container_allocator>;
42
43public:
45 using allocator_type = Allocator;
47 using size_type = stl::size_t;
48
52
57 explicit emitter(const allocator_type &allocator)
58 : handlers{allocator, allocator} {}
59
61 emitter(const emitter &) = delete;
62
67 emitter(emitter &&other) noexcept
68 : handlers{stl::move(other.handlers)} {}
69
75 emitter(emitter &&other, const allocator_type &allocator)
76 : handlers{container_type{stl::move(other.handlers.first()), allocator}, allocator} {
77 ENTT_ASSERT(alloc_traits::is_always_equal::value || handlers.second() == other.handlers.second(), "Copying an emitter is not allowed");
78 }
79
81 virtual ~emitter() {
82 static_assert(stl::is_base_of_v<emitter<Derived, Allocator>, Derived>, "Invalid emitter type");
83 }
84
89 emitter &operator=(const emitter &) = delete;
90
96 emitter &operator=(emitter &&other) noexcept {
97 ENTT_ASSERT(alloc_traits::is_always_equal::value || handlers.second() == other.handlers.second(), "Copying an emitter is not allowed");
98 swap(other);
99 return *this;
100 }
101
106 void swap(emitter &other) noexcept {
107 using stl::swap;
108 swap(handlers, other.handlers);
109 }
110
115 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
116 return handlers.second();
117 }
118
124 template<typename Type>
125 void publish(Type value) {
126 if(const auto id = type_id<Type>().hash(); handlers.first().contains(id)) {
127 handlers.first()[id](&value);
128 }
129 }
130
136 template<typename Type>
137 void on(stl::function<void(Type &, Derived &)> func) {
138 handlers.first().insert_or_assign(type_id<Type>().hash(), [func = stl::move(func), this](void *value) {
139 func(*static_cast<Type *>(value), static_cast<Derived &>(*this));
140 });
141 }
142
147 template<typename Type>
148 void erase() {
149 handlers.first().erase(type_hash<stl::remove_cvref_t<Type>>::value());
150 }
151
153 void clear() noexcept {
154 handlers.first().clear();
155 }
156
162 template<typename Type>
163 [[nodiscard]] bool contains() const {
164 return handlers.first().contains(type_hash<stl::remove_cvref_t<Type>>::value());
165 }
166
171 [[nodiscard]] bool empty() const noexcept {
172 return handlers.first().empty();
173 }
174
175private:
177};
178
179} // namespace entt
180
181#endif
A compressed pair.
Associative container for key-value pairs with unique keys.
emitter & operator=(const emitter &)=delete
Default copy assignment operator, deleted on purpose.
Allocator allocator_type
Allocator type.
Definition emitter.hpp:45
void on(stl::function< void(Type &, Derived &)> func)
Registers a listener with the event emitter.
Definition emitter.hpp:137
emitter(emitter &&other) noexcept
Move constructor.
Definition emitter.hpp:67
emitter & operator=(emitter &&other) noexcept
Move assignment operator.
Definition emitter.hpp:96
emitter(const allocator_type &allocator)
Constructs an emitter with a given allocator.
Definition emitter.hpp:57
void clear() noexcept
Disconnects all the listeners.
Definition emitter.hpp:153
emitter()
Default constructor.
Definition emitter.hpp:50
bool contains() const
Checks if there are listeners registered for the specific event.
Definition emitter.hpp:163
stl::size_t size_type
Unsigned integer type.
Definition emitter.hpp:47
void erase()
Disconnects a listener from the event emitter.
Definition emitter.hpp:148
emitter(const emitter &)=delete
Default copy constructor, deleted on purpose.
void swap(emitter &other) noexcept
Exchanges the contents with those of a given emitter.
Definition emitter.hpp:106
bool empty() const noexcept
Checks if there are listeners registered with the event emitter.
Definition emitter.hpp:171
emitter(emitter &&other, const allocator_type &allocator)
Allocator-extended move constructor.
Definition emitter.hpp:75
virtual ~emitter()
Default destructor.
Definition emitter.hpp:81
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition emitter.hpp:115
void publish(Type value)
Publishes a given event.
Definition emitter.hpp:125
Custom EnTT namespace for the standard template library.
Definition entt.hpp:5
EnTT default namespace.
Definition dense_map.hpp:25
const type_info & type_id() noexcept
Returns the type info object associated to a given type.
stl::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:29