aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-10-13 23:55:35 +0200
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-10-14 09:17:32 +0200
commitee214de7b829b08155bc223ea64c61c9ce2bcdf3 (patch)
treed81c2238692674a131a0353ddf915d278d9ad355
parent76c42a9863b83229e6afaf4be32e9582e3d97d3f (diff)
downloadlibquotient-ee214de7b829b08155bc223ea64c61c9ce2bcdf3.tar.gz
libquotient-ee214de7b829b08155bc223ea64c61c9ce2bcdf3.zip
Make JoinState (de)serializable library-wide
-rw-r--r--jobs/syncjob.cpp12
-rw-r--r--joinstate.h16
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<size_t>(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 <array>
+
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<const char*, 3> JoinStateStrings
+ { { "join", "invite", "leave" } };
+
+ inline constexpr const char* toCString(JoinState js)
+ {
+ return JoinStateStrings[size_t(js)];
+ }
+} // namespace QMatrixClient