From 72dd1eb7c1986c23a7630205e2f2a0280b7c2a2b Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 29 Sep 2019 18:03:31 +0900 Subject: Simplify visit() signature Checking that BaseEventT descends from Event is really extraneous. --- lib/events/event.h | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/events/event.h b/lib/events/event.h index 25362786..f985ae92 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -334,22 +334,18 @@ inline auto visit(const BaseEventT& event, FnT&& visitor) return visitor(event); } -template -constexpr auto is_event() -{ - return std::is_base_of>::value; -} - -template -constexpr auto needs_cast() -{ - return !std::is_convertible>::value; +namespace _impl { + template + constexpr auto needs_downcast() + { + return !std::is_convertible_v>; + } } // A single type-specific void visitor template -inline std::enable_if_t() && needs_cast() - && std::is_void>::value> +inline std::enable_if_t<_impl::needs_downcast() + && std::is_void_v>> visit(const BaseEventT& event, FnT&& visitor) { using event_type = fn_arg_t; @@ -358,10 +354,9 @@ 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() && needs_cast(), - fn_return_t> // non-voidness is guarded by - // defaultValue type +inline std::enable_if_t<_impl::needs_downcast(), fn_return_t> visit(const BaseEventT& event, FnT&& visitor, fn_return_t&& defaultValue = {}) { @@ -373,9 +368,8 @@ visit(const BaseEventT& event, FnT&& visitor, // A chain of 2 or more visitors template -inline std::enable_if_t(), fn_return_t> -visit(const BaseEventT& event, FnT1&& visitor1, FnT2&& visitor2, - FnTs&&... visitors) +inline fn_return_t visit(const BaseEventT& event, FnT1&& visitor1, + FnT2&& visitor2, FnTs&&... visitors) { using event_type1 = fn_arg_t; if (is>(event)) -- cgit v1.2.3