core: move invalidate_dive_cache() to struct dive

Seems natural in a C++ code base.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-25 14:04:01 +02:00 committed by bstoeger
parent 1b593dc56c
commit 72bb38601d
11 changed files with 42 additions and 44 deletions

View file

@ -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<unsigned char, 20> 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)

View file

@ -66,7 +66,7 @@ struct dive {
std::vector<divecomputer> 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<unsigned char, 20> 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);

View file

@ -730,8 +730,8 @@ struct dive *dive_table::register_dive(std::unique_ptr<dive> 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;

View file

@ -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;
}

View file

@ -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)