dispatcher: minor changes

This commit is contained in:
Michele Caini
2022-04-08 23:53:07 +02:00
parent b982817a56
commit 50faed4a50

View File

@@ -33,13 +33,13 @@ struct basic_dispatcher_handler {
virtual std::size_t size() const ENTT_NOEXCEPT = 0;
};
template<typename Event, typename Allocator>
template<typename Type, typename Allocator>
class dispatcher_handler final: public basic_dispatcher_handler {
static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
static_assert(std::is_same_v<Type, std::decay_t<Type>>, "Invalid type");
using alloc_traits = std::allocator_traits<Allocator>;
using signal_type = sigh<void(Event &), typename alloc_traits::template rebind_alloc<void (*)(Event &)>>;
using container_type = std::vector<Event, typename alloc_traits::template rebind_alloc<Event>>;
using signal_type = sigh<void(Type &), typename alloc_traits::template rebind_alloc<void (*)(Type &)>>;
using container_type = std::vector<Type, typename alloc_traits::template rebind_alloc<Type>>;
public:
using allocator_type = Allocator;
@@ -67,18 +67,18 @@ public:
}
[[nodiscard]] auto bucket() ENTT_NOEXCEPT {
using sink_type = typename sigh<void(Event &)>::sink_type;
using sink_type = typename sigh<void(Type &)>::sink_type;
return sink_type{signal};
}
void trigger(Event event) {
void trigger(Type event) {
signal.publish(event);
}
template<typename... Args>
void enqueue(Args &&...args) {
if constexpr(std::is_aggregate_v<Event>) {
events.push_back(Event{std::forward<Args>(args)...});
if constexpr(std::is_aggregate_v<Type>) {
events.push_back(Type{std::forward<Args>(args)...});
} else {
events.emplace_back(std::forward<Args>(args)...);
}
@@ -106,8 +106,8 @@ private:
* A dispatcher can be used either to trigger an immediate event or to enqueue
* events to be published all together once per tick.<br/>
* Listeners are provided in the form of member functions. For each event of
* type `Event`, listeners are such that they can be invoked with an argument of
* type `Event &`, no matter what the return type is.
* type `Type`, listeners are such that they can be invoked with an argument of
* type `Type &`, no matter what the return type is.
*
* The dispatcher creates instances of the `sigh` class internally. Refer to the
* documentation of the latter for more details.
@@ -116,8 +116,8 @@ private:
*/
template<typename Allocator>
class basic_dispatcher {
template<typename Event>
using handler_type = internal::dispatcher_handler<Event, Allocator>;
template<typename Type>
using handler_type = internal::dispatcher_handler<Type, Allocator>;
using key_type = id_type;
// std::shared_ptr because of its type erased allocator which is pretty useful here
@@ -127,24 +127,24 @@ class basic_dispatcher {
using container_allocator = typename alloc_traits::template rebind_alloc<std::pair<const key_type, mapped_type>>;
using container_type = dense_map<id_type, mapped_type, identity, std::equal_to<id_type>, container_allocator>;
template<typename Event>
[[nodiscard]] handler_type<Event> &assure(const id_type id) {
template<typename Type>
[[nodiscard]] handler_type<Type> &assure(const id_type id) {
auto &&ptr = pools.first()[id];
if(!ptr) {
const auto &allocator = pools.second();
ptr = std::allocate_shared<handler_type<Event>>(allocator, allocator);
ptr = std::allocate_shared<handler_type<Type>>(allocator, allocator);
}
return static_cast<handler_type<Event> &>(*ptr);
return static_cast<handler_type<Type> &>(*ptr);
}
template<typename Event>
[[nodiscard]] const handler_type<Event> *assure(const id_type id) const {
template<typename Type>
[[nodiscard]] const handler_type<Type> *assure(const id_type id) const {
auto &container = pools.first();
if(const auto it = container.find(id); it != container.end()) {
return static_cast<const handler_type<Event> *>(it->second.get());
return static_cast<const handler_type<Type> *>(it->second.get());
}
return nullptr;
@@ -211,13 +211,13 @@ public:
/**
* @brief Returns the number of pending events for a given type.
* @tparam Event Type of event for which to return the count.
* @tparam Type Type of event for which to return the count.
* @param id Name used to map the event queue within the dispatcher.
* @return The number of pending events for the given type.
*/
template<typename Event>
size_type size(const id_type id = type_hash<Event>::value()) const ENTT_NOEXCEPT {
const auto *cpool = assure<Event>(id);
template<typename Type>
size_type size(const id_type id = type_hash<Type>::value()) const ENTT_NOEXCEPT {
const auto *cpool = assure<Type>(id);
return cpool ? cpool->size() : 0u;
}
@@ -241,86 +241,87 @@ public:
* A sink is an opaque object used to connect listeners to events.
*
* The function type for a listener is _compatible_ with:
*
* @code{.cpp}
* void(Event &);
* void(Type &);
* @endcode
*
* The order of invocation of the listeners isn't guaranteed.
*
* @sa sink
*
* @tparam Event Type of event of which to get the sink.
* @tparam Type Type of event of which to get the sink.
* @param id Name used to map the event queue within the dispatcher.
* @return A temporary sink object.
*/
template<typename Event>
[[nodiscard]] auto sink(const id_type id = type_hash<Event>::value()) {
return assure<Event>(id).bucket();
template<typename Type>
[[nodiscard]] auto sink(const id_type id = type_hash<Type>::value()) {
return assure<Type>(id).bucket();
}
/**
* @brief Triggers an immediate event of a given type.
* @tparam Event Type of event to trigger.
* @param event An instance of the given type of event.
* @tparam Type Type of event to trigger.
* @param value An instance of the given type of event.
*/
template<typename Event>
void trigger(Event &&event = {}) {
trigger(type_hash<std::decay_t<Event>>::value(), std::forward<Event>(event));
template<typename Type>
void trigger(Type &&value = {}) {
trigger(type_hash<std::decay_t<Type>>::value(), std::forward<Type>(value));
}
/**
* @brief Triggers an immediate event on a queue of a given type.
* @tparam Event Type of event to trigger.
* @param event An instance of the given type of event.
* @tparam Type Type of event to trigger.
* @param value An instance of the given type of event.
* @param id Name used to map the event queue within the dispatcher.
*/
template<typename Event>
void trigger(const id_type id, Event &&event = {}) {
assure<std::decay_t<Event>>(id).trigger(std::forward<Event>(event));
template<typename Type>
void trigger(const id_type id, Type &&value = {}) {
assure<std::decay_t<Type>>(id).trigger(std::forward<Type>(value));
}
/**
* @brief Enqueues an event of the given type.
* @tparam Event Type of event to enqueue.
* @tparam Type Type of event to enqueue.
* @tparam Args Types of arguments to use to construct the event.
* @param args Arguments to use to construct the event.
*/
template<typename Event, typename... Args>
template<typename Type, typename... Args>
void enqueue(Args &&...args) {
enqueue_hint<Event>(type_hash<Event>::value(), std::forward<Args>(args)...);
enqueue_hint<Type>(type_hash<Type>::value(), std::forward<Args>(args)...);
}
/**
* @brief Enqueues an event of the given type.
* @tparam Event Type of event to enqueue.
* @param event An instance of the given type of event.
* @tparam Type Type of event to enqueue.
* @param value An instance of the given type of event.
*/
template<typename Event>
void enqueue(Event &&event) {
enqueue_hint(type_hash<std::decay_t<Event>>::value(), std::forward<Event>(event));
template<typename Type>
void enqueue(Type &&value) {
enqueue_hint(type_hash<std::decay_t<Type>>::value(), std::forward<Type>(value));
}
/**
* @brief Enqueues an event of the given type.
* @tparam Event Type of event to enqueue.
* @tparam Type Type of event to enqueue.
* @tparam Args Types of arguments to use to construct the event.
* @param id Name used to map the event queue within the dispatcher.
* @param args Arguments to use to construct the event.
*/
template<typename Event, typename... Args>
template<typename Type, typename... Args>
void enqueue_hint(const id_type id, Args &&...args) {
assure<Event>(id).enqueue(std::forward<Args>(args)...);
assure<Type>(id).enqueue(std::forward<Args>(args)...);
}
/**
* @brief Enqueues an event of the given type.
* @tparam Event Type of event to enqueue.
* @tparam Type Type of event to enqueue.
* @param id Name used to map the event queue within the dispatcher.
* @param event An instance of the given type of event.
* @param value An instance of the given type of event.
*/
template<typename Event>
void enqueue_hint(const id_type id, Event &&event) {
assure<std::decay_t<Event>>(id).enqueue(std::forward<Event>(event));
template<typename Type>
void enqueue_hint(const id_type id, Type &&value) {
assure<std::decay_t<Type>>(id).enqueue(std::forward<Type>(value));
}
/**
@@ -349,12 +350,12 @@ public:
/**
* @brief Discards all the events stored so far in a given queue.
* @tparam Event Type of event to discard.
* @tparam Type Type of event to discard.
* @param id Name used to map the event queue within the dispatcher.
*/
template<typename Event>
void clear(const id_type id = type_hash<Event>::value()) {
assure<Event>(id).clear();
template<typename Type>
void clear(const id_type id = type_hash<Type>::value()) {
assure<Type>(id).clear();
}
/*! @brief Discards all the events queued so far. */
@@ -366,12 +367,12 @@ public:
/**
* @brief Delivers all the pending events of a given queue.
* @tparam Event Type of event to send.
* @tparam Type Type of event to send.
* @param id Name used to map the event queue within the dispatcher.
*/
template<typename Event>
void update(const id_type id = type_hash<Event>::value()) {
assure<Event>(id).publish();
template<typename Type>
void update(const id_type id = type_hash<Type>::value()) {
assure<Type>(id).publish();
}
/*! @brief Delivers all the pending events. */