From c42d268db0b40cdba06381fc64a6966a72c90709 Mon Sep 17 00:00:00 2001
From: Alexey Rusakov <Kitsune-Ral@users.sf.net>
Date: Fri, 11 Feb 2022 22:21:25 +0100
Subject: QUO_CONTENT_GETTER

To streamline adding of simple getters of content parts.
---
 lib/events/callanswerevent.h     |  6 ++----
 lib/events/callcandidatesevent.h | 17 +++--------------
 lib/events/callinviteevent.h     |  5 +----
 lib/events/event.h               | 15 +++++++++++++++
 lib/events/redactionevent.h      |  2 +-
 lib/events/roomevent.h           |  4 ++--
 6 files changed, 24 insertions(+), 25 deletions(-)

(limited to 'lib/events')

diff --git a/lib/events/callanswerevent.h b/lib/events/callanswerevent.h
index 8ffe60f2..70292a7a 100644
--- a/lib/events/callanswerevent.h
+++ b/lib/events/callanswerevent.h
@@ -17,10 +17,8 @@ public:
                              const QString& sdp);
     explicit CallAnswerEvent(const QString& callId, const QString& sdp);
 
-    int lifetime() const
-    {
-        return contentPart<int>("lifetime"_ls);
-    } // FIXME: Omittable<>?
+    QUO_CONTENT_GETTER(int, lifetime) // FIXME: Omittable<>?
+
     QString sdp() const
     {
         return contentPart<QJsonObject>("answer"_ls).value("sdp"_ls).toString();
diff --git a/lib/events/callcandidatesevent.h b/lib/events/callcandidatesevent.h
index 74c38f2c..e949f722 100644
--- a/lib/events/callcandidatesevent.h
+++ b/lib/events/callcandidatesevent.h
@@ -23,20 +23,9 @@ public:
                         { { QStringLiteral("candidates"), candidates } })
     {}
 
-    QJsonArray candidates() const
-    {
-        return contentPart<QJsonArray>("candidates"_ls);
-    }
-
-    QString callId() const
-    {
-        return contentPart<QString>("call_id");
-    }
-
-    int version() const
-    {
-        return contentPart<int>("version");
-    }
+    QUO_CONTENT_GETTER(QJsonArray, candidates)
+    QUO_CONTENT_GETTER(QString, callId)
+    QUO_CONTENT_GETTER(int, version)
 };
 
 REGISTER_EVENT_TYPE(CallCandidatesEvent)
diff --git a/lib/events/callinviteevent.h b/lib/events/callinviteevent.h
index 47362b5c..1b1f4f0f 100644
--- a/lib/events/callinviteevent.h
+++ b/lib/events/callinviteevent.h
@@ -16,10 +16,7 @@ public:
     explicit CallInviteEvent(const QString& callId, const int lifetime,
                              const QString& sdp);
 
-    int lifetime() const
-    {
-        return contentPart<int>("lifetime"_ls);
-    } // FIXME: Omittable<>?
+    QUO_CONTENT_GETTER(int, lifetime) // FIXME: Omittable<>?
     QString sdp() const
     {
         return contentPart<QJsonObject>("offer"_ls).value("sdp"_ls).toString();
diff --git a/lib/events/event.h b/lib/events/event.h
index a27c0b96..ec21c6aa 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -258,6 +258,21 @@ template <typename EventT>
 using EventsArray = std::vector<event_ptr_tt<EventT>>;
 using Events = EventsArray<Event>;
 
+//! \brief Define an inline method obtaining a content part
+//!
+//! This macro adds a const method that extracts a JSON value at the key
+//! <tt>toSnakeCase(PartName_)</tt> (sic) and converts it to the type
+//! \p PartType_. Effectively, the generated method is an equivalent of
+//! \code
+//! contentPart<PartType_>(Quotient::toSnakeCase(#PartName_##_ls));
+//! \endcode
+#define QUO_CONTENT_GETTER(PartType_, PartName_)                  \
+    PartType_ PartName_() const                                   \
+    {                                                             \
+        static const auto JsonKey = toSnakeCase(#PartName_##_ls); \
+        return contentPart<PartType_>(JsonKey);                   \
+    }
+
 // === Facilities for event class definitions ===
 
 // This macro should be used in a public section of an event class to
diff --git a/lib/events/redactionevent.h b/lib/events/redactionevent.h
index be20bf52..1c486a44 100644
--- a/lib/events/redactionevent.h
+++ b/lib/events/redactionevent.h
@@ -17,7 +17,7 @@ public:
     {
         return fullJson()["redacts"_ls].toString();
     }
-    QString reason() const { return contentPart<QString>("reason"_ls); }
+    QUO_CONTENT_GETTER(QString, reason)
 };
 REGISTER_EVENT_TYPE(RedactionEvent)
 } // namespace Quotient
diff --git a/lib/events/roomevent.h b/lib/events/roomevent.h
index 0692ad8a..7f724689 100644
--- a/lib/events/roomevent.h
+++ b/lib/events/roomevent.h
@@ -96,8 +96,8 @@ public:
     ~CallEventBase() override = default;
     bool isCallEvent() const override { return true; }
 
-    QString callId() const { return contentPart<QString>("call_id"_ls); }
-    int version() const { return contentPart<int>("version"_ls); }
+    QUO_CONTENT_GETTER(QString, callId)
+    QUO_CONTENT_GETTER(int, version)
 
 protected:
     static QJsonObject basicJson(const QString& matrixType,
-- 
cgit v1.2.3