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>;
69 : handlers{std::move(
other.handlers)} {}
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");
125 template<
typename Type>
128 handlers.
first()[id](&value);
137 template<
typename Type>
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());
173 return handlers.
first().empty();
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.
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 publish(Type &&value)
Publishes a given 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.
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
std::uint32_t id_type
Alias declaration for type identifiers.