1#ifndef ENTT_SIGNAL_EMITTER_HPP
2#define ENTT_SIGNAL_EMITTER_HPP
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"
34template<
typename Derived,
typename Allocator>
37 using mapped_type = stl::function<void(
void *)>;
39 using alloc_traits = stl::allocator_traits<Allocator>;
40 using container_allocator = alloc_traits::template rebind_alloc<stl::pair<const key_type, mapped_type>>;
58 : handlers{allocator, allocator} {}
68 : handlers{stl::move(other.handlers)} {}
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");
82 static_assert(stl::is_base_of_v<emitter<Derived, Allocator>, Derived>,
"Invalid emitter type");
97 ENTT_ASSERT(alloc_traits::is_always_equal::value || handlers.second() == other.handlers.second(),
"Copying an emitter is not allowed");
108 swap(handlers, other.handlers);
116 return handlers.second();
124 template<
typename Type>
127 handlers.first()[id](&value);
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));
147 template<
typename Type>
149 handlers.first().erase(
type_hash<stl::remove_cvref_t<Type>>::value());
154 handlers.first().clear();
162 template<
typename Type>
164 return handlers.first().contains(
type_hash<stl::remove_cvref_t<Type>>::value());
171 [[nodiscard]]
bool empty() const noexcept {
172 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.
void on(stl::function< void(Type &, Derived &)> func)
Registers a listener with the event emitter.
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.
stl::size_t size_type
Unsigned integer type.
void erase()
Disconnects a listener from the event emitter.
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.
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.
Custom EnTT namespace for the standard template library.
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.