From efeb50a46ad824aa258472f6ac8da74810f05a55 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 31 Mar 2018 13:16:02 +0900 Subject: Move source files to a separate folder It's been long overdue to separate them from the rest of the stuff (docs etc.). Also, this allows installing to a directory within the checked out git tree (say, ./install/, similar to ./build/). --- lib/events/directchatevent.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/events/directchatevent.cpp (limited to 'lib/events/directchatevent.cpp') diff --git a/lib/events/directchatevent.cpp b/lib/events/directchatevent.cpp new file mode 100644 index 00000000..7049d967 --- /dev/null +++ b/lib/events/directchatevent.cpp @@ -0,0 +1,36 @@ +/****************************************************************************** + * Copyright (C) 2018 Kitsune Ral + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "directchatevent.h" + +#include "converters.h" + +using namespace QMatrixClient; + +DirectChatEvent::DirectChatEvent(const QJsonObject& obj) + : Event(Type::DirectChat, obj) +{ } + +QMultiHash DirectChatEvent::usersToDirectChats() const +{ + QMultiHash result; + for (auto it = contentJson().begin(); it != contentJson().end(); ++it) + for (auto roomIdValue: it.value().toArray()) + result.insert(it.key(), roomIdValue.toString()); + return result; +} -- cgit v1.2.3 From d76b2fbe30a503009d33c4df06848d7356fc39a6 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 3 Apr 2018 18:32:14 +0900 Subject: DirectChatEvent: be careful with range-for over temporaries ...because temporaries returned by temporaries tend to disappear before you enter the loop body (see the bottom of http://en.cppreference.com/w/cpp/language/range-for#Explanation). --- lib/connection.cpp | 10 ++-------- lib/events/directchatevent.cpp | 11 +++++++++-- 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'lib/events/directchatevent.cpp') diff --git a/lib/connection.cpp b/lib/connection.cpp index 9d0a34f3..71ada8f7 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -281,8 +281,7 @@ void Connection::sync(int timeout) void Connection::onSyncSuccess(SyncData &&data) { d->data->setLastEvent(data.nextBatch()); - auto allRoomData = data.takeRoomData(); - for (auto&& roomData: allRoomData) + for (auto&& roomData: data.takeRoomData()) { const auto forgetIdx = d->roomIdsToForget.indexOf(roomData.roomId); if (forgetIdx != -1) @@ -303,12 +302,7 @@ void Connection::onSyncSuccess(SyncData &&data) { r->updateData(std::move(roomData)); QCoreApplication::processEvents(); } - // `for (auto&& accountEvent: data.takeAccountData())` doesn't work well - // with Clang on FreeBSD in Release configuration; it seems that - // the lifetime of the returned rvalue does not extend (violating the - // standard) and accountEvent refers to garbage in the loop body as a result. - auto allAccountData = data.takeAccountData(); - for (auto&& accountEvent: allAccountData) + for (auto&& accountEvent: data.takeAccountData()) { if (accountEvent->type() == EventType::DirectChat) { diff --git a/lib/events/directchatevent.cpp b/lib/events/directchatevent.cpp index 7049d967..63d638a3 100644 --- a/lib/events/directchatevent.cpp +++ b/lib/events/directchatevent.cpp @@ -29,8 +29,15 @@ DirectChatEvent::DirectChatEvent(const QJsonObject& obj) QMultiHash DirectChatEvent::usersToDirectChats() const { QMultiHash result; - for (auto it = contentJson().begin(); it != contentJson().end(); ++it) - for (auto roomIdValue: it.value().toArray()) + const auto json = contentJson(); + for (auto it = json.begin(); it != json.end(); ++it) + { + // Beware of range-for's over temporary returned from temporary + // (see the bottom of + // http://en.cppreference.com/w/cpp/language/range-for#Explanation) + const auto roomIds = it.value().toArray(); + for (const auto& roomIdValue: roomIds) result.insert(it.key(), roomIdValue.toString()); + } return result; } -- cgit v1.2.3