core: introduce divelog structure

The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.

Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).

The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.

To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.

The whole commit is large, but was mostly an automatic
conversion.

One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-11-08 21:31:08 +01:00 committed by bstoeger
parent eebb47ec22
commit 9c253ee6c5
81 changed files with 661 additions and 698 deletions

View file

@ -2,6 +2,7 @@
#include "command_divelist.h"
#include "core/divelist.h"
#include "core/divelog.h"
#include "core/qthelper.h"
#include "core/selection.h"
#include "core/subsurface-qt/divelistnotifier.h"
@ -17,7 +18,7 @@ static void remove_trip_from_backend(dive_trip *trip)
{
if (trip->selected)
deselect_trip(trip);
remove_trip(trip, &trip_table); // Remove trip from backend
remove_trip(trip, divelog.trips); // Remove trip from backend
}
// This helper function removes a dive, takes ownership of the dive and adds it to a DiveToAdd structure.
@ -77,9 +78,9 @@ dive *DiveListBase::addDive(DiveToAdd &d)
// dives have been added, their status will be updated.
res->hidden_by_filter = true;
int idx = dive_table_get_insertion_index(&dive_table, res);
int idx = dive_table_get_insertion_index(divelog.dives, res);
fulltext_register(res); // Register the dive's fulltext cache
add_to_dive_table(&dive_table, idx, res); // Return ownership to backend
add_to_dive_table(divelog.dives, idx, res); // Return ownership to backend
invalidate_dive_cache(res); // Ensure that dive is written in git_save()
return res;
@ -210,7 +211,7 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd)
addedTrips.reserve(toAdd.trips.size());
for (OwningTripPtr &trip: toAdd.trips) {
addedTrips.push_back(trip.get());
insert_trip(trip.release(), &trip_table); // Return ownership to backend
insert_trip(trip.release(), divelog.trips); // Return ownership to backend
}
toAdd.trips.clear();
@ -300,7 +301,7 @@ static void moveDivesBetweenTrips(DivesToTrip &dives)
for (OwningTripPtr &trip: dives.tripsToAdd) {
dive_trip *t = trip.release(); // Give up ownership
createdTrips.push_back(t);
insert_trip(t, &trip_table); // Return ownership to backend
insert_trip(t, divelog.trips); // Return ownership to backend
}
dives.tripsToAdd.clear();
@ -430,7 +431,7 @@ AddDive::AddDive(dive *d, bool autogroup, bool newNumber)
allocTrip.reset(trip);
}
int idx = dive_table_get_insertion_index(&dive_table, divePtr.get());
int idx = dive_table_get_insertion_index(divelog.dives, divePtr.get());
if (newNumber)
divePtr->number = get_dive_nr_at_idx(idx);
@ -451,7 +452,7 @@ void AddDive::redoit()
currentDive = current_dive;
divesAndSitesToRemove = addDives(divesToAdd);
sort_trip_table(&trip_table); // Though unlikely, adding a dive may reorder trips
sort_trip_table(divelog.trips); // Though unlikely, adding a dive may reorder trips
// Select the newly added dive
setSelection(divesAndSitesToRemove.dives, divesAndSitesToRemove.dives[0]);
@ -461,7 +462,7 @@ void AddDive::undoit()
{
// Simply remove the dive that was previously added...
divesToAdd = removeDives(divesAndSitesToRemove);
sort_trip_table(&trip_table); // Though unlikely, removing a dive may reorder trips
sort_trip_table(divelog.trips); // Though unlikely, removing a dive may reorder trips
// ...and restore the selection
setSelection(selection, currentDive);
@ -517,9 +518,9 @@ ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, str
if (filter_presets) {
for (const filter_preset &preset: *filter_presets) {
QString name = preset.name;
auto it = std::find_if(filter_preset_table.begin(), filter_preset_table.end(),
auto it = std::find_if(divelog.filter_presets->begin(), divelog.filter_presets->end(),
[&name](const filter_preset &preset) { return preset.name == name; });
if (it != filter_preset_table.end() && it->data == preset.data)
if (it != divelog.filter_presets->end() && it->data == preset.data)
continue;
filterPresetsToAdd.emplace_back(preset.name, preset.data);
}
@ -553,7 +554,7 @@ void ImportDives::redoit()
// Add devices
for (const device &dev: devicesToAddAndRemove.devices)
add_to_device_table(&device_table, &dev);
add_to_device_table(divelog.devices, &dev);
// Add new filter presets
for (auto &it: filterPresetsToAdd) {
@ -581,7 +582,7 @@ void ImportDives::undoit()
// Remove devices
for (const device &dev: devicesToAddAndRemove.devices)
remove_device(&device_table, &dev);
remove_device(divelog.devices, &dev);
// Remove filter presets. Do this in reverse order.
for (auto it = filterPresetsToRemove.rbegin(); it != filterPresetsToRemove.rend(); ++it) {
@ -610,7 +611,7 @@ bool DeleteDive::workToBeDone()
void DeleteDive::undoit()
{
divesToDelete = addDives(divesToAdd);
sort_trip_table(&trip_table); // Though unlikely, removing a dive may reorder trips
sort_trip_table(divelog.trips); // Though unlikely, removing a dive may reorder trips
// Select all re-added dives and make the first one current
dive *currentDive = !divesToDelete.dives.empty() ? divesToDelete.dives[0] : nullptr;
@ -620,7 +621,7 @@ void DeleteDive::undoit()
void DeleteDive::redoit()
{
divesToAdd = removeDives(divesToDelete);
sort_trip_table(&trip_table); // Though unlikely, adding a dive may reorder trips
sort_trip_table(divelog.trips); // Though unlikely, adding a dive may reorder trips
// Deselect all dives and select dive that was close to the first deleted dive
dive *newCurrent = nullptr;
@ -648,8 +649,8 @@ void ShiftTime::redoit()
}
// Changing times may have unsorted the dive and trip tables
sort_dive_table(&dive_table);
sort_trip_table(&trip_table);
sort_dive_table(divelog.dives);
sort_trip_table(divelog.trips);
for (dive_trip *trip: trips)
sort_dive_table(&trip->dives); // Keep the trip-table in order
@ -717,7 +718,7 @@ bool TripBase::workToBeDone()
void TripBase::redoit()
{
moveDivesBetweenTrips(divesToMove);
sort_trip_table(&trip_table); // Though unlikely, moving dives may reorder trips
sort_trip_table(divelog.trips); // Though unlikely, moving dives may reorder trips
// Select the moved dives
std::vector<dive *> dives;
@ -790,7 +791,7 @@ AutogroupDives::AutogroupDives()
dive_trip *trip;
bool alloc;
int from, to;
for(int i = 0; (trip = get_dives_to_autogroup(&dive_table, i, &from, &to, &alloc)) != NULL; i = to) {
for(int i = 0; (trip = get_dives_to_autogroup(divelog.dives, i, &from, &to, &alloc)) != NULL; i = to) {
// If this is an allocated trip, take ownership
if (alloc)
divesToMove.tripsToAdd.emplace_back(trip);
@ -1009,7 +1010,7 @@ MergeDives::MergeDives(const QVector <dive *> &dives)
// like the best option.
int idx = get_divenr(dives[0]);
int num = dives.count();
if (idx < 0 || idx + num > dive_table.nr) {
if (idx < 0 || idx + num > divelog.dives->nr) {
// It was the callers responsibility to pass only known dives.
// Something is seriously wrong - give up.
qWarning("Merging unknown dives");
@ -1017,7 +1018,7 @@ MergeDives::MergeDives(const QVector <dive *> &dives)
}
// std::equal compares two ranges. The parameters are (begin_range1, end_range1, begin_range2).
// Here, we can compare C-arrays, because QVector guarantees contiguous storage.
if (std::equal(&dives[0], &dives[0] + num, &dive_table.dives[idx]) &&
if (std::equal(&dives[0], &dives[0] + num, &divelog.dives->dives[idx]) &&
dives[0]->number && dives.last()->number && dives[0]->number < dives.last()->number) {
// We have a consecutive set of dives. Rename all following dives according to the
// number of erased dives. This considers that there might be missing numbers.
@ -1033,15 +1034,15 @@ MergeDives::MergeDives(const QVector <dive *> &dives)
// consecutive, and the difference will be 1, so the
// above example is not supposed to be normal.
int diff = dives.last()->number - dives[0]->number;
divesToRenumber.reserve(dive_table.nr - idx - num);
divesToRenumber.reserve(divelog.dives->nr - idx - num);
int previousnr = dives[0]->number;
for (int i = idx + num; i < dive_table.nr; ++i) {
int newnr = dive_table.dives[i]->number - diff;
for (int i = idx + num; i < divelog.dives->nr; ++i) {
int newnr = divelog.dives->dives[i]->number - diff;
// Stop renumbering if stuff isn't in order (see also core/divelist.c)
if (newnr <= previousnr)
break;
divesToRenumber.append(QPair<dive *,int>(dive_table.dives[i], newnr));
divesToRenumber.append(QPair<dive *,int>(divelog.dives->dives[i], newnr));
previousnr = newnr;
}
}