From e90251b0cf660b91d3c4cbed4bb53e9d8ee48beb Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 30 Jun 2024 16:33:52 +0200 Subject: [PATCH] core: move dive_[has|get]_gps_location() to struct dive Feel natural in a C++ code base. Signed-off-by: Berthold Stoeger --- commands/command_edit.cpp | 2 +- core/dive.cpp | 21 ++++++++------------- core/dive.h | 5 +++-- core/divesite.cpp | 2 +- core/parse-xml.cpp | 2 +- desktop-widgets/locationinformation.cpp | 2 +- qt-models/divetripmodel.cpp | 2 +- 7 files changed, 16 insertions(+), 20 deletions(-) diff --git a/commands/command_edit.cpp b/commands/command_edit.cpp index 62f205e3f..11e52a7f8 100644 --- a/commands/command_edit.cpp +++ b/commands/command_edit.cpp @@ -381,7 +381,7 @@ static struct dive_site *createDiveSite(const std::string &name) // If the current dive has a location, use that as location for the new dive site if (current_dive) { - location_t loc = dive_get_gps_location(current_dive); + location_t loc = current_dive->get_gps_location(); if (has_location(&loc)) ds->location = loc; } diff --git a/core/dive.cpp b/core/dive.cpp index 0800f25ff..c98d0f638 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -2538,20 +2538,18 @@ bool dive_site_has_gps_location(const struct dive_site *ds) return ds && has_location(&ds->location); } -int dive_has_gps_location(const struct dive *dive) +bool dive::dive_has_gps_location() const { - if (!dive) - return false; - return dive_site_has_gps_location(dive->dive_site); + return dive_site && dive_site_has_gps_location(dive_site); } /* Extract GPS location of a dive computer stored in the GPS1 * or GPS2 extra data fields */ -static location_t dc_get_gps_location(const struct divecomputer *dc) +static location_t dc_get_gps_location(const struct divecomputer &dc) { location_t res; - for (const auto &data: dc->extra_data) { + for (const auto &data: dc.extra_data) { if (data.key == "GPS1") { parse_location(data.value.c_str(), &res); /* If we found a valid GPS1 field exit early since @@ -2573,10 +2571,10 @@ static location_t dc_get_gps_location(const struct divecomputer *dc) * This function is potentially slow, therefore only call sparingly * and remember the result. */ -location_t dive_get_gps_location(const struct dive *d) +location_t dive::get_gps_location() const { - for (const struct divecomputer &dc: d->dcs) { - location_t res = dc_get_gps_location(&dc); + for (const struct divecomputer &dc: dcs) { + location_t res = dc_get_gps_location(dc); if (has_location(&res)) return res; } @@ -2584,10 +2582,7 @@ location_t dive_get_gps_location(const struct dive *d) /* No libdivecomputer generated GPS data found. * Let's use the location of the current dive site. */ - if (d->dive_site) - return d->dive_site->location; - - return location_t(); + return dive_site ? dive_site->location : location_t(); } gasmix_loop::gasmix_loop(const struct dive &d, const struct divecomputer &dc) : diff --git a/core/dive.h b/core/dive.h index 576838529..d95d5e733 100644 --- a/core/dive.h +++ b/core/dive.h @@ -118,6 +118,9 @@ struct dive { depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, int roundto) const; depth_t gas_mnd(struct gasmix mix, depth_t end, int roundto) const; + bool dive_has_gps_location() const; + location_t get_gps_location() const; + /* Don't call directly, use dive_table::merge_dives()! */ static std::unique_ptr create_merged_dive(const struct dive &a, const struct dive &b, int offset, bool prefer_downloaded); }; @@ -166,8 +169,6 @@ extern const struct divecomputer *get_dive_dc(const struct dive *dive, int nr); extern std::unique_ptr clone_make_first_dc(const struct dive &d, int dc_number); extern bool dive_site_has_gps_location(const struct dive_site *ds); -extern int dive_has_gps_location(const struct dive *dive); -extern location_t dive_get_gps_location(const struct dive *d); extern bool time_during_dive_with_offset(const struct dive *dive, timestamp_t when, timestamp_t offset); diff --git a/core/divesite.cpp b/core/divesite.cpp index 77a5f0b3a..2133d9497 100644 --- a/core/divesite.cpp +++ b/core/divesite.cpp @@ -60,7 +60,7 @@ dive_site *dive_site_table::get_by_gps_proximity(location_t loc, int distance) c struct dive_site *res = nullptr; unsigned int cur_distance, min_distance = distance; for (const auto &ds: *this) { - if (dive_site_has_gps_location(ds.get()) && + if (ds->has_gps_location() && (cur_distance = get_distance(ds->location, loc)) < min_distance) { min_distance = cur_distance; res = ds.get(); diff --git a/core/parse-xml.cpp b/core/parse-xml.cpp index 6fbc48f19..f66b314a6 100644 --- a/core/parse-xml.cpp +++ b/core/parse-xml.cpp @@ -1207,7 +1207,7 @@ static void gps_in_dive(const char *buffer, struct dive *dive, struct parser_sta } ds->add_dive(dive); } else { - if (dive_site_has_gps_location(ds) && + if (ds->has_gps_location() && has_location(&location) && ds->location != location) { // Houston, we have a problem report_info("dive site uuid in dive, but gps location (%10.6f/%10.6f) different from dive location (%10.6f/%10.6f)", diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp index a3801e7b2..d61a03741 100644 --- a/desktop-widgets/locationinformation.cpp +++ b/desktop-widgets/locationinformation.cpp @@ -667,7 +667,7 @@ void DiveLocationLineEdit::setCurrentDiveSite(struct dive *d) location_t currentLocation; if (d) { currDs = get_dive_site_for_dive(d); - currentLocation = dive_get_gps_location(d); + currentLocation = d->get_gps_location(); } else { currDs = nullptr; } diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 120d49b03..2e8cb9195 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -371,7 +371,7 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role) case COUNTRY: return QVariant(); case LOCATION: - if (dive_has_gps_location(d)) + if (d->dive_has_gps_location()) return getGlobeIcon(); break; case PHOTOS: