EnTT 3.14.0
Loading...
Searching...
No Matches
emitter.hpp
1#ifndef ENTT_SIGNAL_EMITTER_HPP
2#define ENTT_SIGNAL_EMITTER_HPP
3
4#include <functional>
5#include <type_traits>
6#include <utility>
7#include "../container/dense_map.hpp"
8#include "../core/compressed_pair.hpp"
9#include "../core/fwd.hpp"
10#include "../core/type_info.hpp"
11#include "../core/utility.hpp"
12#include "fwd.hpp"
13
14namespace entt {
15
35template<typename Derived, typename Allocator>
36class emitter {
37 using key_type = id_type;
38 using mapped_type = std::function<void(void *)>;
39
40 using alloc_traits = std::allocator_traits<Allocator>;
41 using container_allocator = typename alloc_traits::template rebind_alloc<std::pair<const key_type, mapped_type>>;
43
44public:
46 using allocator_type = Allocator;
48 using size_type = std::size_t;
49
53
59 : handlers{allocator, allocator} {}
60
62 emitter(const emitter &) = delete;
63
69 : handlers{std::move(other.handlers)} {}
70
77 : handlers{container_type{std::move(other.handlers.first()), allocator}, allocator} {
78 ENTT_ASSERT(alloc_traits::is_always_equal::value || handlers.second() == other.handlers.second(), "Copying an emitter is not allowed");
79 }
80
82 virtual ~emitter() {
83 static_assert(std::is_base_of_v<emitter<Derived, Allocator>, Derived>, "Invalid emitter type");
84 }
85
90 emitter &operator=(const emitter &) = delete;
91
98 ENTT_ASSERT(alloc_traits::is_always_equal::value || handlers.second() == other.handlers.second(), "Copying an emitter is not allowed");
99 swap(other);
100 return *this;
101 }
102
107 void swap(emitter &other) noexcept {
108 using std::swap;
109 swap(handlers, other.handlers);
110 }
111
117 return handlers.second();
118 }
119
125 template<typename Type>
126 void publish(Type &&value) {
127 if(const auto id = type_id<Type>().hash(); handlers.first().contains(id)) {
128 handlers.first()[id](&value);
129 }
130 }
131
137 template<typename Type>
138 void on(std::function<void(Type &, Derived &)> func) {
139 handlers.first().insert_or_assign(type_id<Type>().hash(), [func = std::move(func), this](void *value) {
140 func(*static_cast<Type *>(value), static_cast<Derived &>(*this));
141 });
142 }
143
148 template<typename Type>
149 void erase() {
150 handlers.first().erase(type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value());
151 }
152
155 handlers.first().clear();
156 }
157
163 template<typename Type>
164 [[nodiscard]] bool contains() const {
165 return handlers.first().contains(type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value());
166 }
167
173 return handlers.first().empty();
174 }
175
176private:
178};
179
180} // namespace entt
181
182#endif
A compressed pair.
constexpr second_type & second() noexcept
Returns the second element that a pair stores.
constexpr first_type & first() noexcept
Returns the first element that a pair stores.
Associative container for key-value pairs with unique keys.
General purpose event emitter.
Definition emitter.hpp:36
emitter & operator=(const emitter &)=delete
Default copy assignment operator, deleted on purpose.
Allocator allocator_type
Allocator type.
Definition emitter.hpp:46
emitter(emitter &&other) noexcept
Move constructor.
Definition emitter.hpp:68
emitter & operator=(emitter &&other) noexcept
Move assignment operator.
Definition emitter.hpp:97
emitter(const allocator_type &allocator)
Constructs an emitter with a given allocator.
Definition emitter.hpp:58
void clear() noexcept
Disconnects all the listeners.
Definition emitter.hpp:154
emitter()
Default constructor.
Definition emitter.hpp:51
bool contains() const
Checks if there are listeners registered for the specific event.
Definition emitter.hpp:164
void publish(Type &&value)
Publishes a given event.
Definition emitter.hpp:126
void erase()
Disconnects a listener from the event emitter.
Definition emitter.hpp:149
std::size_t size_type
Unsigned integer type.
Definition emitter.hpp:48
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:107
bool empty() const noexcept
Checks if there are listeners registered with the event emitter.
Definition emitter.hpp:172
void on(std::function< void(Type &, Derived &)> func)
Registers a listener with the event emitter.
Definition emitter.hpp:138
emitter(emitter &&other, const allocator_type &allocator)
Allocator-extended move constructor.
Definition emitter.hpp:76
virtual ~emitter()
Default destructor.
Definition emitter.hpp:82
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition emitter.hpp:116
EnTT default namespace.
Definition dense_map.hpp:22
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219
std::uint32_t id_type
Alias declaration for type identifiers.
Definition fwd.hpp:14
Type hash.
Definition type_info.hpp:92