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 /lib/roomstateview.h | |
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).
Diffstat (limited to 'lib/roomstateview.h')
-rw-r--r-- | lib/roomstateview.h | 20 |
1 files changed, 20 insertions, 0 deletions
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 |