core: move *_surface_pressure() functions into struct dive

Seems 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-22 20:08:47 +02:00 committed by bstoeger
parent 4a165980e7
commit d81ca005ab
6 changed files with 14 additions and 15 deletions

View file

@ -624,13 +624,13 @@ static bool is_potentially_redundant(const struct event &event)
return true;
}
pressure_t calculate_surface_pressure(const struct dive *dive)
pressure_t dive::calculate_surface_pressure() const
{
pressure_t res;
int sum = 0, nr = 0;
bool logged = dive->is_logged();
for (auto &dc: dive->dcs) {
bool logged = is_logged();
for (auto &dc: dcs) {
if ((logged || !is_dc_planner(&dc)) && dc.surface_pressure.mbar) {
sum += dc.surface_pressure.mbar;
nr++;
@ -642,18 +642,16 @@ pressure_t calculate_surface_pressure(const struct dive *dive)
static void fixup_surface_pressure(struct dive *dive)
{
dive->surface_pressure = calculate_surface_pressure(dive);
dive->surface_pressure = dive->calculate_surface_pressure();
}
/* if the surface pressure in the dive data is redundant to the calculated
* value (i.e., it was added by running fixup on the dive) return 0,
* otherwise return the surface pressure given in the dive */
pressure_t un_fixup_surface_pressure(const struct dive *d)
pressure_t dive::un_fixup_surface_pressure() const
{
pressure_t res = d->surface_pressure;
if (res.mbar && res.mbar == calculate_surface_pressure(d).mbar)
res.mbar = 0;
return res;
return surface_pressure.mbar == calculate_surface_pressure().mbar ?
pressure_t() : surface_pressure;
}
static void fixup_water_salinity(struct dive *dive)

View file

@ -94,6 +94,9 @@ struct dive {
double depth_to_atm(int depth) const;
int rel_mbar_to_depth(int mbar) const;
int mbar_to_depth(int mbar) const;
pressure_t calculate_surface_pressure() const;
pressure_t un_fixup_surface_pressure() const;
};
/* For the top-level list: an entry is either a dive or a trip */
@ -180,8 +183,6 @@ extern bool dive_less_than(const struct dive &a, const struct dive &b);
extern bool dive_less_than_ptr(const struct dive *a, const struct dive *b);
extern bool dive_or_trip_less_than(struct dive_or_trip a, struct dive_or_trip b);
extern struct dive *fixup_dive(struct dive *dive);
extern pressure_t calculate_surface_pressure(const struct dive *dive);
extern pressure_t un_fixup_surface_pressure(const struct dive *d);
extern int get_dive_salinity(const struct dive *dive);
extern int dive_getUniqID();
extern std::array<std::unique_ptr<dive>, 2> split_dive(const struct dive &dive);

View file

@ -434,7 +434,7 @@ static void save_dc(struct membuffer *b, const struct dive &dive, const struct d
*/
static void create_dive_buffer(const struct dive &dive, struct membuffer *b)
{
pressure_t surface_pressure = un_fixup_surface_pressure(&dive);
pressure_t surface_pressure = dive.un_fixup_surface_pressure();
if (dive.dcs[0].duration.seconds > 0)
put_format(b, "duration %u:%02u min\n", FRACTION_TUPLE(dive.dcs[0].duration.seconds, 60));
SAVE("rating", rating);

View file

@ -476,7 +476,7 @@ static void save_picture(struct membuffer *b, const struct picture &pic)
void save_one_dive_to_mb(struct membuffer *b, const struct dive &dive, bool anonymize)
{
pressure_t surface_pressure = un_fixup_surface_pressure(&dive);
pressure_t surface_pressure = dive.un_fixup_surface_pressure();
put_string(b, "<dive");
if (dive.number)

View file

@ -228,13 +228,13 @@ const char *monthname(int mon)
int gettimezoneoffset()
{
time_t now = time(nullptr);
#ifdef WIN32
// Somewhat surprisingly, Windows doesn't have localtime_r (I thought this was POSIX?).
// Let's use the global timezone variable.
// Ultimately, use the portable C++20 API.
return static_cast<int>(-timezone);
#else
time_t now = time(nullptr);
struct tm local;
localtime_r(&now, &local);
return local.tm_gmtoff;

View file

@ -455,7 +455,7 @@ void TabDiveInformation::updateTextBox(int event) // Either the text box has bee
}
break;
case 2: // i.e. event = COMBO_CHANGED, that is, the option "Use dc" was selected from combobox
atmpress = calculate_surface_pressure(currentDive); // re-calculate air pressure from dc data
atmpress = currentDive->calculate_surface_pressure(); // re-calculate air pressure from dc data
ui->atmPressVal->setText(QString::number(atmpress.mbar)); // display it in text box
setIndexNoSignal(ui->atmPressType, 0); // reset combobox to mbar
break;