1#ifndef ENTT_SIGNAL_EMITTER_HPP
2#define ENTT_SIGNAL_EMITTER_HPP
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"
35template<
typename Derived,
typename Allocator>
38 using mapped_type = std::function<void(
void *)>;
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>>;
59 : handlers{allocator, allocator} {}
69 : handlers{std::move(other.handlers)} {}
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");
83 static_assert(std::is_base_of_v<emitter<Derived, Allocator>, Derived>,
"Invalid emitter type");
98 ENTT_ASSERT(alloc_traits::is_always_equal::value || handlers.second() == other.handlers.second(),
"Copying an emitter is not allowed");
109 swap(handlers, other.handlers);
117 return handlers.second();
125 template<
typename Type>
128 handlers.first()[id](&value);
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));
148 template<
typename Type>
150 handlers.first().erase(
type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value());
155 handlers.first().clear();
163 template<
typename Type>
165 return handlers.first().contains(
type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value());
172 [[nodiscard]]
bool empty() const noexcept {
173 return handlers.first().empty();
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.
emitter(emitter &&other) noexcept
Move constructor.
emitter & operator=(emitter &&other) noexcept
Move assignment operator.
emitter(const allocator_type &allocator)
Constructs an emitter with a given allocator.
void clear() noexcept
Disconnects all the listeners.
emitter()
Default constructor.
bool contains() const
Checks if there are listeners registered for the specific event.
void erase()
Disconnects a listener from the event emitter.
std::size_t size_type
Unsigned integer type.
emitter(const emitter &)=delete
Default copy constructor, deleted on purpose.
void swap(emitter &other) noexcept
Exchanges the contents with those of a given emitter.
bool empty() const noexcept
Checks if there are listeners registered with the event emitter.
void on(std::function< void(Type &, Derived &)> func)
Registers a listener with the event emitter.
emitter(emitter &&other, const allocator_type &allocator)
Allocator-extended move constructor.
virtual ~emitter()
Default destructor.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
void publish(Type value)
Publishes a given event.
std::uint32_t id_type
Alias declaration for type identifiers.
const type_info & type_id() noexcept
Returns the type info object associated to a given type.