core: make register_dive() member of dive_table

This one is for symmetry with unregister_dive(). However, it
makes me unhappy, because it modifies global state, namely the
selection machinery and the fulltext. I think these should be
moved up in the call chain.

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

View file

@ -72,7 +72,7 @@ dive *DiveListBase::addDive(DiveToAdd &d)
d.site->add_dive(d.dive.get());
diveSiteCountChanged(d.site);
}
return register_dive(std::move(d.dive)); // Transfer ownership to core and update fulltext index
return divelog.dives.register_dive(std::move(d.dive)); // Transfer ownership to core and update fulltext index
}
// Some signals are sent in batches per trip. To avoid writing the same loop

View file

@ -690,7 +690,9 @@ static void autogroup_dives(struct dive_table &table, struct trip_table &trip_ta
/* This removes a dive from the global dive table but doesn't free the
* resources associated with the dive. The caller must removed the dive
* from the trip-list. Returns a pointer to the unregistered dive.
* The unregistered dive has the selection- and hidden-flags cleared. */
* The unregistered dive has the selection- and hidden-flags cleared.
* TODO: This makes me unhappy, as it touches global state, viz.
* selection and fulltext. */
std::unique_ptr<dive> dive_table::unregister_dive(int idx)
{
if (idx < 0 || static_cast<size_t>(idx) >= size())
@ -710,8 +712,9 @@ std::unique_ptr<dive> dive_table::unregister_dive(int idx)
/* Add a dive to the global dive table.
* Index it in the fulltext cache and make sure that it is written
* in git_save().
*/
struct dive *register_dive(std::unique_ptr<dive> d)
* TODO: This makes me unhappy, as it touches global state, viz.
* selection and fulltext. */
struct dive *dive_table::register_dive(std::unique_ptr<dive> d)
{
// When we add dives, we start in hidden-by-filter status. Once all
// dives have been added, their status will be updated.
@ -719,7 +722,7 @@ struct dive *register_dive(std::unique_ptr<dive> d)
fulltext_register(d.get()); // Register the dive's fulltext cache
invalidate_dive_cache(d.get()); // Ensure that dive is written in git_save()
auto [res, idx] = divelog.dives.put(std::move(d));
auto [res, idx] = put(std::move(d));
return res;
}

View file

@ -19,6 +19,7 @@ int comp_dives_ptr(const struct dive *a, const struct dive *b);
struct dive_table : public sorted_owning_table<dive, &comp_dives> {
dive *get_by_uniq_id(int id) const;
void record_dive(std::unique_ptr<dive> d); // call fixup_dive() before adding dive to table.
struct dive *register_dive(std::unique_ptr<dive> d);
std::unique_ptr<dive> unregister_dive(int idx);
int get_dive_nr_at_idx(int idx) const;
@ -56,7 +57,6 @@ extern void get_dive_gas(const struct dive *dive, int *o2_p, int *he_p, int *o2l
int get_min_datafile_version();
void report_datafile_version(int version);
void clear_dive_file_data();
struct dive *register_dive(std::unique_ptr<dive> d);
extern bool has_dive(unsigned int deviceid, unsigned int diveid);
#endif // DIVELIST_H