diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-07 21:09:43 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-07 21:09:43 +0900 |
commit | ab1f92a1eea2556f9ee6dd81f343c5b61f1b781c (patch) | |
tree | 365c9168ba24f53d9fb8abf26047dbdc0b910d69 /lib | |
parent | a80e4a675e21a1401fabfeecaf7e538227aa9dbd (diff) | |
download | libquotient-ab1f92a1eea2556f9ee6dd81f343c5b61f1b781c.tar.gz libquotient-ab1f92a1eea2556f9ee6dd81f343c5b61f1b781c.zip |
visit<>(): Add an overload accepting a default value
Diffstat (limited to 'lib')
-rw-r--r-- | lib/events/event.h | 17 |
1 files changed, 16 insertions, 1 deletions
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 <typename FnT, typename DefaultT> + inline auto visit(const Event& event, FnT visitor, + DefaultT&& defaultValue) + -> std::enable_if_t< + std::is_convertible<DefaultT, fn_return_t<FnT>>::value, + fn_return_t<FnT>> + { + using event_type = fn_arg_t<FnT>; + if (is<event_type>(event)) + return visitor(static_cast<event_type>(event)); + return std::forward<fn_return_t<FnT>>(defaultValue); + } + template <typename FnT> inline fn_return_t<FnT> visit(const Event& event, FnT visitor) { using event_type = fn_arg_t<FnT>; if (is<event_type>(event)) return visitor(static_cast<event_type>(event)); + // Cannot define in terms of the previous overload because + // fn_return_t<FnT> may be void and void() is not a valid default + // parameter value. return fn_return_t<FnT>(); } @@ -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*) |