From 639f1d482633a7adb72164c56e3b5ea429db96a5 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 27 Dec 2020 23:19:12 +0100 Subject: event.h: Minor tweaks around visit<> --- lib/events/event.h | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/events/event.h b/lib/events/event.h index 626a0229..309ebddf 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -298,7 +298,7 @@ using Events = EventsArray; // === is<>(), eventCast<>() and visit<>() === -template +template inline bool is(const Event& e) { return e.type() == typeId(); @@ -309,7 +309,7 @@ inline bool isUnknown(const Event& e) return e.type() == unknownEventTypeId(); } -template +template inline auto eventCast(const BasePtrT& eptr) -> decltype(static_cast(&*eptr)) { @@ -319,7 +319,7 @@ inline auto eventCast(const BasePtrT& eptr) } // A single generic catch-all visitor -template +template inline auto visit(const BaseEventT& event, FnT&& visitor) -> decltype(visitor(event)) { @@ -327,18 +327,17 @@ inline auto visit(const BaseEventT& event, FnT&& visitor) } namespace _impl { - template - constexpr auto needs_downcast() - { - return !std::is_convertible_v>; - } + template + inline constexpr auto needs_downcast = + std::is_base_of_v>> + && !std::is_same_v>>; } // A single type-specific void visitor -template -inline std::enable_if_t<_impl::needs_downcast() +template +inline auto visit(const BaseT& event, FnT&& visitor) + -> std::enable_if_t<_impl::needs_downcast && std::is_void_v>> -visit(const BaseEventT& event, FnT&& visitor) { using event_type = fn_arg_t; if (is>(event)) @@ -347,10 +346,10 @@ visit(const BaseEventT& event, FnT&& visitor) // A single type-specific non-void visitor with an optional default value // non-voidness is guarded by defaultValue type -template -inline std::enable_if_t<_impl::needs_downcast(), fn_return_t> -visit(const BaseEventT& event, FnT&& visitor, - fn_return_t&& defaultValue = {}) +template +inline auto visit(const BaseT& event, FnT&& visitor, + fn_return_t&& defaultValue = {}) + -> std::enable_if_t<_impl::needs_downcast, fn_return_t> { using event_type = fn_arg_t; if (is>(event)) @@ -359,9 +358,10 @@ visit(const BaseEventT& event, FnT&& visitor, } // A chain of 2 or more visitors -template -inline fn_return_t visit(const BaseEventT& event, FnT1&& visitor1, - FnT2&& visitor2, FnTs&&... visitors) +template +inline std::common_type_t, fn_return_t> visit( + const BaseT& event, FnT1&& visitor1, FnT2&& visitor2, + FnTs&&... visitors) { using event_type1 = fn_arg_t; if (is>(event)) @@ -374,8 +374,8 @@ inline fn_return_t visit(const BaseEventT& event, FnT1&& visitor1, // over a range of event pointers template inline auto visitEach(RangeT&& events, FnTs&&... visitors) - -> std::enable_if_t, Event>> + -> std::enable_if_t(visitors)...))>> { for (auto&& evtPtr: events) visit(*evtPtr, std::forward(visitors)...); -- cgit v1.2.3