aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-07-08 09:05:27 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-07-08 09:05:27 +0900
commitc0c1f47926cb01ddcfe9ed6556af2623d462b770 (patch)
tree37af85130d472d2d55f2c3d8b9002b367095a11d /lib/events
parent0621efc37355d1986800e40f367d918bf859c5cd (diff)
downloadlibquotient-c0c1f47926cb01ddcfe9ed6556af2623d462b770.tar.gz
libquotient-c0c1f47926cb01ddcfe9ed6556af2623d462b770.zip
eventCast<>()
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/event.h24
1 files changed, 20 insertions, 4 deletions
diff --git a/lib/events/event.h b/lib/events/event.h
index 5aa78dbb..b188a40b 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -273,20 +273,36 @@ namespace QMatrixClient
#define DEFINE_EVENTTYPE_ALIAS(_Id, _Type) \
namespace EventType \
{ \
- [[deprecated("Use typeId<>(), is<>() or visit<>()")]] \
+ [[deprecated("Use is<>(), eventCast<>() or visit<>()")]] \
static const auto _Id = typeId<_Type>(); \
} \
// End of macro
- // === is<>() and visit<>() ===
+ // === is<>(), eventCast<>() and visit<>() ===
template <typename EventT>
inline bool is(const Event& e) { return e.type() == typeId<EventT>(); }
inline bool isUnknown(const Event& e) { return e.type() == unknownEventTypeId(); }
- template <typename FnT, typename DefaultT>
- inline auto visit(const Event& event, FnT visitor,
+ template <typename EventT, typename BaseEventT>
+ inline Omittable<EventT> eventCast(BaseEventT&& e)
+ {
+ if (is<EventT>(e))
+ return static_cast<EventT>(e);
+ return none;
+ }
+
+ template <typename EventT, typename PtrT>
+ inline EventT* eventCast(PtrT* eptr)
+ {
+ if (is<EventT>(*eptr))
+ return static_cast<EventT*>(eptr);
+ return {};
+ }
+
+ template <typename BaseEventT, typename FnT, typename DefaultT>
+ inline auto visit(const BaseEventT& event, FnT visitor,
DefaultT&& defaultValue)
-> std::enable_if_t<
std::is_convertible<DefaultT, fn_return_t<FnT>>::value,