diff options
author | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-07-30 19:57:41 +0200 |
---|---|---|
committer | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-09-04 18:42:11 +0200 |
commit | fc6321d53f7743f7629841d02c6e28e1a3ab3f83 (patch) | |
tree | e3cff2e7fc9228abf1b4df6100eaac17f3f2c543 | |
parent | 80499cc7619bb857c284e6e89db728ccee9c61f6 (diff) | |
download | libquotient-fc6321d53f7743f7629841d02c6e28e1a3ab3f83.tar.gz libquotient-fc6321d53f7743f7629841d02c6e28e1a3ab3f83.zip |
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).
-rw-r--r-- | lib/room.cpp | 5 | ||||
-rw-r--r-- | lib/roomstateview.h | 20 |
2 files changed, 22 insertions, 3 deletions
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<RoomNameEvent>().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<RoomCanonicalAliasEvent>().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 <Keyed_State_Event EvT> + 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<EvT>(stateKey)) + return evt->content(); + return std::move(defaultValue); + } + + template <Keyless_State_Event EvT> + auto content(typename EvT::content_type defaultValue = {}) const + { + // Same as above + if (const auto evt = get<EvT>()) + 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 |