From ee214de7b829b08155bc223ea64c61c9ce2bcdf3 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 13 Oct 2017 23:55:35 +0200 Subject: Make JoinState (de)serializable library-wide --- jobs/syncjob.cpp | 12 +++--------- joinstate.h | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/jobs/syncjob.cpp b/jobs/syncjob.cpp index 6d37db5c..ce5dd894 100644 --- a/jobs/syncjob.cpp +++ b/jobs/syncjob.cpp @@ -69,19 +69,13 @@ BaseJob::Status SyncData::parseJson(const QJsonDocument &data) // TODO: account_data QJsonObject rooms = json.value("rooms").toObject(); - static const struct { QString jsonKey; JoinState enumVal; } roomStates[] + for (size_t i = 0; i < JoinStateStrings.size(); ++i) { - { "join", JoinState::Join }, - { "invite", JoinState::Invite }, - { "leave", JoinState::Leave } - }; - for (const auto& roomState: roomStates) - { - const QJsonObject rs = rooms.value(roomState.jsonKey).toObject(); + const auto rs = rooms.value(JoinStateStrings[i]).toObject(); // We have a Qt container on the right and an STL one on the left roomData.reserve(static_cast(rs.size())); for(auto roomIt = rs.begin(); roomIt != rs.end(); ++roomIt) - roomData.emplace_back(roomIt.key(), roomState.enumVal, + roomData.emplace_back(roomIt.key(), JoinState(i), roomIt.value().toObject()); } qCDebug(PROFILER) << "*** SyncData::parseJson():" << et.elapsed() << "ms"; diff --git a/joinstate.h b/joinstate.h index 348ca8a6..cfdb90f2 100644 --- a/joinstate.h +++ b/joinstate.h @@ -18,12 +18,24 @@ #pragma once +#include + namespace QMatrixClient { enum class JoinState { - Join, + Join = 0, Invite, Leave }; -} + + // We cannot use REGISTER_ENUM outside of a Q_OBJECT and besides, we want + // to use strings that match respective JSON keys. + static constexpr std::array JoinStateStrings + { { "join", "invite", "leave" } }; + + inline constexpr const char* toCString(JoinState js) + { + return JoinStateStrings[size_t(js)]; + } +} // namespace QMatrixClient -- cgit v1.2.3