core: make get_dive_nr_at_idx() member of dive_table

This function implicitely accessed the global divelog. To make
that explicit make it a member of dive_table, such that the
caller must access it via the global variable.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-18 21:07:58 +02:00 committed by bstoeger
parent cce5f5950c
commit 271df8962b
3 changed files with 7 additions and 14 deletions

View file

@ -419,7 +419,7 @@ AddDive::AddDive(dive *d, bool autogroup, bool newNumber)
int idx = divelog.dives.get_insertion_index(divePtr.get());
if (newNumber)
divePtr->number = get_dive_nr_at_idx(idx);
divePtr->number = divelog.dives.get_dive_nr_at_idx(idx);
divesToAdd.dives.push_back({ std::move(divePtr), trip, site });
if (allocTrip)

View file

@ -1142,13 +1142,6 @@ process_imported_dives_result process_imported_dives(struct divelog &import_log,
return res;
}
static struct dive *get_last_valid_dive()
{
auto it = std::find_if(divelog.dives.rbegin(), divelog.dives.rend(),
[](auto &d) { return !d->invalid; });
return it != divelog.dives.rend() ? it->get() : nullptr;
}
/* return the number a dive gets when inserted at the given index.
* this function is supposed to be called *before* a dive was added.
* this returns:
@ -1156,14 +1149,14 @@ static struct dive *get_last_valid_dive()
* - last_nr+1 for addition at end of log (if last dive had a number)
* - 0 for all other cases
*/
int get_dive_nr_at_idx(int idx)
int dive_table::get_dive_nr_at_idx(int idx) const
{
if (static_cast<size_t>(idx) < divelog.dives.size())
if (static_cast<size_t>(idx) < size())
return 0;
struct dive *last_dive = get_last_valid_dive();
if (!last_dive)
auto it = std::find_if(rbegin(), rend(), [](auto &d) { return !d->invalid; });
if (it == rend())
return 1;
return last_dive->number ? last_dive->number + 1 : 0;
return (*it)->number ? (*it)->number + 1 : 0;
}
/* lookup of trip in main trip_table based on its id */

View file

@ -21,6 +21,7 @@ struct dive_table : public sorted_owning_table<dive, &comp_dives> {
void record_dive(std::unique_ptr<dive> d); // call fixup_dive() before adding dive to table.
std::unique_ptr<dive> unregister_dive(int idx);
int get_dive_nr_at_idx(int idx) const;
timestamp_t get_surface_interval(timestamp_t when) const;
struct dive *find_next_visible_dive(timestamp_t when);
};
@ -51,7 +52,6 @@ struct process_imported_dives_result {
extern process_imported_dives_result process_imported_dives(struct divelog &import_log, int flags);
extern void get_dive_gas(const struct dive *dive, int *o2_p, int *he_p, int *o2low_p);
extern int get_dive_nr_at_idx(int idx);
int get_min_datafile_version();
void report_datafile_version(int version);