From 76d672210d2b520582fba2cd43c048405238369e Mon Sep 17 00:00:00 2001
From: Berthold Stoeger
Date: Sun, 30 Jun 2024 20:55:34 +0200
Subject: [PATCH] core: move get_dive_location()/_country() to struct dive
Feels natural in a C++ code base.
Signed-off-by: Berthold Stoeger
---
core/dive.cpp | 10 ++++------
core/dive.h | 5 ++---
core/save-git.cpp | 2 +-
core/save-html.cpp | 2 +-
core/trip.cpp | 4 ++--
core/worldmap-save.cpp | 2 +-
desktop-widgets/templatelayout.cpp | 2 +-
mobile-widgets/qmlmanager.cpp | 2 +-
qt-models/divetripmodel.cpp | 10 +++++-----
9 files changed, 18 insertions(+), 21 deletions(-)
diff --git a/core/dive.cpp b/core/dive.cpp
index febdef1c3..acbeb0bcf 100644
--- a/core/dive.cpp
+++ b/core/dive.cpp
@@ -2496,16 +2496,14 @@ depth_t dive::gas_mnd(struct gasmix mix, depth_t end, int roundto) const
return depth_t { (int)lrint(((double)mbar_to_depth(maxambient)) / roundto) * roundto };
}
-std::string get_dive_country(const struct dive *dive)
+std::string dive::get_country() const
{
- struct dive_site *ds = dive->dive_site;
- return ds ? taxonomy_get_country(ds->taxonomy) : std::string();
+ return dive_site ? taxonomy_get_country(dive_site->taxonomy) : std::string();
}
-std::string get_dive_location(const struct dive *dive)
+std::string dive::get_location() const
{
- const struct dive_site *ds = dive->dive_site;
- return ds ? ds->name : std::string();
+ return dive_site ? dive_site->name : std::string();
}
int dive::number_of_computers() const
diff --git a/core/dive.h b/core/dive.h
index 8b4d71c40..9a1fb0a8d 100644
--- a/core/dive.h
+++ b/core/dive.h
@@ -111,6 +111,8 @@ struct dive {
const cylinder_t *get_cylinder(int idx) const;
weight_t total_weight() const;
int get_salinity() const;
+ std::string get_country() const;
+ std::string get_location() const;
int depth_to_mbar(int depth) const;
double depth_to_mbarf(int depth) const;
@@ -163,9 +165,6 @@ struct dive_components {
extern fraction_t best_o2(depth_t depth, const struct dive *dive, bool in_planner);
extern fraction_t best_he(depth_t depth, const struct dive *dive, bool o2narcotic, fraction_t fo2);
-extern std::string get_dive_country(const struct dive *dive);
-extern std::string get_dive_location(const struct dive *dive);
-
extern std::unique_ptr clone_make_first_dc(const struct dive &d, int dc_number);
extern bool time_during_dive_with_offset(const struct dive *dive, timestamp_t when, timestamp_t offset);
diff --git a/core/save-git.cpp b/core/save-git.cpp
index 644053175..b1c95b85b 100644
--- a/core/save-git.cpp
+++ b/core/save-git.cpp
@@ -1099,7 +1099,7 @@ static void create_commit_message(struct membuffer *msg, bool create_empty)
} else if (!divelog.dives.empty()) {
const struct dive &dive = *divelog.dives.back();
dive_trip *trip = dive.divetrip;
- std::string location = get_dive_location(&dive);
+ std::string location = dive.get_location();
if (location.empty())
location = "no location";
const char *sep = "\n";
diff --git a/core/save-html.cpp b/core/save-html.cpp
index 9b17531e5..436fdd571 100644
--- a/core/save-html.cpp
+++ b/core/save-html.cpp
@@ -345,7 +345,7 @@ static void write_one_dive(struct membuffer *b, const struct dive &dive, const c
put_format(b, "\"subsurface_number\":%d,", dive.number);
put_HTML_date(b, dive, "\"date\":\"", "\",");
put_HTML_time(b, dive, "\"time\":\"", "\",");
- write_attribute(b, "location", get_dive_location(&dive).c_str(), ", ");
+ write_attribute(b, "location", dive.get_location().c_str(), ", ");
put_HTML_coordinates(b, dive);
put_format(b, "\"rating\":%d,", dive.rating);
put_format(b, "\"visibility\":%d,", dive.visibility);
diff --git a/core/trip.cpp b/core/trip.cpp
index 1869dbd75..6bd9ad72a 100644
--- a/core/trip.cpp
+++ b/core/trip.cpp
@@ -60,7 +60,7 @@ struct dive_trip *unregister_dive_from_trip(struct dive *dive)
std::unique_ptr create_trip_from_dive(const struct dive *dive)
{
auto trip = std::make_unique();
- trip->location = get_dive_location(dive);
+ trip->location = dive->get_location();
return trip;
}
@@ -162,7 +162,7 @@ std::vector get_dives_to_autogroup(const struct dive_
dive->when >= lastdive->when + TRIP_THRESHOLD)
break;
if (trip->location.empty())
- trip->location = get_dive_location(dive.get());
+ trip->location = dive->get_location();
lastdive = dive.get();
}
res.push_back({ i, to, trip, std::move(allocated) });
diff --git a/core/worldmap-save.cpp b/core/worldmap-save.cpp
index 4c985059a..efe1bf19a 100644
--- a/core/worldmap-save.cpp
+++ b/core/worldmap-save.cpp
@@ -59,7 +59,7 @@ static void writeMarkers(struct membuffer *b, bool selected_only)
put_HTML_watertemp(b, *dive, " ", "
");
pre = format_string_std("%s ", translate("gettextFromC", "Location:"));
put_string(b, pre.c_str());
- put_HTML_quoted(b, get_dive_location(dive.get()).c_str());
+ put_HTML_quoted(b, dive->get_location().c_str());
put_string(b, "
");
pre = format_string_std(" %s ", translate("gettextFromC", "Notes:"));
put_HTML_notes(b, *dive, pre.c_str(), "
");
diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp
index 3fc8a105c..51460ecea 100644
--- a/desktop-widgets/templatelayout.cpp
+++ b/desktop-widgets/templatelayout.cpp
@@ -526,7 +526,7 @@ QVariant TemplateLayout::getValue(QString list, QString property, const State &s
} else if (property == "timestamp") {
return QVariant::fromValue(d->when);
} else if (property == "location") {
- return QString::fromStdString(get_dive_location(d));
+ return QString::fromStdString(d->get_location());
} else if (property == "gps") {
return formatDiveGPS(d);
} else if (property == "gps_decimal") {
diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp
index aa55134a9..87cc7889c 100644
--- a/mobile-widgets/qmlmanager.cpp
+++ b/mobile-widgets/qmlmanager.cpp
@@ -1066,7 +1066,7 @@ bool QMLManager::checkLocation(DiveSiteChange &res, struct dive *d, QString loca
{
struct dive_site *ds = d->dive_site;
bool changed = false;
- QString oldLocation = QString::fromStdString(get_dive_location(d));
+ QString oldLocation = QString::fromStdString(d->get_location());
if (oldLocation != location) {
ds = divelog.sites.get_by_name(location.toStdString());
if (!ds && !location.isEmpty()) {
diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp
index 2e8cb9195..45a319b18 100644
--- a/qt-models/divetripmodel.cpp
+++ b/qt-models/divetripmodel.cpp
@@ -279,7 +279,7 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role)
case MobileListModel::DateTimeRole: return formatDiveDateTime(d);
case MobileListModel::IdRole: return d->id;
case MobileListModel::NumberRole: return d->number;
- case MobileListModel::LocationRole: return QString::fromStdString(get_dive_location(d));
+ case MobileListModel::LocationRole: return QString::fromStdString(d->get_location());
case MobileListModel::DepthRole: return get_depth_string(d->dcs[0].maxdepth.mm, true, true);
case MobileListModel::DurationRole: return formatDiveDuration(d);
case MobileListModel::DepthDurationRole: return QStringLiteral("%1 / %2").arg(get_depth_string(d->dcs[0].maxdepth.mm, true, true),
@@ -350,13 +350,13 @@ QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role)
case PHOTOS:
break;
case COUNTRY:
- return QString::fromStdString(get_dive_country(d));
+ return QString::fromStdString(d->get_country());
case BUDDIES:
return QString::fromStdString(d->buddy);
case DIVEGUIDE:
return QString::fromStdString(d->diveguide);
case LOCATION:
- return QString::fromStdString(get_dive_location(d));
+ return QString::fromStdString(d->get_location());
case GAS:
return formatDiveGasString(d);
case NOTES:
@@ -1761,13 +1761,13 @@ bool DiveTripModelList::lessThan(const QModelIndex &i1, const QModelIndex &i2) c
case PHOTOS:
return lessThanHelper(countPhotos(d1) - countPhotos(d2), row_diff);
case COUNTRY:
- return lessThanHelper(strCmp(get_dive_country(d1), get_dive_country(d2)), row_diff);
+ return lessThanHelper(strCmp(d1->get_country(), d2->get_country()), row_diff);
case BUDDIES:
return lessThanHelper(strCmp(d1->buddy, d2->buddy), row_diff);
case DIVEGUIDE:
return lessThanHelper(strCmp(d1->diveguide, d2->diveguide), row_diff);
case LOCATION:
- return lessThanHelper(strCmp(get_dive_location(d1), get_dive_location(d2)), row_diff);
+ return lessThanHelper(strCmp(d1->get_location(), d2->get_location()), row_diff);
case NOTES:
return lessThanHelper(strCmp(d1->notes, d2->notes), row_diff);
case DIVEMODE: