From ab1f92a1eea2556f9ee6dd81f343c5b61f1b781c Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 7 Jul 2018 21:09:43 +0900 Subject: visit<>(): Add an overload accepting a default value --- lib/events/event.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index 8fed0082..cd49606f 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -285,12 +285,28 @@ namespace QMatrixClient inline bool isUnknown(const Event& e) { return e.type() == unknownEventTypeId(); } + template + inline auto visit(const Event& event, FnT visitor, + DefaultT&& defaultValue) + -> std::enable_if_t< + std::is_convertible>::value, + fn_return_t> + { + using event_type = fn_arg_t; + if (is(event)) + return visitor(static_cast(event)); + return std::forward>(defaultValue); + } + template inline fn_return_t visit(const Event& event, FnT visitor) { using event_type = fn_arg_t; if (is(event)) return visitor(static_cast(event)); + // Cannot define in terms of the previous overload because + // fn_return_t may be void and void() is not a valid default + // parameter value. return fn_return_t(); } @@ -312,7 +328,6 @@ namespace QMatrixClient return visit(*eptr, visitors...); return return_type(); } - } // namespace QMatrixClient Q_DECLARE_METATYPE(QMatrixClient::Event*) Q_DECLARE_METATYPE(const QMatrixClient::Event*) -- cgit v1.2.3