From fc6321d53f7743f7629841d02c6e28e1a3ab3f83 Mon Sep 17 00:00:00 2001 From: Alexey Rusakov Date: Sat, 30 Jul 2022 19:57:41 +0200 Subject: RoomStateView::content() This gives a more conventional API compared to queryOr() that can be used for event objects that have content() defined - with the downside being that content() unpacks the entire object instead of retrieving one particular piece (but for state events and single key-value content it's not a problem, and those make for the vast majority of events). --- lib/room.cpp | 5 ++--- lib/roomstateview.h | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/room.cpp b/lib/room.cpp index 042d38ac..a6617cc3 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -597,7 +597,7 @@ bool Room::allHistoryLoaded() const QString Room::name() const { - return currentState().queryOr(&RoomNameEvent::name, QString()); + return currentState().content().value; } QStringList Room::aliases() const @@ -613,8 +613,7 @@ QStringList Room::aliases() const QStringList Room::altAliases() const { - return currentState().queryOr(&RoomCanonicalAliasEvent::altAliases, - QStringList()); + return currentState().content().altAliases; } QString Room::canonicalAlias() const diff --git a/lib/roomstateview.h b/lib/roomstateview.h index 13b375f2..119c24cf 100644 --- a/lib/roomstateview.h +++ b/lib/roomstateview.h @@ -90,6 +90,26 @@ public: return contains(EvT::TypeId); } + template + auto content(const QString& stateKey, + typename EvT::content_type defaultValue = {}) const + { + // StateEvent<>::content is special in that it returns a const-ref, + // and lift() inside queryOr() can't wrap that in a temporary Omittable. + if (const auto evt = get(stateKey)) + return evt->content(); + return std::move(defaultValue); + } + + template + auto content(typename EvT::content_type defaultValue = {}) const + { + // Same as above + if (const auto evt = get()) + return evt->content(); + return defaultValue; + } + //! \brief Get the content of the current state event with the given //! event type and state key //! \return An empty object if there's no event in the current state with -- cgit v1.2.3