core: move get_dive_dc() to struct dive

Feels natural in a C++ code base.

This removes a nullptr-check so some care has to be taken.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-30 20:38:12 +02:00 committed by bstoeger
parent 731052c776
commit f1f082d86a
15 changed files with 59 additions and 55 deletions

View file

@ -246,7 +246,7 @@ void copy_events_until(const struct dive *sd, struct dive *dd, int dcNr, int tim
return;
const struct divecomputer *s = &sd->dcs[0];
struct divecomputer *d = get_dive_dc(dd, dcNr);
struct divecomputer *d = dd->get_dc(dcNr);
if (!s || !d)
return;
@ -2513,17 +2513,17 @@ int dive::number_of_computers() const
return static_cast<int>(dcs.size());
}
struct divecomputer *get_dive_dc(struct dive *dive, int nr)
struct divecomputer *dive::get_dc(int nr)
{
if (!dive || dive->dcs.empty())
if (dcs.empty()) // Can't happen!
return NULL;
nr = std::max(0, nr);
return &dive->dcs[static_cast<size_t>(nr) % dive->dcs.size()];
return &dcs[static_cast<size_t>(nr) % dcs.size()];
}
const struct divecomputer *get_dive_dc(const struct dive *dive, int nr)
const struct divecomputer *dive::get_dc(int nr) const
{
return get_dive_dc((struct dive *)dive, nr);
return const_cast<dive &>(*this).get_dc(nr);
}
bool dive::dive_has_gps_location() const

View file

@ -82,6 +82,9 @@ struct dive {
void invalidate_cache();
bool cache_is_valid() const;
struct divecomputer *get_dc(int nr);
const struct divecomputer *get_dc(int nr) const;
void clear();
int number_of_computers() const;
void fixup_no_cylinder(); /* to fix cylinders, we need the divelist (to calculate cns) */
@ -162,8 +165,6 @@ extern fraction_t best_he(depth_t depth, const struct dive *dive, bool o2narcoti
extern std::string get_dive_country(const struct dive *dive);
extern std::string get_dive_location(const struct dive *dive);
extern struct divecomputer *get_dive_dc(struct dive *dive, int nr);
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);

View file

@ -667,7 +667,7 @@ bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, i
int laststoptime = timestep;
bool o2breaking = false;
int decostopcounter = 0;
struct divecomputer *dc = get_dive_dc(dive, dcNr);
struct divecomputer *dc = dive->get_dc(dcNr);
enum divemode_t divemode = dc->divemode;
set_gf(diveplan->gflow, diveplan->gfhigh);