From 72bb38601d0c53b35f2fbcfd8149392ea16de38c Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 25 Jun 2024 14:04:01 +0200 Subject: [PATCH] core: move invalidate_dive_cache() to struct dive Seems natural in a C++ code base. Signed-off-by: Berthold Stoeger --- commands/command_divelist.cpp | 4 ++-- commands/command_edit.cpp | 34 +++++++++++++++++----------------- commands/command_event.cpp | 2 +- commands/command_pictures.cpp | 6 +++--- core/dive.cpp | 12 ++++++------ core/dive.h | 10 ++++------ core/divelist.cpp | 4 ++-- core/load-git.cpp | 2 +- core/save-git.cpp | 4 ++-- desktop-widgets/mainwindow.cpp | 4 ++-- mobile-widgets/qmlmanager.cpp | 4 ++-- 11 files changed, 42 insertions(+), 44 deletions(-) diff --git a/commands/command_divelist.cpp b/commands/command_divelist.cpp index f4959ad3e..3c216eb58 100644 --- a/commands/command_divelist.cpp +++ b/commands/command_divelist.cpp @@ -239,7 +239,7 @@ static void renumberDives(QVector> &divesToRenumber) continue; std::swap(d->number, pair.second); dives.push_back(d); - invalidate_dive_cache(d); + d->invalidate_cache(); } // Send signals. @@ -268,7 +268,7 @@ static std::unique_ptr moveDiveToTrip(DiveToTrip &diveToTrip) std::swap(trip, diveToTrip.trip); if (trip) trip->add_dive(diveToTrip.dive); - invalidate_dive_cache(diveToTrip.dive); // Ensure that dive is written in git_save() + diveToTrip.dive->invalidate_cache(); // Ensure that dive is written in git_save() return res; } diff --git a/commands/command_edit.cpp b/commands/command_edit.cpp index 0816ffb30..62f205e3f 100644 --- a/commands/command_edit.cpp +++ b/commands/command_edit.cpp @@ -149,7 +149,7 @@ void EditBase::undo() for (dive *d: dives) { set(d, value); fulltext_register(d); // Update the fulltext cache - invalidate_dive_cache(d); // Ensure that dive is written in git_save() + d->invalidate_cache(); // Ensure that dive is written in git_save() } std::swap(old, value); @@ -540,7 +540,7 @@ void EditTagsBase::undo() } set(d, tags); fulltext_register(d); // Update the fulltext cache - invalidate_dive_cache(d); // Ensure that dive is written in git_save() + d->invalidate_cache(); // Ensure that dive is written in git_save() } std::swap(tagsToAdd, tagsToRemove); @@ -741,7 +741,7 @@ void PasteDives::undo() for (PasteState &state: dives) { divesToNotify.push_back(state.d); state.swap(what); - invalidate_dive_cache(state.d); // Ensure that dive is written in git_save() + state.d->invalidate_cache(); // Ensure that dive is written in git_save() } // Send signals. @@ -825,7 +825,7 @@ void ReplanDive::undo() std::swap(d->duration, duration); std::swap(d->salinity, salinity); divelog.dives.fixup_dive(*d); - invalidate_dive_cache(d); // Ensure that dive is written in git_save() + d->invalidate_cache(); // Ensure that dive is written in git_save() QVector divesToNotify = { d }; // Note that we have to emit cylindersReset before divesChanged, because the divesChanged @@ -900,7 +900,7 @@ void EditProfile::undo() std::swap(d->meandepth, meandepth); std::swap(d->duration, duration); divelog.dives.fixup_dive(*d); - invalidate_dive_cache(d); // Ensure that dive is written in git_save() + d->invalidate_cache(); // Ensure that dive is written in git_save() QVector divesToNotify = { d }; emit diveListNotifier.divesChanged(divesToNotify, DiveField::DURATION | DiveField::DEPTH); @@ -936,7 +936,7 @@ void AddWeight::undo() continue; d->weightsystems.pop_back(); emit diveListNotifier.weightRemoved(d, d->weightsystems.size()); - invalidate_dive_cache(d); // Ensure that dive is written in git_save() + d->invalidate_cache(); // Ensure that dive is written in git_save() } } @@ -945,7 +945,7 @@ void AddWeight::redo() for (dive *d: dives) { d->weightsystems.emplace_back(); emit diveListNotifier.weightAdded(d, d->weightsystems.size() - 1); - invalidate_dive_cache(d); // Ensure that dive is written in git_save() + d->invalidate_cache(); // Ensure that dive is written in git_save() } } @@ -1011,7 +1011,7 @@ void RemoveWeight::undo() for (size_t i = 0; i < dives.size(); ++i) { add_to_weightsystem_table(&dives[i]->weightsystems, indices[i], ws); emit diveListNotifier.weightAdded(dives[i], indices[i]); - invalidate_dive_cache(dives[i]); // Ensure that dive is written in git_save() + dives[i]->invalidate_cache(); // Ensure that dive is written in git_save() } } @@ -1020,7 +1020,7 @@ void RemoveWeight::redo() for (size_t i = 0; i < dives.size(); ++i) { remove_weightsystem(dives[i], indices[i]); emit diveListNotifier.weightRemoved(dives[i], indices[i]); - invalidate_dive_cache(dives[i]); // Ensure that dive is written in git_save() + dives[i]->invalidate_cache(); // Ensure that dive is written in git_save() } } @@ -1063,7 +1063,7 @@ void EditWeight::redo() add_weightsystem_description(new_ws); // This updates the weightsystem info table set_weightsystem(dives[i], indices[i], new_ws); emit diveListNotifier.weightEdited(dives[i], indices[i]); - invalidate_dive_cache(dives[i]); // Ensure that dive is written in git_save() + dives[i]->invalidate_cache(); // Ensure that dive is written in git_save() } std::swap(ws, new_ws); } @@ -1103,7 +1103,7 @@ void AddCylinder::undo() remove_cylinder(dives[i], indexes[i]); divelog.dives.update_cylinder_related_info(*dives[i]); emit diveListNotifier.cylinderRemoved(dives[i], indexes[i]); - invalidate_dive_cache(dives[i]); // Ensure that dive is written in git_save() + dives[i]->invalidate_cache(); // Ensure that dive is written in git_save() } } @@ -1116,7 +1116,7 @@ void AddCylinder::redo() add_cylinder(&d->cylinders, index, cyl); divelog.dives.update_cylinder_related_info(*d); emit diveListNotifier.cylinderAdded(d, index); - invalidate_dive_cache(d); // Ensure that dive is written in git_save() + d->invalidate_cache(); // Ensure that dive is written in git_save() } } @@ -1203,7 +1203,7 @@ void RemoveCylinder::undo() cylinder_renumber(*dives[i], &mapping[0]); divelog.dives.update_cylinder_related_info(*dives[i]); emit diveListNotifier.cylinderAdded(dives[i], indexes[i]); - invalidate_dive_cache(dives[i]); // Ensure that dive is written in git_save() + dives[i]->invalidate_cache(); // Ensure that dive is written in git_save() } } @@ -1215,7 +1215,7 @@ void RemoveCylinder::redo() cylinder_renumber(*dives[i], &mapping[0]); divelog.dives.update_cylinder_related_info(*dives[i]); emit diveListNotifier.cylinderRemoved(dives[i], indexes[i]); - invalidate_dive_cache(dives[i]); // Ensure that dive is written in git_save() + dives[i]->invalidate_cache(); // Ensure that dive is written in git_save() } } @@ -1275,7 +1275,7 @@ void EditCylinder::redo() std::swap(*dives[i]->get_cylinder(indexes[i]), cyl[i]); divelog.dives.update_cylinder_related_info(*dives[i]); emit diveListNotifier.cylinderEdited(dives[i], indexes[i]); - invalidate_dive_cache(dives[i]); // Ensure that dive is written in git_save() + dives[i]->invalidate_cache(); // Ensure that dive is written in git_save() } } @@ -1306,7 +1306,7 @@ void EditSensors::mapSensors(int toCyl, int fromCyl) } } emit diveListNotifier.diveComputerEdited(dc); - invalidate_dive_cache(d); // Ensure that dive is written in git_save() + d->invalidate_cache(); // Ensure that dive is written in git_save() } void EditSensors::undo() @@ -1435,7 +1435,7 @@ void EditDive::exchangeDives() if (newDiveSite) newDiveSite->add_dive(oldDive); newDiveSite = oldDiveSite; // remember the previous dive site - invalidate_dive_cache(oldDive); + oldDive->invalidate_cache(); // Changing times may have unsorted the dive and trip tables QVector dives = { oldDive }; diff --git a/commands/command_event.cpp b/commands/command_event.cpp index da727780f..c08ad5b8f 100644 --- a/commands/command_event.cpp +++ b/commands/command_event.cpp @@ -31,7 +31,7 @@ void EventBase::undo() void EventBase::updateDive() { - invalidate_dive_cache(d); + d->invalidate_cache(); emit diveListNotifier.eventsChanged(d); setSelection({ d }, d, dcNr); } diff --git a/commands/command_pictures.cpp b/commands/command_pictures.cpp index 09a6259ea..6044d94f6 100644 --- a/commands/command_pictures.cpp +++ b/commands/command_pictures.cpp @@ -36,7 +36,7 @@ void SetPictureOffset::redo() // If someone complains about speed, do our usual "smart" thing. std::sort(d->pictures.begin(), d->pictures.end()); emit diveListNotifier.pictureOffsetChanged(d, filename, newOffset); - invalidate_dive_cache(d); + d->invalidate_cache(); } // Undo and redo do the same thing @@ -83,7 +83,7 @@ static std::vector removePictures(std::vectorinvalidate_cache(); emit diveListNotifier.picturesRemoved(list.d, std::move(filenames)); } picturesToRemove.clear(); @@ -113,7 +113,7 @@ static std::vector addPictures(std::vectorinvalidate_cache(); emit diveListNotifier.picturesAdded(list.d, std::move(picsForSignal)); } picturesToAdd.clear(); diff --git a/core/dive.cpp b/core/dive.cpp index a49fed566..39ebf24e4 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -208,7 +208,7 @@ void copy_dive(const struct dive *s, struct dive *d) { /* simply copy things over, but then clear fulltext cache and dive cache. */ *d = *s; - invalidate_dive_cache(d); + d->invalidate_cache(); } #define CONDITIONAL_COPY_STRING(_component) \ @@ -2402,15 +2402,15 @@ fraction_t best_he(depth_t depth, const struct dive *dive, bool o2narcotic, frac return fhe; } -void invalidate_dive_cache(struct dive *dive) +static constexpr std::array null_id = {}; +void dive::invalidate_cache() { - memset(dive->git_id, 0, 20); + git_id = null_id; } -bool dive_cache_is_valid(const struct dive *dive) +bool dive::cache_is_valid() const { - static const unsigned char null_id[20] = { 0, }; - return !!memcmp(dive->git_id, null_id, 20); + return git_id != null_id; } int get_surface_pressure_in_mbar(const struct dive *dive, bool non_null) diff --git a/core/dive.h b/core/dive.h index 75adb9256..597b45f39 100644 --- a/core/dive.h +++ b/core/dive.h @@ -66,7 +66,7 @@ struct dive { std::vector dcs; // Attn: pointers to divecomputers are not stable! int id = 0; // unique ID for this dive picture_table pictures; - unsigned char git_id[20] = {}; + std::array git_id = {}; bool notrip = false; /* Don't autogroup this dive to a trip */ bool selected = false; bool hidden_by_filter = false; @@ -79,6 +79,9 @@ struct dive { dive(dive &&); dive &operator=(const dive &); + void invalidate_cache(); + bool cache_is_valid() const; + void fixup_no_cylinder(); /* to fix cylinders, we need the divelist (to calculate cns) */ timestamp_t endtime() const; /* maximum over divecomputers (with samples) */ duration_t totaltime() const; /* maximum over divecomputers (with samples) */ @@ -121,9 +124,6 @@ struct dive_or_trip { struct dive_trip *trip; }; -extern void invalidate_dive_cache(struct dive *dive); -extern bool dive_cache_is_valid(const struct dive *dive); - extern void cylinder_renumber(struct dive &dive, int mapping[]); extern int same_gasmix_cylinder(const cylinder_t &cyl, int cylid, const struct dive *dive, bool check_unused); @@ -205,8 +205,6 @@ extern bool cylinder_with_sensor_sample(const struct dive *dive, int cylinder_id /* UI related protopypes */ -extern void invalidate_dive_cache(struct dive *dc); - extern int total_weight(const struct dive *); extern void update_setpoint_events(const struct dive *dive, struct divecomputer *dc); diff --git a/core/divelist.cpp b/core/divelist.cpp index 6abc4c916..ac99603c9 100644 --- a/core/divelist.cpp +++ b/core/divelist.cpp @@ -730,8 +730,8 @@ struct dive *dive_table::register_dive(std::unique_ptr d) // dives have been added, their status will be updated. d->hidden_by_filter = true; - fulltext_register(d.get()); // Register the dive's fulltext cache - invalidate_dive_cache(d.get()); // Ensure that dive is written in git_save() + fulltext_register(d.get()); // Register the dive's fulltext cache + d->invalidate_cache(); // Ensure that dive is written in git_save() auto [res, idx] = put(std::move(d)); return res; diff --git a/core/load-git.cpp b/core/load-git.cpp index 3be292718..c4b07159c 100644 --- a/core/load-git.cpp +++ b/core/load-git.cpp @@ -1512,7 +1512,7 @@ static int dive_directory(const char *root, const git_tree_entry *entry, const c finish_active_dive(state); create_new_dive(utc_mktime(&tm), state); - memcpy(state->active_dive->git_id, git_tree_entry_id(entry)->id, 20); + memcpy(state->active_dive->git_id.data(), git_tree_entry_id(entry)->id, 20); return GIT_WALK_OK; } diff --git a/core/save-git.cpp b/core/save-git.cpp index 9a6eaf6a3..f5a8847c1 100644 --- a/core/save-git.cpp +++ b/core/save-git.cpp @@ -659,9 +659,9 @@ static int save_one_dive(git_repository *repo, struct dir *tree, struct dive &di * If the dive git ID is valid, we just create the whole directory * with that ID */ - if (cached_ok && dive_cache_is_valid(&dive)) { + if (cached_ok && dive.cache_is_valid()) { git_oid oid; - git_oid_fromraw(&oid, dive.git_id); + git_oid_fromraw(&oid, dive.git_id.data()); ret = tree_insert(tree->files, mb_cstring(&name), 1, &oid, GIT_FILEMODE_TREE); if (ret) diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 38f565e4f..db22ed799 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -1571,8 +1571,8 @@ void MainWindow::hideProgressBar() void MainWindow::divesChanged(const QVector &dives, DiveField) { for (struct dive *d: dives) { - report_info("dive #%d changed, cache is %s", d->number, dive_cache_is_valid(d) ? "valid" : "invalidated"); + report_info("dive #%d changed, cache is %s", d->number, d->cache_is_valid() ? "valid" : "invalidated"); // a brute force way to deal with that would of course be to call - // invalidate_dive_cache(d); + // d->invalidate_cache(); } } diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index b0f83c4be..9e5a2375b 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -2311,9 +2311,9 @@ void QMLManager::rememberOldStatus() void QMLManager::divesChanged(const QVector &dives, DiveField) { for (struct dive *d: dives) { - report_info("dive #%d changed, cache is %s", d->number, dive_cache_is_valid(d) ? "valid" : "invalidated"); + report_info("dive #%d changed, cache is %s", d->number, d->cache_is_valid() ? "valid" : "invalidated"); // a brute force way to deal with that would of course be to call - // invalidate_dive_cache(d); + // d->invalidate_cache(); } }