summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2018-02-21 18:35:58 +0300
committerGeorge Hazan <ghazan@miranda.im>2018-02-21 18:40:14 +0300
commitb8a1ac529c6615d5ba6aa3d7ea9e8b4fe9ad4b71 (patch)
tree05f3df6a2d296709f46f537eaeefea3c58b24f6c
parent058282527241fe458a1aae28d565a727dcc1a811 (diff)
Discord: C++'11 iterators
-rw-r--r--protocols/Discord/src/dispatch.cpp45
-rw-r--r--protocols/Discord/src/groupchat.cpp4
-rw-r--r--protocols/Discord/src/guilds.cpp14
-rw-r--r--protocols/Discord/src/server.cpp5
-rw-r--r--protocols/Discord/src/utils.cpp16
5 files changed, 35 insertions, 49 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp
index 1c53c6ccd0..e1aba4e28e 100644
--- a/protocols/Discord/src/dispatch.cpp
+++ b/protocols/Discord/src/dispatch.cpp
@@ -95,8 +95,8 @@ void CDiscordProto::OnCommandChannelCreated(const JSONNode &pRoot)
// group channel for a guild
CDiscordUser *pUser = ProcessGuildChannel(pGuild, pRoot);
if (pUser != nullptr)
- for (int i = 0; i < pGuild->arChatUsers.getCount(); i++)
- AddUserToChannel(*pUser, pGuild->arChatUsers[i]);
+ for (auto &it : pGuild->arChatUsers)
+ AddUserToChannel(*pUser, *it);
}
}
@@ -222,12 +222,11 @@ void CDiscordProto::OnCommandGuildMemberRemoved(const JSONNode &pRoot)
CMStringW wszUserId = pRoot["user"]["id"].as_mstring();
- for (int i = 0; i < arUsers.getCount(); i++) {
- CDiscordUser &pUser = arUsers[i];
- if (pUser.guildId != pGuild->id)
+ for (auto &pUser : arUsers) {
+ if (pUser->guildId != pGuild->id)
continue;
- GCEVENT gce = { m_szModuleName, pUser.wszUsername, GC_EVENT_PART };
+ GCEVENT gce = { m_szModuleName, pUser->wszUsername, GC_EVENT_PART };
gce.time = time(nullptr);
gce.ptszUID = wszUserId;
Chat_Event(&gce);
@@ -249,20 +248,19 @@ void CDiscordProto::OnCommandGuildMemberUpdated(const JSONNode &pRoot)
if (gm->wszNick.IsEmpty())
gm->wszNick = pRoot["user"]["username"].as_mstring() + L"#" + pRoot["user"]["discriminator"].as_mstring();
- for (int i = 0; i < arUsers.getCount(); i++) {
- CDiscordUser &pUser = arUsers[i];
- if (pUser.guildId != pGuild->id)
+ for (auto &it : arUsers) {
+ if (it->guildId != pGuild->id)
continue;
CMStringW wszOldNick;
- SESSION_INFO *si = pci->SM_FindSession(pUser.wszUsername, m_szModuleName);
+ SESSION_INFO *si = pci->SM_FindSession(it->wszUsername, m_szModuleName);
if (si != nullptr) {
USERINFO *ui = pci->UM_FindUser(si->pUsers, wszUserId);
if (ui != nullptr)
wszOldNick = ui->pszNick;
}
- GCEVENT gce = { m_szModuleName, pUser.wszUsername, GC_EVENT_NICK };
+ GCEVENT gce = { m_szModuleName, it->wszUsername, GC_EVENT_NICK };
gce.time = time(nullptr);
gce.ptszUID = wszUserId;
gce.ptszNick = wszOldNick;
@@ -295,21 +293,18 @@ void CDiscordProto::OnCommandRoleDeleted(const JSONNode &pRoot)
int iOldPosition = pRole->position;
pGuild->arRoles.remove(pRole);
- for (int i = 0; i < pGuild->arRoles.getCount(); i++) {
- CDiscordRole &p = pGuild->arRoles[i];
- if (p.position > iOldPosition)
- p.position--;
- }
+ for (auto &it : pGuild->arRoles)
+ if (it->position > iOldPosition)
+ it->position--;
- for (int i = 0; i < arUsers.getCount(); i++) {
- CDiscordUser &p = arUsers[i];
- if (p.guildId != pGuild->id)
+ for (auto &it : arUsers) {
+ if (it->guildId != pGuild->id)
continue;
- SESSION_INFO *si = pci->SM_FindSession(p.wszUsername, m_szModuleName);
+ SESSION_INFO *si = pci->SM_FindSession(it->wszUsername, m_szModuleName);
if (si != nullptr) {
pci->TM_RemoveAll(&si->pStatuses);
- BuildStatusList(pGuild, p.wszUsername);
+ BuildStatusList(pGuild, it->wszUsername);
}
}
}
@@ -384,11 +379,9 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot)
pm->wszNick = pRoot["user"]["username"].as_mstring() + L"#" + pRoot["user"]["discriminator"].as_mstring();
pGuild->arChatUsers.insert(pm);
- for (int i = 0; i < arUsers.getCount(); i++) {
- CDiscordUser &pChannel = arUsers[i];
- if (pChannel.guildId == pGuild->id)
- AddUserToChannel(pChannel, *pm);
- }
+ for (auto &it : arUsers)
+ if (it->guildId == pGuild->id)
+ AddUserToChannel(*it, *pm);
}
ParseSpecialChars(si, wszText);
diff --git a/protocols/Discord/src/groupchat.cpp b/protocols/Discord/src/groupchat.cpp
index b2ba5f8248..1492a58800 100644
--- a/protocols/Discord/src/groupchat.cpp
+++ b/protocols/Discord/src/groupchat.cpp
@@ -29,8 +29,8 @@ void CDiscordProto::BuildStatusList(const CDiscordGuild *pGuild, const CMStringW
{
Chat_AddGroup(m_szModuleName, wszChannelId, L"@owner");
- for (int i = 0; i < pGuild->arRoles.getCount(); i++)
- Chat_AddGroup(m_szModuleName, wszChannelId, pGuild->arRoles[i].wszName);
+ for (auto &it : pGuild->arRoles)
+ Chat_AddGroup(m_szModuleName, wszChannelId, it->wszName);
}
/////////////////////////////////////////////////////////////////////////////////////////
diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp
index 70bfeeeab0..78eb0a1507 100644
--- a/protocols/Discord/src/guilds.cpp
+++ b/protocols/Discord/src/guilds.cpp
@@ -140,8 +140,8 @@ CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JS
if (oldMsgId != 0 && pUser->lastMsg.id > oldMsgId)
RetrieveHistory(pUser->hContact, MSG_AFTER, oldMsgId, 99);
- for (int i = 0; i < pGuild->arChatUsers.getCount(); i++)
- AddUserToChannel(*pUser, pGuild->arChatUsers[i]);
+ for (auto &it : pGuild->arChatUsers)
+ AddUserToChannel(*pUser, *it);
return pUser;
}
@@ -222,12 +222,10 @@ void CDiscordProto::ParseGuildContents(CDiscordGuild *pGuild, const JSONNode &pR
gm->iStatus = StrToStatus(s["status"].as_mstring());
}
- for (int k = 0; k < newMembers.getCount(); k++) {
- CDiscordGuildMember *pm = newMembers[k];
- for (int i = 0; i < arUsers.getCount(); i++) {
- CDiscordUser &pUser = arUsers[i];
- if (pUser.guildId == pGuild->id)
- AddUserToChannel(pUser, *pm);
+ for (auto &pm : newMembers) {
+ for (auto &pUser : arUsers) {
+ if (pUser->guildId == pGuild->id)
+ AddUserToChannel(*pUser, *pm);
}
}
}
diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp
index 4dd70b00ab..5a3a41f99b 100644
--- a/protocols/Discord/src/server.cpp
+++ b/protocols/Discord/src/server.cpp
@@ -81,9 +81,8 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest
arNodes.insert(&p);
}
- for (int i = 0; i < arNodes.getCount(); i++) {
- JSONNode &p = *arNodes[i];
-
+ for (auto &it : arNodes) {
+ auto &p = *it;
CMStringW wszText = PrepareMessageText(p);
CMStringW wszUserId = p["author"]["id"].as_mstring();
SnowFlake msgid = ::getId(p["id"]);
diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp
index 1167b4a782..5be21385ec 100644
--- a/protocols/Discord/src/utils.cpp
+++ b/protocols/Discord/src/utils.cpp
@@ -113,22 +113,18 @@ CDiscordUser* CDiscordProto::FindUser(SnowFlake id)
CDiscordUser* CDiscordProto::FindUser(const wchar_t *pwszUsername, int iDiscriminator)
{
- for (int i = 0; i < arUsers.getCount(); i++) {
- CDiscordUser &p = arUsers[i];
- if (p.wszUsername == pwszUsername && p.iDiscriminator == iDiscriminator)
- return &p;
- }
+ for (auto &p : arUsers)
+ if (p->wszUsername == pwszUsername && p->iDiscriminator == iDiscriminator)
+ return p;
return nullptr;
}
CDiscordUser* CDiscordProto::FindUserByChannel(SnowFlake channelId)
{
- for (int i = 0; i < arUsers.getCount(); i++) {
- CDiscordUser &p = arUsers[i];
- if (p.channelId == channelId)
- return &p;
- }
+ for (auto &p : arUsers)
+ if (p->channelId == channelId)
+ return p;
return nullptr;
}