From e7e9330d665c1d8d2391707d27019a7f454cbcdf Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 26 Mar 2018 11:43:00 +0900 Subject: Introduce JoinStates (QFlags) This required to change numeric values for JoinState enum; so anybody who relied on them being 0-based and/or contiguous, beware. --- jobs/syncjob.cpp | 5 +++-- joinstate.h | 15 +++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/jobs/syncjob.cpp b/jobs/syncjob.cpp index 6b3f3acf..ed579f12 100644 --- a/jobs/syncjob.cpp +++ b/jobs/syncjob.cpp @@ -74,13 +74,14 @@ BaseJob::Status SyncData::parseJson(const QJsonDocument &data) accountData.fromJson(json); QJsonObject rooms = json.value("rooms").toObject(); - for (size_t i = 0; i < JoinStateStrings.size(); ++i) + JoinStates::Int ii = 1; // ii is used to make a JoinState value + for (size_t i = 0; i < JoinStateStrings.size(); ++i, ii <<= 1) { 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(), JoinState(i), + roomData.emplace_back(roomIt.key(), JoinState(ii), roomIt.value().toObject()); } qCDebug(PROFILER) << "*** SyncData::parseJson(): batch with" diff --git a/joinstate.h b/joinstate.h index d6c374d2..42613895 100644 --- a/joinstate.h +++ b/joinstate.h @@ -18,17 +18,21 @@ #pragma once +#include + #include namespace QMatrixClient { enum class JoinState { - Join = 0, - Invite, - Leave + Join = 0x1, + Invite = 0x2, + Leave = 0x4 }; + Q_DECLARE_FLAGS(JoinStates, JoinState) + // We cannot use REGISTER_ENUM outside of a Q_OBJECT and besides, we want // to use strings that match respective JSON keys. static const std::array JoinStateStrings @@ -36,6 +40,9 @@ namespace QMatrixClient inline const char* toCString(JoinState js) { - return JoinStateStrings[size_t(js)]; + size_t state = size_t(js), index = 0; + while (state >>= 1) ++index; + return JoinStateStrings[index]; } } // namespace QMatrixClient +Q_DECLARE_OPERATORS_FOR_FLAGS(QMatrixClient::JoinStates) -- cgit v1.2.3