1#ifndef ENTT_SIGNAL_DISPATCHER_HPP
2#define ENTT_SIGNAL_DISPATCHER_HPP
10#include "../container/dense_map.hpp"
11#include "../core/compressed_pair.hpp"
12#include "../core/fwd.hpp"
13#include "../core/type_info.hpp"
14#include "../core/utility.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 virtual std::
size_t size() const noexcept = 0;
31template<typename Type, typename Allocator>
32class dispatcher_handler final: public basic_dispatcher_handler {
33 static_assert(std::is_same_v<Type, std::decay_t<Type>>,
"Invalid type");
35 using alloc_traits = std::allocator_traits<Allocator>;
36 using signal_type = sigh<void(Type &), Allocator>;
37 using container_type = std::vector<Type, typename alloc_traits::template rebind_alloc<Type>>;
40 using allocator_type = Allocator;
42 dispatcher_handler(
const allocator_type &allocator)
46 void publish()
override {
47 const auto length = events.size();
49 for(std::size_t pos{}; pos < length; ++pos) {
50 signal.publish(events[pos]);
53 events.erase(events.cbegin(), events.cbegin() + length);
56 void disconnect(
void *instance)
override {
57 bucket().disconnect(instance);
60 void clear() noexcept
override {
64 [[nodiscard]]
auto bucket() noexcept {
65 return typename signal_type::sink_type{signal};
68 void trigger(Type event) {
69 signal.publish(event);
72 template<
typename... Args>
73 void enqueue(Args &&...args) {
74 if constexpr(std::is_aggregate_v<Type> && (
sizeof...(Args) != 0u || !std::is_default_constructible_v<Type>)) {
75 events.push_back(Type{std::forward<Args>(args)...});
77 events.emplace_back(std::forward<Args>(args)...);
81 [[nodiscard]] std::size_t size() const noexcept
override {
87 container_type events;
107template<
typename Allocator>
109 template<
typename Type>
110 using handler_type = internal::dispatcher_handler<Type, Allocator>;
114 using mapped_type = std::shared_ptr<internal::basic_dispatcher_handler>;
116 using alloc_traits = std::allocator_traits<Allocator>;
117 using container_allocator =
typename alloc_traits::template rebind_alloc<std::pair<const key_type, mapped_type>>;
120 template<
typename Type>
121 [[nodiscard]] handler_type<Type> &assure(
const id_type id) {
122 static_assert(std::is_same_v<Type, std::decay_t<Type>>,
"Non-decayed types not allowed");
123 auto &&ptr = pools.first()[id];
126 const auto &allocator = get_allocator();
127 ptr = std::allocate_shared<handler_type<Type>>(allocator, allocator);
130 return static_cast<handler_type<Type> &
>(*ptr);
133 template<
typename Type>
134 [[nodiscard]]
const handler_type<Type> *assure(
const id_type id)
const {
135 static_assert(std::is_same_v<Type, std::decay_t<Type>>,
"Non-decayed types not allowed");
137 if(
auto it = pools.first().find(
id); it != pools.first().cend()) {
138 return static_cast<const handler_type<Type> *
>(it->second.get());
159 : pools{allocator, allocator} {}
166 : pools{std::move(other.pools)} {}
174 : pools{
container_type{std::move(other.pools.first()), allocator}, allocator} {
175 ENTT_ASSERT(alloc_traits::is_always_equal::value || pools.second() == other.pools.second(),
"Copying a dispatcher is not allowed");
184 ENTT_ASSERT(alloc_traits::is_always_equal::value || pools.second() == other.pools.second(),
"Copying a dispatcher is not allowed");
185 pools = std::move(other.pools);
195 swap(pools, other.pools);
203 return pools.second();
212 template<
typename Type>
214 const auto *cpool = assure<std::decay_t<Type>>(id);
215 return cpool ? cpool->size() : 0u;
225 for(
auto &&cpool: pools.first()) {
226 count += cpool.second->size();
251 template<
typename Type>
253 return assure<Type>(
id).bucket();
261 template<
typename Type>
263 trigger(
type_hash<std::decay_t<Type>>::value(), std::forward<Type>(value));
272 template<
typename Type>
274 assure<std::decay_t<Type>>(id).trigger(std::forward<Type>(value));
283 template<
typename Type,
typename... Args>
293 template<
typename Type>
295 enqueue_hint(
type_hash<std::decay_t<Type>>::value(), std::forward<Type>(value));
305 template<
typename Type,
typename... Args>
307 assure<Type>(
id).enqueue(std::forward<Args>(args)...);
316 template<
typename Type>
318 assure<std::decay_t<Type>>(id).enqueue(std::forward<Type>(value));
327 template<
typename Type>
329 disconnect(&value_or_instance);
338 template<
typename Type>
340 for(
auto &&cpool: pools.first()) {
341 cpool.second->disconnect(value_or_instance);
350 template<
typename Type>
352 assure<Type>(
id).clear();
357 for(
auto &&cpool: pools.first()) {
358 cpool.second->clear();
367 template<
typename Type>
369 assure<Type>(
id).publish();
374 for(
auto &&cpool: pools.first()) {
375 cpool.second->publish();
Basic dispatcher implementation.
void clear(const id_type id=type_hash< Type >::value())
Discards all the events stored so far in a given queue.
void swap(basic_dispatcher &other)
Exchanges the contents with those of a given dispatcher.
void trigger(Type &&value={})
Triggers an immediate event of a given type.
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.
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.
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)
Enqueues an event of the given type.
size_type size() const noexcept
Returns the total number of pending events.
std::size_t size_type
Unsigned integer type.
void disconnect(Type &value_or_instance)
Utility function to disconnect everything related to a given value or instance from a dispatcher.
basic_dispatcher(basic_dispatcher &&other, const allocator_type &allocator) noexcept
Allocator-extended move constructor.
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.
void trigger(const id_type id, Type &&value={})
Triggers an immediate event on a queue of a given type.
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.
std::uint32_t id_type
Alias declaration for type identifiers.
constexpr void swap(compressed_pair< First, Second > &lhs, compressed_pair< First, Second > &rhs)
Swaps two compressed pair objects.