aboutsummaryrefslogtreecommitdiff
path: root/lib/events/event.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events/event.h')
-rw-r--r--lib/events/event.h28
1 files changed, 23 insertions, 5 deletions
diff --git a/lib/events/event.h b/lib/events/event.h
index e0d83976..b7bbd83e 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -32,19 +32,23 @@ namespace QMatrixClient
template <typename EventT>
using event_ptr_tt = std::unique_ptr<EventT>;
+ /// Unwrap a plain pointer from a smart pointer
template <typename EventT>
- inline EventT* rawPtr(const event_ptr_tt<EventT>& ptr) // unwrap
+ inline EventT* rawPtr(const event_ptr_tt<EventT>& ptr)
{
return ptr.get();
}
+ /// Unwrap a plain pointer and downcast it to the specified type
template <typename TargetEventT, typename EventT>
inline TargetEventT* weakPtrCast(const event_ptr_tt<EventT>& ptr)
{
return static_cast<TargetEventT*>(rawPtr(ptr));
}
+ /// Re-wrap a smart pointer to base into a smart pointer to derived
template <typename TargetT, typename SourceT>
+ [[deprecated("Consider using eventCast() or visit() instead")]]
inline event_ptr_tt<TargetT> ptrCast(event_ptr_tt<SourceT>&& ptr)
{
return unique_ptr_cast<TargetT>(ptr);
@@ -208,6 +212,9 @@ namespace QMatrixClient
template <typename EventT>
inline auto registerEventType()
{
+ // Initialise exactly once, even if this function is called twice for
+ // the same type (for whatever reason - you never know the ways of
+ // static initialisation is done).
static const auto _ = setupFactory<EventT>();
return _; // Only to facilitate usage in static initialisation
}
@@ -257,8 +264,18 @@ namespace QMatrixClient
return fromJson<T>(contentJson()[key]);
}
+ friend QDebug operator<<(QDebug dbg, const Event& e)
+ {
+ QDebugStateSaver _dss { dbg };
+ dbg.noquote().nospace()
+ << e.matrixType() << '(' << e.type() << "): ";
+ e.dumpTo(dbg);
+ return dbg;
+ }
+
virtual bool isStateEvent() const { return false; }
virtual bool isCallEvent() const { return false; }
+ virtual void dumpTo(QDebug dbg) const;
protected:
QJsonObject& editJson() { return _json; }
@@ -327,7 +344,8 @@ namespace QMatrixClient
-> decltype(static_cast<EventT*>(&*eptr))
{
Q_ASSERT(eptr);
- return is<EventT>(*eptr) ? static_cast<EventT*>(&*eptr) : nullptr;
+ return is<std::decay_t<EventT>>(*eptr)
+ ? static_cast<EventT*>(&*eptr) : nullptr;
}
// A single generic catch-all visitor
@@ -359,7 +377,7 @@ namespace QMatrixClient
visit(const BaseEventT& event, FnT&& visitor)
{
using event_type = fn_arg_t<FnT>;
- if (is<event_type>(event))
+ if (is<std::decay_t<event_type>>(event))
visitor(static_cast<event_type>(event));
}
@@ -373,7 +391,7 @@ namespace QMatrixClient
fn_return_t<FnT>&& defaultValue = {})
{
using event_type = fn_arg_t<FnT>;
- if (is<event_type>(event))
+ if (is<std::decay_t<event_type>>(event))
return visitor(static_cast<event_type>(event));
return std::forward<fn_return_t<FnT>>(defaultValue);
}
@@ -386,7 +404,7 @@ namespace QMatrixClient
FnTs&&... visitors)
{
using event_type1 = fn_arg_t<FnT1>;
- if (is<event_type1>(event))
+ if (is<std::decay_t<event_type1>>(event))
return visitor1(static_cast<event_type1&>(event));
return visit(event, std::forward<FnT2>(visitor2),
std::forward<FnTs>(visitors)...);