1#ifndef ENTT_SIGNAL_DISPATCHER_HPP
2#define ENTT_SIGNAL_DISPATCHER_HPP
4#include "../container/dense_map.hpp"
5#include "../core/compressed_pair.hpp"
6#include "../core/concepts.hpp"
7#include "../core/fwd.hpp"
8#include "../core/type_info.hpp"
9#include "../stl/cstddef.hpp"
10#include "../stl/functional.hpp"
11#include "../stl/memory.hpp"
12#include "../stl/type_traits.hpp"
13#include "../stl/utility.hpp"
14#include "../stl/vector.hpp"
23struct basic_dispatcher_handler {
24 virtual ~basic_dispatcher_handler() =
default;
25 virtual void publish() = 0;
26 virtual void disconnect(
void *) = 0;
27 virtual void clear() noexcept = 0;
28 [[nodiscard]] virtual stl::
size_t size() const noexcept = 0;
31template<cvref_unqualified Type, typename Allocator>
32class dispatcher_handler final: public basic_dispatcher_handler {
33 using alloc_traits = stl::allocator_traits<Allocator>;
34 using signal_type = sigh<void(Type &), Allocator>;
35 using container_type = stl::vector<Type, typename alloc_traits::template rebind_alloc<Type>>;
38 using allocator_type = Allocator;
40 dispatcher_handler(
const allocator_type &allocator)
44 void publish()
override {
45 container_type other{};
48 for(
auto &&elem: other) {
53 void disconnect(
void *instance)
override {
54 bucket().disconnect(instance);
57 void clear() noexcept
override {
61 [[nodiscard]]
auto bucket() noexcept {
62 return typename signal_type::sink_type{signal};
65 void trigger(Type &event) {
66 signal.publish(event);
69 template<
typename... Args>
70 void enqueue(Args &&...args) {
71 if constexpr(stl::is_aggregate_v<Type> && (
sizeof...(Args) != 0u || !stl::is_default_constructible_v<Type>)) {
72 events.push_back(Type{stl::forward<Args>(args)...});
74 events.emplace_back(stl::forward<Args>(args)...);
78 [[nodiscard]] stl::size_t size() const noexcept
override {
84 container_type events;
104template<
typename Allocator>
106 template<
typename Type>
107 using handler_type = internal::dispatcher_handler<Type, Allocator>;
111 using mapped_type = stl::shared_ptr<internal::basic_dispatcher_handler>;
113 using alloc_traits = stl::allocator_traits<Allocator>;
114 using container_allocator = alloc_traits::template rebind_alloc<stl::pair<const key_type, mapped_type>>;
117 template<cvref_unqualified Type>
118 [[nodiscard]] handler_type<Type> &assure(
const id_type id) {
119 auto &&ptr = pools.first()[id];
123 ptr = stl::allocate_shared<handler_type<Type>>(allocator, allocator);
126 return static_cast<handler_type<Type> &
>(*ptr);
129 template<cvref_unqualified Type>
130 [[nodiscard]]
const handler_type<Type> *assure(
const id_type id)
const {
131 if(
auto it = pools.first().find(
id); it != pools.first().cend()) {
132 return static_cast<const handler_type<Type> *
>(it->second.get());
153 : pools{allocator, allocator} {}
163 : pools{stl::move(other.pools)} {}
171 : pools{container_type{
stl::move(other.pools.first()), allocator}, allocator} {
172 ENTT_ASSERT(alloc_traits::is_always_equal::value ||
get_allocator() == other.get_allocator(),
"Copying a dispatcher is not allowed");
190 ENTT_ASSERT(alloc_traits::is_always_equal::value ||
get_allocator() == other.get_allocator(),
"Copying a dispatcher is not allowed");
201 swap(pools, other.pools);
209 return pools.second();
218 template<
typename Type>
220 const auto *cpool = assure<stl::decay_t<Type>>(id);
221 return cpool ? cpool->size() : 0u;
231 for(
auto &&cpool: pools.first()) {
232 count += cpool.second->size();
257 template<
typename Type>
259 return assure<Type>(
id).bucket();
267 template<
typename Type>
278 template<
typename Type>
280 assure<stl::decay_t<Type>>(id).
trigger(value);
289 template<
typename Type,
typename... Args>
299 template<
typename Type>
311 template<
typename Type,
typename... Args>
313 assure<Type>(
id).enqueue(stl::forward<Args>(args)...);
322 template<
typename Type>
324 assure<stl::decay_t<Type>>(id).
enqueue(stl::forward<Type>(value));
333 template<
typename Type>
344 template<
typename Type>
346 for(
auto &&cpool: pools.first()) {
347 cpool.second->disconnect(value_or_instance);
356 template<
typename Type>
358 assure<Type>(
id).clear();
363 for(
auto &&cpool: pools.first()) {
364 cpool.second->clear();
373 template<
typename Type>
375 assure<Type>(
id).publish();
380 for(
auto &&cpool: pools.first()) {
381 cpool.second->publish();
void clear(const id_type id=type_hash< Type >::value())
Discards all the events stored so far in a given queue.
stl::size_t size_type
Unsigned integer type.
void swap(basic_dispatcher &other) noexcept
void clear() noexcept
Discards all the events queued so far.
void enqueue_hint(const id_type id, Type &&value)
Enqueues an event of the given type.
void trigger(Type value)
Triggers an immediate event of a given type.
Allocator allocator_type
Allocator type.
auto sink(const id_type id=type_hash< Type >::value())
Returns a sink object for the given event and queue.
basic_dispatcher(basic_dispatcher &&other, const allocator_type &allocator)
Allocator-extended move constructor.
void update() const
Delivers all the pending events.
void enqueue(Type &&value)
Enqueues an event of the given type.
void enqueue_hint(const id_type id, Args &&...args)
size_type size() const noexcept
Returns the total number of pending events.
void trigger(const id_type id, Type value)
Triggers an immediate event on a queue of a given type.
void disconnect(Type &value_or_instance)
Utility function to disconnect everything related to a given value or instance from a dispatcher.
void update(const id_type id=type_hash< Type >::value())
Delivers all the pending events of a given queue.
size_type size(const id_type id=type_hash< Type >::value()) const noexcept
Returns the number of pending events for a given type.
basic_dispatcher(basic_dispatcher &&other) noexcept
Move constructor.
basic_dispatcher & operator=(const basic_dispatcher &)=delete
Default copy assignment operator, deleted on purpose.
~basic_dispatcher()=default
Default destructor.
basic_dispatcher(const basic_dispatcher &)=delete
Default copy constructor, deleted on purpose.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
basic_dispatcher()
Default constructor.
basic_dispatcher(const allocator_type &allocator)
Constructs a dispatcher with a given allocator.
void disconnect(Type *value_or_instance)
Utility function to disconnect everything related to a given value or instance from a dispatcher.
basic_dispatcher & operator=(basic_dispatcher &&other) noexcept
Move assignment operator.
void enqueue(Args &&...args)
Enqueues an event of the given type.
Associative container for key-value pairs with unique keys.
Custom EnTT namespace for the standard template library.
stl::uint32_t id_type
Alias declaration for type identifiers.
static constexpr id_type value() noexcept
Returns the numeric representation of a given type.