core: include trip table directly in divelog

Having this as a pointer is an artifact from the C/C++ split.
The triptable header is small enough so that we can
include it directly

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-08 16:53:55 +02:00 committed by bstoeger
parent 5af9d28291
commit 2bdcdab391
13 changed files with 39 additions and 39 deletions

View file

@ -16,7 +16,7 @@ static std::unique_ptr<dive_trip> remove_trip_from_backend(dive_trip *trip)
{ {
if (trip->selected) if (trip->selected)
deselect_trip(trip); deselect_trip(trip);
auto [t, idx] = divelog.trips->pull(trip); auto [t, idx] = divelog.trips.pull(trip);
return std::move(t); return std::move(t);
} }
@ -39,7 +39,7 @@ DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector<std::unique_ptr<d
diveSiteCountChanged(d->dive_site); diveSiteCountChanged(d->dive_site);
res.site = unregister_dive_from_dive_site(d); res.site = unregister_dive_from_dive_site(d);
if (res.trip && res.trip->dives.empty()) { if (res.trip && res.trip->dives.empty()) {
divelog.trips->sort(); // Removal of dives has changed order of trips! (TODO: remove this) divelog.trips.sort(); // Removal of dives has changed order of trips! (TODO: remove this)
auto trip = remove_trip_from_backend(res.trip); // Remove trip from backend auto trip = remove_trip_from_backend(res.trip); // Remove trip from backend
tripsToAdd.push_back(std::move(trip)); // Take ownership of trip tripsToAdd.push_back(std::move(trip)); // Take ownership of trip
} }
@ -200,7 +200,7 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd)
std::vector<dive_trip *> addedTrips; std::vector<dive_trip *> addedTrips;
addedTrips.reserve(toAdd.trips.size()); addedTrips.reserve(toAdd.trips.size());
for (std::unique_ptr<dive_trip> &trip: toAdd.trips) { for (std::unique_ptr<dive_trip> &trip: toAdd.trips) {
auto [t, idx] = divelog.trips->put(std::move(trip)); // Return ownership to backend auto [t, idx] = divelog.trips.put(std::move(trip)); // Return ownership to backend
addedTrips.push_back(t); addedTrips.push_back(t);
} }
toAdd.trips.clear(); toAdd.trips.clear();
@ -287,7 +287,7 @@ static void moveDivesBetweenTrips(DivesToTrip &dives)
// First, bring back the trip(s) // First, bring back the trip(s)
for (std::unique_ptr<dive_trip> &trip: dives.tripsToAdd) { for (std::unique_ptr<dive_trip> &trip: dives.tripsToAdd) {
auto [t, idx] = divelog.trips->put(std::move(trip)); // Return ownership to backend auto [t, idx] = divelog.trips.put(std::move(trip)); // Return ownership to backend
createdTrips.push_back(t); createdTrips.push_back(t);
} }
dives.tripsToAdd.clear(); dives.tripsToAdd.clear();
@ -438,7 +438,7 @@ void AddDive::redoit()
currentDive = current_dive; currentDive = current_dive;
divesAndSitesToRemove = addDives(divesToAdd); divesAndSitesToRemove = addDives(divesToAdd);
divelog.trips->sort(); // Though unlikely, adding a dive may reorder trips divelog.trips.sort(); // Though unlikely, adding a dive may reorder trips
// Select the newly added dive // Select the newly added dive
setSelection(divesAndSitesToRemove.dives, divesAndSitesToRemove.dives[0], -1); setSelection(divesAndSitesToRemove.dives, divesAndSitesToRemove.dives[0], -1);
@ -448,7 +448,7 @@ void AddDive::undoit()
{ {
// Simply remove the dive that was previously added... // Simply remove the dive that was previously added...
divesToAdd = removeDives(divesAndSitesToRemove); divesToAdd = removeDives(divesAndSitesToRemove);
divelog.trips->sort(); // Though unlikely, removing a dive may reorder trips divelog.trips.sort(); // Though unlikely, removing a dive may reorder trips
// ...and restore the selection // ...and restore the selection
setSelection(selection, currentDive, -1); setSelection(selection, currentDive, -1);
@ -583,7 +583,7 @@ bool DeleteDive::workToBeDone()
void DeleteDive::undoit() void DeleteDive::undoit()
{ {
divesToDelete = addDives(divesToAdd); divesToDelete = addDives(divesToAdd);
divelog.trips->sort(); // Though unlikely, removing a dive may reorder trips divelog.trips.sort(); // Though unlikely, removing a dive may reorder trips
// Select all re-added dives and make the first one current // Select all re-added dives and make the first one current
dive *currentDive = !divesToDelete.dives.empty() ? divesToDelete.dives[0] : nullptr; dive *currentDive = !divesToDelete.dives.empty() ? divesToDelete.dives[0] : nullptr;
@ -593,7 +593,7 @@ void DeleteDive::undoit()
void DeleteDive::redoit() void DeleteDive::redoit()
{ {
divesToAdd = removeDives(divesToDelete); divesToAdd = removeDives(divesToDelete);
divelog.trips->sort(); // Though unlikely, adding a dive may reorder trips divelog.trips.sort(); // Though unlikely, adding a dive may reorder trips
// Deselect all dives and select dive that was close to the first deleted dive // Deselect all dives and select dive that was close to the first deleted dive
dive *newCurrent = nullptr; dive *newCurrent = nullptr;
@ -622,7 +622,7 @@ void ShiftTime::redoit()
// Changing times may have unsorted the dive and trip tables // Changing times may have unsorted the dive and trip tables
divelog.dives.sort(); divelog.dives.sort();
divelog.trips->sort(); divelog.trips.sort();
for (dive_trip *trip: trips) for (dive_trip *trip: trips)
trip->sort_dives(); trip->sort_dives();
@ -690,7 +690,7 @@ bool TripBase::workToBeDone()
void TripBase::redoit() void TripBase::redoit()
{ {
moveDivesBetweenTrips(divesToMove); moveDivesBetweenTrips(divesToMove);
divelog.trips->sort(); // Though unlikely, moving dives may reorder trips divelog.trips.sort(); // Though unlikely, moving dives may reorder trips
// Select the moved dives // Select the moved dives
std::vector<dive *> dives; std::vector<dive *> dives;

View file

@ -1442,7 +1442,7 @@ void EditDive::exchangeDives()
timestamp_t delta = oldDive->when - newDive->when; timestamp_t delta = oldDive->when - newDive->when;
if (delta != 0) { if (delta != 0) {
divelog.dives.sort(); divelog.dives.sort();
divelog.trips->sort(); divelog.trips.sort();
if (newDive->divetrip != oldDive->divetrip) if (newDive->divetrip != oldDive->divetrip)
qWarning("Command::EditDive::redo(): This command does not support moving between trips!"); qWarning("Command::EditDive::redo(): This command does not support moving between trips!");
if (oldDive->divetrip) if (oldDive->divetrip)

View file

@ -727,10 +727,10 @@ struct dive *register_dive(std::unique_ptr<dive> d)
void process_loaded_dives() void process_loaded_dives()
{ {
divelog.dives.sort(); divelog.dives.sort();
divelog.trips->sort(); divelog.trips.sort();
/* Autogroup dives if desired by user. */ /* Autogroup dives if desired by user. */
autogroup_dives(divelog.dives, *divelog.trips); autogroup_dives(divelog.dives, divelog.trips);
fulltext_populate(); fulltext_populate();
@ -926,7 +926,7 @@ void add_imported_dives(struct divelog &import_log, int flags)
/* Add new trips */ /* Add new trips */
for (auto &trip: trips_to_add) for (auto &trip: trips_to_add)
divelog.trips->put(std::move(trip)); divelog.trips.put(std::move(trip));
trips_to_add.clear(); trips_to_add.clear();
/* Add new dive sites */ /* Add new dive sites */
@ -960,7 +960,7 @@ static bool try_to_merge_trip(dive_trip &trip_import, struct dive_table &import_
struct dive_table &dives_to_add, std::vector<dive *> &dives_to_remove, struct dive_table &dives_to_add, std::vector<dive *> &dives_to_remove,
bool &sequence_changed, int &start_renumbering_at) bool &sequence_changed, int &start_renumbering_at)
{ {
for (auto &trip_old: *divelog.trips) { for (auto &trip_old: divelog.trips) {
if (trips_overlap(trip_import, *trip_old)) { if (trips_overlap(trip_import, *trip_old)) {
sequence_changed |= merge_dive_tables(trip_import.dives, import_table, trip_old->dives, sequence_changed |= merge_dive_tables(trip_import.dives, import_table, trip_old->dives,
prefer_imported, trip_old.get(), prefer_imported, trip_old.get(),
@ -1044,7 +1044,7 @@ process_imported_dives_result process_imported_dives(struct divelog &import_log,
/* Autogroup tripless dives if desired by user. But don't autogroup /* Autogroup tripless dives if desired by user. But don't autogroup
* if tripless dives should be added to a new trip. */ * if tripless dives should be added to a new trip. */
if (!(flags & IMPORT_ADD_TO_NEW_TRIP)) if (!(flags & IMPORT_ADD_TO_NEW_TRIP))
autogroup_dives(import_log.dives, *import_log.trips); autogroup_dives(import_log.dives, import_log.trips);
/* If dive sites already exist, use the existing versions. */ /* If dive sites already exist, use the existing versions. */
for (auto &new_ds: import_log.sites) { for (auto &new_ds: import_log.sites) {
@ -1072,7 +1072,7 @@ process_imported_dives_result process_imported_dives(struct divelog &import_log,
* could be smarter here, but realistically not a whole lot of trips * could be smarter here, but realistically not a whole lot of trips
* will be imported so do a simple n*m loop until someone complains. * will be imported so do a simple n*m loop until someone complains.
*/ */
for (auto &trip_import: *import_log.trips) { for (auto &trip_import: import_log.trips) {
if ((flags & IMPORT_MERGE_ALL_TRIPS) || trip_import->autogen) { if ((flags & IMPORT_MERGE_ALL_TRIPS) || trip_import->autogen) {
if (try_to_merge_trip(*trip_import, import_log.dives, flags & IMPORT_PREFER_IMPORTED, if (try_to_merge_trip(*trip_import, import_log.dives, flags & IMPORT_PREFER_IMPORTED,
res.dives_to_add, res.dives_to_remove, res.dives_to_add, res.dives_to_remove,
@ -1096,7 +1096,7 @@ process_imported_dives_result process_imported_dives(struct divelog &import_log,
/* Finally, add trip to list of trips to add */ /* Finally, add trip to list of trips to add */
res.trips_to_add.put(std::move(trip_import)); res.trips_to_add.put(std::move(trip_import));
} }
import_log.trips->clear(); /* All trips were consumed */ import_log.trips.clear(); /* All trips were consumed */
if ((flags & IMPORT_ADD_TO_NEW_TRIP) && !import_log.dives.empty()) { if ((flags & IMPORT_ADD_TO_NEW_TRIP) && !import_log.dives.empty()) {
/* Create a new trip for unassigned dives, if desired. */ /* Create a new trip for unassigned dives, if desired. */

View file

@ -11,7 +11,6 @@
struct divelog divelog; struct divelog divelog;
divelog::divelog() : divelog::divelog() :
trips(std::make_unique<trip_table>()),
filter_presets(std::make_unique<filter_preset_table>()), filter_presets(std::make_unique<filter_preset_table>()),
autogroup(false) autogroup(false)
{ {
@ -34,10 +33,10 @@ void divelog::delete_single_dive(int idx)
// Deleting a dive may change the order of trips! // Deleting a dive may change the order of trips!
if (trip) if (trip)
trips->sort(); trips.sort();
if (trip && trip->dives.empty()) if (trip && trip->dives.empty())
trips->pull(trip); trips.pull(trip);
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);
dives.erase(dives.begin() + idx); dives.erase(dives.begin() + idx);
} }
@ -51,7 +50,7 @@ void divelog::delete_multiple_dives(const std::vector<dive *> &dives_to_delete)
struct dive_trip *trip = unregister_dive_from_trip(d); struct dive_trip *trip = unregister_dive_from_trip(d);
if (trip && trip->dives.empty()) { if (trip && trip->dives.empty()) {
trips_changed = true; trips_changed = true;
trips->pull(trip); trips.pull(trip);
} }
unregister_dive_from_dive_site(d); unregister_dive_from_dive_site(d);
@ -60,14 +59,14 @@ void divelog::delete_multiple_dives(const std::vector<dive *> &dives_to_delete)
// Deleting a dive may change the order of trips! // Deleting a dive may change the order of trips!
if (trips_changed) if (trips_changed)
trips->sort(); trips.sort();
} }
void divelog::clear() void divelog::clear()
{ {
dives.clear(); dives.clear();
sites.clear(); sites.clear();
trips->clear(); trips.clear();
devices.clear(); devices.clear();
filter_presets->clear(); filter_presets->clear();
} }

View file

@ -5,6 +5,7 @@
#include "divelist.h" #include "divelist.h"
#include "divesitetable.h" #include "divesitetable.h"
#include "triptable.h"
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -15,7 +16,7 @@ struct filter_preset_table;
struct divelog { struct divelog {
dive_table dives; dive_table dives;
std::unique_ptr<trip_table> trips; trip_table trips;
dive_site_table sites; dive_site_table sites;
std::vector<device> devices; std::vector<device> devices;
std::unique_ptr<filter_preset_table> filter_presets; std::unique_ptr<filter_preset_table> filter_presets;

View file

@ -1384,7 +1384,7 @@ static void finish_active_trip(struct git_parser_state *state)
auto &trip = state->active_trip; auto &trip = state->active_trip;
if (trip) if (trip)
state->log->trips->put(std::move(trip)); state->log->trips.put(std::move(trip));
} }
static void finish_active_dive(struct git_parser_state *state) static void finish_active_dive(struct git_parser_state *state)

View file

@ -288,7 +288,7 @@ void trip_end(struct parser_state *state)
{ {
if (!state->cur_trip) if (!state->cur_trip)
return; return;
state->log->trips->put(std::move(state->cur_trip)); state->log->trips.put(std::move(state->cur_trip));
} }
void picture_start(struct parser_state *state) void picture_start(struct parser_state *state)

View file

@ -985,7 +985,7 @@ static int create_git_tree(git_repository *repo, struct dir *root, bool select_o
save_divesites(repo, root); save_divesites(repo, root);
save_filter_presets(repo, root); save_filter_presets(repo, root);
for (auto &trip: *divelog.trips) for (auto &trip: divelog.trips)
trip->saved = false; trip->saved = false;
/* save the dives */ /* save the dives */

View file

@ -436,7 +436,7 @@ static void write_trips(struct membuffer *b, const char *photos_dir, bool select
char sep_ = ' '; char sep_ = ' ';
char *sep = &sep_; char *sep = &sep_;
for (auto &trip: *divelog.trips) for (auto &trip: divelog.trips)
trip->saved = 0; trip->saved = 0;
for (auto &dive: divelog.dives) { for (auto &dive: divelog.dives) {

View file

@ -678,7 +678,7 @@ static void save_dives_buffer(struct membuffer *b, bool select_only, bool anonym
put_format(b, "</site>\n"); put_format(b, "</site>\n");
} }
put_format(b, "</divesites>\n<dives>\n"); put_format(b, "</divesites>\n<dives>\n");
for (auto &trip: *divelog.trips) for (auto &trip: divelog.trips)
trip->saved = 0; trip->saved = 0;
/* save the filter presets */ /* save the filter presets */

View file

@ -156,7 +156,7 @@ QVector<dive *> setSelectionCore(const std::vector<dive *> &selection, dive *cur
static void clear_trip_selection() static void clear_trip_selection()
{ {
amount_trips_selected = 0; amount_trips_selected = 0;
for (auto &trip: *divelog.trips) for (auto &trip: divelog.trips)
trip->selected = false; trip->selected = false;
} }
@ -202,7 +202,7 @@ void setTripSelection(dive_trip *trip, dive *currentDive)
current_dive = currentDive; current_dive = currentDive;
for (auto &d: divelog.dives) for (auto &d: divelog.dives)
d->selected = d->divetrip == trip; d->selected = d->divetrip == trip;
for (auto &t: *divelog.trips) for (auto &t: divelog.trips)
t->selected = t.get() == trip; t->selected = t.get() == trip;
amount_selected = static_cast<int>(trip->dives.size()); amount_selected = static_cast<int>(trip->dives.size());
@ -291,7 +291,7 @@ struct dive_trip *single_selected_trip()
{ {
if (amount_trips_selected != 1) if (amount_trips_selected != 1)
return NULL; return NULL;
for (auto &trip: *divelog.trips) { for (auto &trip: divelog.trips) {
if (trip->selected) if (trip->selected)
return trip.get(); return trip.get();
} }

View file

@ -18,8 +18,8 @@ TripSelectionDialog::TripSelectionDialog(QWidget *parent) : QDialog(parent)
// We could use a model, but it seems barely worth the hassle. // We could use a model, but it seems barely worth the hassle.
QStringList list; QStringList list;
list.reserve(divelog.trips->size()); list.reserve(divelog.trips.size());
for (auto &trip: *divelog.trips) for (auto &trip: divelog.trips)
list.push_back(formatTripTitleWithDives(*trip)); list.push_back(formatTripTitleWithDives(*trip));
ui.trips->addItems(list); ui.trips->addItems(list);
} }
@ -37,9 +37,9 @@ dive_trip *TripSelectionDialog::selectedTrip() const
if (rows.size() != 1) if (rows.size() != 1)
return nullptr; return nullptr;
int idx = rows[0].row(); int idx = rows[0].row();
if (idx < 0 || static_cast<size_t>(idx) >= divelog.trips->size()) if (idx < 0 || static_cast<size_t>(idx) >= divelog.trips.size())
return nullptr; return nullptr;
return (*divelog.trips)[idx].get(); return divelog.trips[idx].get();
} }
dive_trip *TripSelectionDialog::getTrip() dive_trip *TripSelectionDialog::getTrip()

View file

@ -1369,7 +1369,7 @@ void QMLManager::commitChanges(QString diveId, QString number, QString date, QSt
void QMLManager::updateTripDetails(QString tripIdString, QString tripLocation, QString tripNotes) void QMLManager::updateTripDetails(QString tripIdString, QString tripLocation, QString tripNotes)
{ {
int tripId = tripIdString.toInt(); int tripId = tripIdString.toInt();
dive_trip *trip = divelog.trips->get_by_uniq_id(tripId); dive_trip *trip = divelog.trips.get_by_uniq_id(tripId);
if (!trip) { if (!trip) {
report_info("updateTripData: cannot find trip for tripId %s", qPrintable(tripIdString)); report_info("updateTripData: cannot find trip for tripId %s", qPrintable(tripIdString));
return; return;
@ -1428,7 +1428,7 @@ void QMLManager::addDiveToTrip(int id, int tripId)
appendTextToLog(QString("Asked to add non-existing dive with id %1 to trip %2.").arg(id).arg(tripId)); appendTextToLog(QString("Asked to add non-existing dive with id %1 to trip %2.").arg(id).arg(tripId));
return; return;
} }
struct dive_trip *dt = divelog.trips->get_by_uniq_id(tripId); struct dive_trip *dt = divelog.trips.get_by_uniq_id(tripId);
if (!dt) { if (!dt) {
appendTextToLog(QString("Asked to add dive with id %1 to trip with id %2 which cannot be found.").arg(id).arg(tripId)); appendTextToLog(QString("Asked to add dive with id %1 to trip with id %2 which cannot be found.").arg(id).arg(tripId));
return; return;