core: move dive_[has|get]_gps_location() to struct dive

Feel natural in a C++ code base.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-30 16:33:52 +02:00 committed by bstoeger
parent 286d8fe21c
commit e90251b0cf
7 changed files with 16 additions and 20 deletions

View file

@ -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 the current dive has a location, use that as location for the new dive site
if (current_dive) { if (current_dive) {
location_t loc = dive_get_gps_location(current_dive); location_t loc = current_dive->get_gps_location();
if (has_location(&loc)) if (has_location(&loc))
ds->location = loc; ds->location = loc;
} }

View file

@ -2538,20 +2538,18 @@ bool dive_site_has_gps_location(const struct dive_site *ds)
return ds && has_location(&ds->location); 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 dive_site && dive_site_has_gps_location(dive_site);
return false;
return dive_site_has_gps_location(dive->dive_site);
} }
/* Extract GPS location of a dive computer stored in the GPS1 /* Extract GPS location of a dive computer stored in the GPS1
* or GPS2 extra data fields */ * 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; location_t res;
for (const auto &data: dc->extra_data) { for (const auto &data: dc.extra_data) {
if (data.key == "GPS1") { if (data.key == "GPS1") {
parse_location(data.value.c_str(), &res); parse_location(data.value.c_str(), &res);
/* If we found a valid GPS1 field exit early since /* 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 * This function is potentially slow, therefore only call sparingly
* and remember the result. * 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) { for (const struct divecomputer &dc: dcs) {
location_t res = dc_get_gps_location(&dc); location_t res = dc_get_gps_location(dc);
if (has_location(&res)) if (has_location(&res))
return res; return res;
} }
@ -2584,10 +2582,7 @@ location_t dive_get_gps_location(const struct dive *d)
/* No libdivecomputer generated GPS data found. /* No libdivecomputer generated GPS data found.
* Let's use the location of the current dive site. * Let's use the location of the current dive site.
*/ */
if (d->dive_site) return dive_site ? dive_site->location : location_t();
return d->dive_site->location;
return location_t();
} }
gasmix_loop::gasmix_loop(const struct dive &d, const struct divecomputer &dc) : gasmix_loop::gasmix_loop(const struct dive &d, const struct divecomputer &dc) :

View file

@ -118,6 +118,9 @@ struct dive {
depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, int roundto) const; 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; 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()! */ /* Don't call directly, use dive_table::merge_dives()! */
static std::unique_ptr<dive> create_merged_dive(const struct dive &a, const struct dive &b, int offset, bool prefer_downloaded); static std::unique_ptr<dive> 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<dive> clone_make_first_dc(const struct dive &d, int dc_number); extern std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number);
extern bool dive_site_has_gps_location(const struct dive_site *ds); 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); extern bool time_during_dive_with_offset(const struct dive *dive, timestamp_t when, timestamp_t offset);

View file

@ -60,7 +60,7 @@ dive_site *dive_site_table::get_by_gps_proximity(location_t loc, int distance) c
struct dive_site *res = nullptr; struct dive_site *res = nullptr;
unsigned int cur_distance, min_distance = distance; unsigned int cur_distance, min_distance = distance;
for (const auto &ds: *this) { 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) { (cur_distance = get_distance(ds->location, loc)) < min_distance) {
min_distance = cur_distance; min_distance = cur_distance;
res = ds.get(); res = ds.get();

View file

@ -1207,7 +1207,7 @@ static void gps_in_dive(const char *buffer, struct dive *dive, struct parser_sta
} }
ds->add_dive(dive); ds->add_dive(dive);
} else { } else {
if (dive_site_has_gps_location(ds) && if (ds->has_gps_location() &&
has_location(&location) && ds->location != location) { has_location(&location) && ds->location != location) {
// Houston, we have a problem // 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)", report_info("dive site uuid in dive, but gps location (%10.6f/%10.6f) different from dive location (%10.6f/%10.6f)",

View file

@ -667,7 +667,7 @@ void DiveLocationLineEdit::setCurrentDiveSite(struct dive *d)
location_t currentLocation; location_t currentLocation;
if (d) { if (d) {
currDs = get_dive_site_for_dive(d); currDs = get_dive_site_for_dive(d);
currentLocation = dive_get_gps_location(d); currentLocation = d->get_gps_location();
} else { } else {
currDs = nullptr; currDs = nullptr;
} }

View file

@ -371,7 +371,7 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role)
case COUNTRY: case COUNTRY:
return QVariant(); return QVariant();
case LOCATION: case LOCATION:
if (dive_has_gps_location(d)) if (d->dive_has_gps_location())
return getGlobeIcon(); return getGlobeIcon();
break; break;
case PHOTOS: case PHOTOS: