Make QMap<> access in deleteGpsFix() more idiomatic

To access a QMap<> entry, the value() function is used with a sentinel
as default value. If the sentinel is returned, the code assumes that
the searched for entry doesn't exist.

Make this code more idiomatic by using an iterator and testing for
end().

This fixes a compiler warning, because only one of the elements of
the sentinel was initialized, but the remaining elements were
copied. Harmless, because the code would exit early if it found
the sentinel. Still not nice.

While redoing this function, the entry-not-found message was improved
(adding of function name, space between massage and timestamp) and
elevated from debug to warning level.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2017-12-20 00:01:42 +01:00 committed by Lubomir I. Ivanov
parent 22dc7b84f0
commit 28ae35e7df

View file

@ -436,13 +436,12 @@ void GpsLocation::deleteFixFromStorage(gpsTracker &gt)
void GpsLocation::deleteGpsFix(qint64 when)
{
struct gpsTracker defaultTracker;
defaultTracker.when = 0;
struct gpsTracker deletedTracker = m_trackers.value(when, defaultTracker);
if (deletedTracker.when != when) {
qDebug() << "can't find tracker for timestamp" << when;
auto it = m_trackers.find(when);
if (it == m_trackers.end()) {
qWarning() << "GpsLocation::deleteGpsFix(): can't find tracker for timestamp " << when;
return;
}
struct gpsTracker deletedTracker = *it;
deleteFixFromStorage(deletedTracker);
m_deletedTrackers.append(deletedTracker);
}