Commit b1812c20 authored by Volker Krause's avatar Volker Krause

Fix DepartureReply::departures to avoid copies

Also, make it less departure-centric, now that arrivals are handled here
too.
parent 451fe53d
......@@ -157,7 +157,7 @@ void LiveDataManager::checkTrainTrip(const TrainTrip& trip, const QString& resId
return;
}
for (const auto &dep : reply->departures()) {
for (const auto &dep : reply->result()) {
qCDebug(Log) << "Got departure information:" << dep.route().line().name() << dep.scheduledDepartureTime() << "for" << trip.trainNumber();
if (dep.scheduledDepartureTime() != trip.departureTime() || !isSameLine(dep.route().line().name(), trip.trainName(), trip.trainNumber())) {
continue;
......@@ -180,7 +180,7 @@ void LiveDataManager::checkTrainTrip(const TrainTrip& trip, const QString& resId
return;
}
for (const auto &arr : reply->departures()) {
for (const auto &arr : reply->result()) {
qCDebug(Log) << "Got arrival information:" << arr.route().line().name() << arr.scheduledArrivalTime() << "for" << trip.trainNumber();
if (arr.scheduledArrivalTime() != trip.arrivalTime() || !isSameLine(arr.route().line().name(), trip.trainName(), trip.trainNumber())) {
continue;
......
......@@ -32,30 +32,30 @@ public:
void finalizeResult() override;
DepartureRequest request;
std::vector<Departure> departures;
std::vector<Departure> result;
};
}
void DepartureReplyPrivate::finalizeResult()
{
if (departures.empty()) {
if (result.empty()) {
return;
}
error = Reply::NoError;
errorMsg.clear();
if (request.mode() == DepartureRequest::QueryDeparture) {
std::sort(departures.begin(), departures.end(), [](const auto &lhs, const auto &rhs) {
std::sort(result.begin(), result.end(), [](const auto &lhs, const auto &rhs) {
return lhs.scheduledDepartureTime() < rhs.scheduledDepartureTime();
});
} else {
std::sort(departures.begin(), departures.end(), [](const auto &lhs, const auto &rhs) {
std::sort(result.begin(), result.end(), [](const auto &lhs, const auto &rhs) {
return lhs.scheduledArrivalTime() < rhs.scheduledArrivalTime();
});
}
for (auto it = departures.begin(); it != departures.end(); ++it) {
for (auto mergeIt = it + 1; mergeIt != departures.end();) {
for (auto it = result.begin(); it != result.end(); ++it) {
for (auto mergeIt = it + 1; mergeIt != result.end();) {
if (request.mode() == DepartureRequest::QueryDeparture) {
if ((*it).scheduledDepartureTime() != (*mergeIt).scheduledDepartureTime()) {
break;
......@@ -68,7 +68,7 @@ void DepartureReplyPrivate::finalizeResult()
if (Departure::isSame(*it, *mergeIt)) {
*it = Departure::merge(*it, *mergeIt);
mergeIt = departures.erase(mergeIt);
mergeIt = result.erase(mergeIt);
} else {
++mergeIt;
}
......@@ -91,19 +91,25 @@ DepartureRequest DepartureReply::request() const
return d->request;
}
std::vector<Departure> DepartureReply::departures() const
const std::vector<Departure>& DepartureReply::result() const
{
Q_D(const DepartureReply);
return d->departures; // TODO this copies
return d->result;
}
std::vector<Departure>&& DepartureReply::takeResult()
{
Q_D(DepartureReply);
return std::move(d->result);
}
void DepartureReply::addResult(std::vector<Departure> &&res)
{
Q_D(DepartureReply);
if (d->departures.empty()) {
d->departures = std::move(res);
if (d->result.empty()) {
d->result = std::move(res);
} else {
d->departures.insert(d->departures.end(), res.begin(), res.end());
d->result.insert(d->result.end(), res.begin(), res.end());
}
d->pendingOps--;
......
......@@ -29,7 +29,7 @@ class Departure;
class DepartureRequest;
class DepartureReplyPrivate;
/** Departure query reply. */
/** Departure or arrival query reply. */
class DepartureReply : public Reply
{
Q_OBJECT
......@@ -39,8 +39,10 @@ public:
/** The request this is the reply for. */
DepartureRequest request() const;
/** Returns the found departure information. */
std::vector<Departure> departures() const;
/** Returns the found arrival or departure information. */
const std::vector<Departure>& result() const;
/** Returns the found arrival or departure information for moving elsewhere. */
std::vector<Departure>&& takeResult();
private:
friend class Manager;
......
......@@ -64,7 +64,7 @@ public:
emit loadingChanged();
if (reply->error() == DepartureReply::NoError) {
const auto res = reply->departures();
const auto res = reply->takeResult();
QVariantList l;
l.reserve(res.size());
std::transform(res.begin(), res.end(), std::back_inserter(l), [](const auto &journey) { return QVariant::fromValue(journey); });
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment