core: move dive-site-table functions into class

There were a number of free standing functions acting on a
dive-site-table. Make them member functions. This allows
for shorter names. Use the get_idx() function of the base
class, which returns a size_t instead of an int (since that
is what the standard, somewhat unfortunately, uses).

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-11 14:22:33 +02:00 committed by bstoeger
parent 6b835710bc
commit 76c52c87a3
19 changed files with 87 additions and 99 deletions

View file

@ -383,7 +383,7 @@ ApplyGPSFixes::ApplyGPSFixes(const std::vector<DiveAndLocation> &fixes)
siteLocations.push_back({ ds, dl.location }); siteLocations.push_back({ ds, dl.location });
} }
} else { } else {
ds = create_dive_site(dl.name.toStdString(), *divelog.sites); ds = divelog.sites->create(dl.name.toStdString());
ds->location = dl.location; ds->location = dl.location;
add_dive_to_dive_site(dl.d, ds); add_dive_to_dive_site(dl.d, ds);
dl.d->dive_site = nullptr; // This will be set on redo() dl.d->dive_site = nullptr; // This will be set on redo()

View file

@ -211,9 +211,9 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct
*/ */
{ {
std::string buffer2 = std::string((char *)locality) + " " + (char *)dive_point; std::string buffer2 = std::string((char *)locality) + " " + (char *)dive_point;
struct dive_site *ds = get_dive_site_by_name(buffer2, *log->sites); struct dive_site *ds = log->sites->get_by_name(buffer2);
if (!ds) if (!ds)
ds = create_dive_site(buffer2, *log->sites); ds = log->sites->create(buffer2);
add_dive_to_dive_site(dt_dive, ds); add_dive_to_dive_site(dt_dive, ds);
} }
free(locality); free(locality);

View file

@ -14,49 +14,40 @@
#include <math.h> #include <math.h>
int get_divesite_idx(const struct dive_site *ds, dive_site_table &ds_table)
{
auto it = std::find_if(ds_table.begin(), ds_table.end(), [ds] (const auto &ds2) { return ds2.get() == ds; });
return it != ds_table.end() ? it - ds_table.begin() : -1;
}
template <typename PRED> template <typename PRED>
struct dive_site *get_dive_site_by_predicate(dive_site_table &ds_table, PRED pred) dive_site *get_by_predicate(const dive_site_table &ds_table, PRED pred)
{ {
auto it = std::find_if(ds_table.begin(), ds_table.end(), pred); auto it = std::find_if(ds_table.begin(), ds_table.end(), pred);
return it != ds_table.end() ? it->get() : NULL; return it != ds_table.end() ? it->get() : NULL;
} }
struct dive_site *get_dive_site_by_uuid(uint32_t uuid, dive_site_table &ds_table) dive_site *dive_site_table::get_by_uuid(uint32_t uuid) const
{ {
// The table is sorted by uuid // The table is sorted by uuid
auto it = std::lower_bound(ds_table.begin(), ds_table.end(), uuid, auto it = std::lower_bound(begin(), end(), uuid,
[] (const auto &ds, auto uuid) { return ds->uuid < uuid; }); [] (const auto &ds, auto uuid) { return ds->uuid < uuid; });
return it != ds_table.end() && (*it)->uuid == uuid ? it->get() : NULL; return it != end() && (*it)->uuid == uuid ? it->get() : NULL;
} }
/* there could be multiple sites of the same name - return the first one */ /* there could be multiple sites of the same name - return the first one */
struct dive_site *get_dive_site_by_name(const std::string &name, dive_site_table &ds_table) dive_site *dive_site_table::get_by_name(const std::string &name) const
{ {
return get_dive_site_by_predicate(ds_table, return get_by_predicate(*this, [&name](const auto &ds) { return ds->name == name; });
[&name](const auto &ds) { return ds->name == name; });
} }
/* there could be multiple sites at the same GPS fix - return the first one */ /* there could be multiple sites at the same GPS fix - return the first one */
struct dive_site *get_dive_site_by_gps(const location_t *loc, dive_site_table &ds_table) dive_site *dive_site_table::get_by_gps(const location_t *loc) const
{ {
return get_dive_site_by_predicate(ds_table, return get_by_predicate(*this, [loc](const auto &ds) { return ds->location == *loc; });
[loc](const auto &ds) { return ds->location == *loc; });
} }
/* to avoid a bug where we have two dive sites with different name and the same GPS coordinates /* to avoid a bug where we have two dive sites with different name and the same GPS coordinates
* and first get the gps coordinates (reading a V2 file) and happen to get back "the other" name, * and first get the gps coordinates (reading a V2 file) and happen to get back "the other" name,
* this function allows us to verify if a very specific name/GPS combination already exists */ * this function allows us to verify if a very specific name/GPS combination already exists */
struct dive_site *get_dive_site_by_gps_and_name(const std::string &name, const location_t *loc, dive_site_table &ds_table) dive_site *dive_site_table::get_by_gps_and_name(const std::string &name, const location_t *loc) const
{ {
return get_dive_site_by_predicate(ds_table, return get_by_predicate(*this, [&name, loc](const auto &ds) { return ds->location == *loc &&
[&name, loc](const auto &ds) { return ds->location == *loc && ds->name == name; });
ds->name == name; });
} }
// Calculate the distance in meters between two coordinates. // Calculate the distance in meters between two coordinates.
@ -78,11 +69,11 @@ unsigned int get_distance(const location_t *loc1, const location_t *loc2)
} }
/* find the closest one, no more than distance meters away - if more than one at same distance, pick the first */ /* find the closest one, no more than distance meters away - if more than one at same distance, pick the first */
struct dive_site *get_dive_site_by_gps_proximity(const location_t *loc, int distance, dive_site_table &ds_table) dive_site *dive_site_table::get_by_gps_proximity(const location_t *loc, int distance) const
{ {
struct dive_site *res = nullptr; struct dive_site *res = nullptr;
unsigned int cur_distance, min_distance = distance; unsigned int cur_distance, min_distance = distance;
for (const auto &ds: ds_table) { for (const auto &ds: *this) {
if (dive_site_has_gps_location(ds.get()) && if (dive_site_has_gps_location(ds.get()) &&
(cur_distance = get_distance(&ds->location, loc)) < min_distance) { (cur_distance = get_distance(&ds->location, loc)) < min_distance) {
min_distance = cur_distance; min_distance = cur_distance;
@ -109,7 +100,7 @@ dive_site_table::put_result dive_site_table::register_site(std::unique_ptr<dive_
/* Take care to never have the same uuid twice. This could happen on /* Take care to never have the same uuid twice. This could happen on
* reimport of a log where the dive sites have diverged */ * reimport of a log where the dive sites have diverged */
while (ds->uuid == 0 || get_dive_site_by_uuid(ds->uuid, *this) != NULL) while (ds->uuid == 0 || get_by_uuid(ds->uuid) != NULL)
++ds->uuid; ++ds->uuid;
return put(std::move(ds)); return put(std::move(ds));
@ -136,14 +127,14 @@ dive_site::~dive_site()
} }
/* when parsing, dive sites are identified by uuid */ /* when parsing, dive sites are identified by uuid */
struct dive_site *alloc_or_get_dive_site(uint32_t uuid, dive_site_table &ds_table) dive_site *dive_site_table::alloc_or_get(uint32_t uuid)
{ {
struct dive_site *ds; struct dive_site *ds;
if (uuid && (ds = get_dive_site_by_uuid(uuid, ds_table)) != NULL) if (uuid && (ds = get_by_uuid(uuid)) != NULL)
return ds; return ds;
return ds_table.register_site(std::make_unique<dive_site>(uuid)).ptr; return register_site(std::make_unique<dive_site>(uuid)).ptr;
} }
size_t nr_of_dives_at_dive_site(const dive_site &ds) size_t nr_of_dives_at_dive_site(const dive_site &ds)
@ -158,15 +149,15 @@ bool is_dive_site_selected(const struct dive_site &ds)
} }
/* allocate a new site and add it to the table */ /* allocate a new site and add it to the table */
struct dive_site *create_dive_site(const std::string &name, dive_site_table &ds_table) dive_site *dive_site_table::create(const std::string &name)
{ {
return ds_table.register_site(std::make_unique<dive_site>(name)).ptr; return register_site(std::make_unique<dive_site>(name)).ptr;
} }
/* same as before, but with GPS data */ /* same as before, but with GPS data */
struct dive_site *create_dive_site_with_gps(const std::string &name, const location_t *loc, dive_site_table &ds_table) dive_site *dive_site_table::create(const std::string &name, const location_t *loc)
{ {
return ds_table.register_site(std::make_unique<dive_site>(name, loc)).ptr; return register_site(std::make_unique<dive_site>(name, loc)).ptr;
} }
/* if all fields are empty, the dive site is pointless */ /* if all fields are empty, the dive site is pointless */
@ -201,7 +192,7 @@ static void merge_string(std::string &a, const std::string &b)
* Taxonomy is not compared, as no taxonomy is generated on * Taxonomy is not compared, as no taxonomy is generated on
* import. * import.
*/ */
static bool same_dive_site(const struct dive_site &a, const struct dive_site &b) static bool same(const struct dive_site &a, const struct dive_site &b)
{ {
return a.name == b.name return a.name == b.name
&& a.location == b.location && a.location == b.location
@ -211,8 +202,8 @@ static bool same_dive_site(const struct dive_site &a, const struct dive_site &b)
struct dive_site *get_same_dive_site(const struct dive_site &site) struct dive_site *get_same_dive_site(const struct dive_site &site)
{ {
return get_dive_site_by_predicate(*divelog.sites, return get_by_predicate(*divelog.sites,
[site](const auto &ds) { return same_dive_site(*ds, site); }); [site](const auto &ds) { return same(*ds, site); });
} }
void merge_dive_site(struct dive_site *a, struct dive_site *b) void merge_dive_site(struct dive_site *a, struct dive_site *b)
@ -226,17 +217,17 @@ void merge_dive_site(struct dive_site *a, struct dive_site *b)
a->taxonomy = std::move(b->taxonomy); a->taxonomy = std::move(b->taxonomy);
} }
struct dive_site *find_or_create_dive_site_with_name(const std::string &name, dive_site_table &ds_table) dive_site *dive_site_table::find_or_create(const std::string &name)
{ {
struct dive_site *ds = get_dive_site_by_name(name, ds_table); struct dive_site *ds = get_by_name(name);
if (ds) if (ds)
return ds; return ds;
return create_dive_site(name, ds_table); return create(name);
} }
void purge_empty_dive_sites(dive_site_table &ds_table) void dive_site_table::purge_empty()
{ {
for (const auto &ds: ds_table) { for (const auto &ds: *this) {
if (!dive_site_is_empty(ds.get())) if (!dive_site_is_empty(ds.get()))
continue; continue;
while (!ds->dives.empty()) { while (!ds->dives.empty()) {

View file

@ -35,26 +35,25 @@ inline int divesite_comp_uuid(const dive_site &ds1, const dive_site &ds2)
class dive_site_table : public sorted_owning_table<dive_site, &divesite_comp_uuid> { class dive_site_table : public sorted_owning_table<dive_site, &divesite_comp_uuid> {
public: public:
put_result register_site(std::unique_ptr<dive_site> site); put_result register_site(std::unique_ptr<dive_site> site); // Creates or changes UUID if duplicate
dive_site *get_by_uuid(uint32_t uuid) const;
dive_site *alloc_or_get(uint32_t uuid);
dive_site *create(const std::string &name);
dive_site *create(const std::string &name, const location_t *);
dive_site *find_or_create(const std::string &name);
dive_site *get_by_name(const std::string &name) const;
dive_site *get_by_gps(const location_t *) const;
dive_site *get_by_gps_and_name(const std::string &name, const location_t *) const;
dive_site *get_by_gps_proximity(const location_t *, int distance) const;
void purge_empty();
}; };
int get_divesite_idx(const struct dive_site *ds, dive_site_table &ds_table);
struct dive_site *get_dive_site_by_uuid(uint32_t uuid, dive_site_table &ds_table);
struct dive_site *alloc_or_get_dive_site(uint32_t uuid, dive_site_table &ds_table);
size_t nr_of_dives_at_dive_site(const struct dive_site &ds); size_t nr_of_dives_at_dive_site(const struct dive_site &ds);
bool is_dive_site_selected(const struct dive_site &ds); bool is_dive_site_selected(const struct dive_site &ds);
struct dive_site *create_dive_site(const std::string &name, dive_site_table &ds_table);
struct dive_site *create_dive_site_with_gps(const std::string &name, const location_t *, dive_site_table &ds_table);
struct dive_site *get_dive_site_by_name(const std::string &name, dive_site_table &ds_table);
struct dive_site *get_dive_site_by_gps(const location_t *, dive_site_table &ds_table);
struct dive_site *get_dive_site_by_gps_and_name(const std::string &name, const location_t *, dive_site_table &ds_table);
struct dive_site *get_dive_site_by_gps_proximity(const location_t *, int distance, dive_site_table &ds_table);
struct dive_site *get_same_dive_site(const struct dive_site &); struct dive_site *get_same_dive_site(const struct dive_site &);
bool dive_site_is_empty(struct dive_site *ds); bool dive_site_is_empty(struct dive_site *ds);
void merge_dive_site(struct dive_site *a, struct dive_site *b); void merge_dive_site(struct dive_site *a, struct dive_site *b);
unsigned int get_distance(const location_t *loc1, const location_t *loc2); unsigned int get_distance(const location_t *loc1, const location_t *loc2);
struct dive_site *find_or_create_dive_site_with_name(const std::string &name, dive_site_table &ds_table);
void purge_empty_dive_sites(dive_site_table &ds_table);
void add_dive_to_dive_site(struct dive *d, struct dive_site *ds); void add_dive_to_dive_site(struct dive *d, struct dive_site *ds);
struct dive_site *unregister_dive_from_dive_site(struct dive *d); struct dive_site *unregister_dive_from_dive_site(struct dive *d);
std::string constructLocationTags(const taxonomy_data &taxonomy, bool for_maintab); std::string constructLocationTags(const taxonomy_data &taxonomy, bool for_maintab);

View file

@ -181,7 +181,7 @@ static int cobalt_dive(void *param, int, char **data, char **)
if (location && location_site) { if (location && location_site) {
std::string tmp = std::string(location) + " / " + location_site; std::string tmp = std::string(location) + " / " + location_site;
add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(tmp, *state->log->sites)); add_dive_to_dive_site(state->cur_dive, state->log->sites->find_or_create(tmp));
} }
free(location); free(location);
free(location_site); free(location_site);

View file

@ -276,7 +276,7 @@ static int divinglog_dive(void *param, int, char **data, char **)
state->cur_dive->when = (time_t)(atol(data[1])); state->cur_dive->when = (time_t)(atol(data[1]));
if (data[2]) if (data[2])
add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(std::string(data[2]), *state->log->sites)); add_dive_to_dive_site(state->cur_dive, state->log->sites->find_or_create(std::string(data[2])));
if (data[3]) if (data[3])
utf8_string(data[3], &state->cur_dive->buddy); utf8_string(data[3], &state->cur_dive->buddy);

View file

@ -636,7 +636,7 @@ static void parse_string_field(device_data_t *devdata, struct dive *dive, dc_fie
if (location.lat.udeg && location.lon.udeg) { if (location.lat.udeg && location.lon.udeg) {
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);
add_dive_to_dive_site(dive, create_dive_site_with_gps(std::string(str->value), &location, *devdata->log->sites)); add_dive_to_dive_site(dive, devdata->log->sites->create(std::string(str->value), &location));
} }
} }
} }

View file

@ -190,7 +190,7 @@ static void parse_dives(int log_version, const unsigned char *buf, unsigned int
/* Store the location only if we have one */ /* Store the location only if we have one */
if (!location.empty()) if (!location.empty())
add_dive_to_dive_site(dive, find_or_create_dive_site_with_name(location, sites)); add_dive_to_dive_site(dive, sites.find_or_create(location));
ptr += len + 4 + place_len; ptr += len + 4 + place_len;

View file

@ -175,9 +175,9 @@ static void parse_dive_gps(char *line, struct git_parser_state *state)
parse_location(line, &location); parse_location(line, &location);
if (!ds) { if (!ds) {
ds = get_dive_site_by_gps(&location, *state->log->sites); ds = state->log->sites->get_by_gps(&location);
if (!ds) if (!ds)
ds = create_dive_site_with_gps(std::string(), &location, *state->log->sites); ds = state->log->sites->create(std::string(), &location);
add_dive_to_dive_site(state->active_dive, ds); add_dive_to_dive_site(state->active_dive, ds);
} else { } else {
if (dive_site_has_gps_location(ds) && ds->location != location) { if (dive_site_has_gps_location(ds) && ds->location != location) {
@ -221,9 +221,9 @@ static void parse_dive_location(char *, struct git_parser_state *state)
std::string name = get_first_converted_string(state); std::string name = get_first_converted_string(state);
struct dive_site *ds = get_dive_site_for_dive(state->active_dive); struct dive_site *ds = get_dive_site_for_dive(state->active_dive);
if (!ds) { if (!ds) {
ds = get_dive_site_by_name(name, *state->log->sites); ds = state->log->sites->get_by_name(name);
if (!ds) if (!ds)
ds = create_dive_site(name, *state->log->sites); ds = state->log->sites->create(name);
add_dive_to_dive_site(state->active_dive, ds); add_dive_to_dive_site(state->active_dive, ds);
} else { } else {
// we already had a dive site linked to the dive // we already had a dive site linked to the dive
@ -252,7 +252,7 @@ static void parse_dive_notes(char *, struct git_parser_state *state)
{ state->active_dive->notes = get_first_converted_string_c(state); } { state->active_dive->notes = get_first_converted_string_c(state); }
static void parse_dive_divesiteid(char *line, struct git_parser_state *state) static void parse_dive_divesiteid(char *line, struct git_parser_state *state)
{ add_dive_to_dive_site(state->active_dive, get_dive_site_by_uuid(get_hex(line), *state->log->sites)); } { add_dive_to_dive_site(state->active_dive, state->log->sites->get_by_uuid(get_hex(line))); }
/* /*
* We can have multiple tags. * We can have multiple tags.
@ -1711,7 +1711,7 @@ static int parse_site_entry(struct git_parser_state *state, const git_tree_entry
if (*suffix == '\0') if (*suffix == '\0')
return report_error("Dive site without uuid"); return report_error("Dive site without uuid");
uint32_t uuid = strtoul(suffix, NULL, 16); uint32_t uuid = strtoul(suffix, NULL, 16);
state->active_site = alloc_or_get_dive_site(uuid, *state->log->sites); state->active_site = state->log->sites->alloc_or_get(uuid);
git_blob *blob = git_tree_entry_blob(state->repo, entry); git_blob *blob = git_tree_entry_blob(state->repo, entry);
if (!blob) if (!blob)
return report_error("Unable to read dive site file"); return report_error("Unable to read dive site file");

View file

@ -559,7 +559,7 @@ static void dive_site(const char *buffer, struct dive *d, struct parser_state *s
{ {
uint32_t uuid; uint32_t uuid;
hex_value(buffer, &uuid); hex_value(buffer, &uuid);
add_dive_to_dive_site(d, get_dive_site_by_uuid(uuid, *state->log->sites)); add_dive_to_dive_site(d, state->log->sites->get_by_uuid(uuid));
} }
static void get_notrip(const char *buffer, bool *notrip) static void get_notrip(const char *buffer, bool *notrip)
@ -977,9 +977,9 @@ static void divinglog_place(const char *place, struct dive *d, struct parser_sta
!state->city.empty() ? state->city.c_str() : "", !state->city.empty() ? state->city.c_str() : "",
!state->country.empty() ? ", " : "", !state->country.empty() ? ", " : "",
!state->country.empty() ? state->country.c_str() : ""); !state->country.empty() ? state->country.c_str() : "");
ds = get_dive_site_by_name(buffer, *state->log->sites); ds = state->log->sites->get_by_name(buffer);
if (!ds) if (!ds)
ds = create_dive_site(buffer, *state->log->sites); ds = state->log->sites->create(buffer);
add_dive_to_dive_site(d, ds); add_dive_to_dive_site(d, ds);
// TODO: capture the country / city info in the taxonomy instead // TODO: capture the country / city info in the taxonomy instead
@ -1149,7 +1149,7 @@ static void gps_lat(const char *buffer, struct dive *dive, struct parser_state *
location.lat = parse_degrees(buffer, &end); location.lat = parse_degrees(buffer, &end);
if (!ds) { if (!ds) {
add_dive_to_dive_site(dive, create_dive_site_with_gps(std::string(), &location, *state->log->sites)); add_dive_to_dive_site(dive, state->log->sites->create(std::string(), &location));
} else { } else {
if (ds->location.lat.udeg && ds->location.lat.udeg != location.lat.udeg) if (ds->location.lat.udeg && ds->location.lat.udeg != location.lat.udeg)
report_info("Oops, changing the latitude of existing dive site id %8x name %s; not good", ds->uuid, report_info("Oops, changing the latitude of existing dive site id %8x name %s; not good", ds->uuid,
@ -1166,7 +1166,7 @@ static void gps_long(const char *buffer, struct dive *dive, struct parser_state
location.lon = parse_degrees(buffer, &end); location.lon = parse_degrees(buffer, &end);
if (!ds) { if (!ds) {
add_dive_to_dive_site(dive, create_dive_site_with_gps(std::string(), &location, *state->log->sites)); add_dive_to_dive_site(dive, state->log->sites->create(std::string(), &location));
} else { } else {
if (ds->location.lon.udeg && ds->location.lon.udeg != location.lon.udeg) if (ds->location.lon.udeg && ds->location.lon.udeg != location.lon.udeg)
report_info("Oops, changing the longitude of existing dive site id %8x name %s; not good", ds->uuid, report_info("Oops, changing the longitude of existing dive site id %8x name %s; not good", ds->uuid,
@ -1197,14 +1197,14 @@ static void gps_in_dive(const char *buffer, struct dive *dive, struct parser_sta
parse_location(buffer, &location); parse_location(buffer, &location);
if (!ds) { if (!ds) {
// check if we have a dive site within 20 meters of that gps fix // check if we have a dive site within 20 meters of that gps fix
ds = get_dive_site_by_gps_proximity(&location, 20, *state->log->sites); ds = state->log->sites->get_by_gps_proximity(&location, 20);
if (ds) { if (ds) {
// found a site nearby; in case it turns out this one had a different name let's // found a site nearby; in case it turns out this one had a different name let's
// remember the original coordinates so we can create the correct dive site later // remember the original coordinates so we can create the correct dive site later
state->cur_location = location; state->cur_location = location;
} else { } else {
ds = create_dive_site_with_gps(std::string(), &location, *state->log->sites); ds = state->log->sites->create(std::string(), &location);
} }
add_dive_to_dive_site(dive, ds); add_dive_to_dive_site(dive, ds);
} else { } else {
@ -2225,7 +2225,7 @@ int parse_dlf_buffer(unsigned char *buffer, size_t size, struct divelog *log)
/* Measure GPS */ /* Measure GPS */
state.cur_location.lat.udeg = (int)((ptr[7] << 24) + (ptr[6] << 16) + (ptr[5] << 8) + (ptr[4] << 0)); state.cur_location.lat.udeg = (int)((ptr[7] << 24) + (ptr[6] << 16) + (ptr[5] << 8) + (ptr[4] << 0));
state.cur_location.lon.udeg = (int)((ptr[11] << 24) + (ptr[10] << 16) + (ptr[9] << 8) + (ptr[8] << 0)); state.cur_location.lon.udeg = (int)((ptr[11] << 24) + (ptr[10] << 16) + (ptr[9] << 8) + (ptr[8] << 0));
add_dive_to_dive_site(state.cur_dive, create_dive_site_with_gps("DLF imported"s, &state.cur_location, *state.log->sites)); add_dive_to_dive_site(state.cur_dive, state.log->sites->create("DLF imported"s, &state.cur_location));
break; break;
default: default:
break; break;

View file

@ -199,7 +199,7 @@ void dive_site_end(struct parser_state *state)
if (!state->cur_dive_site) if (!state->cur_dive_site)
return; return;
struct dive_site *ds = alloc_or_get_dive_site(state->cur_dive_site->uuid, *state->log->sites); struct dive_site *ds = state->log->sites->alloc_or_get(state->cur_dive_site->uuid);
merge_dive_site(ds, state->cur_dive_site.get()); merge_dive_site(ds, state->cur_dive_site.get());
if (verbose > 3) if (verbose > 3)
@ -470,7 +470,7 @@ void add_dive_site(const char *ds_name, struct dive *dive, struct parser_state *
struct dive_site *ds = dive->dive_site; struct dive_site *ds = dive->dive_site;
if (!ds) { if (!ds) {
// if the dive doesn't have a dive site, check if there's already a dive site by this name // if the dive doesn't have a dive site, check if there's already a dive site by this name
ds = get_dive_site_by_name(trimmed, *state->log->sites); ds = state->log->sites->get_by_name(trimmed);
} }
if (ds) { if (ds) {
// we have a dive site, let's hope there isn't a different name // we have a dive site, let's hope there isn't a different name
@ -481,12 +481,12 @@ void add_dive_site(const char *ds_name, struct dive *dive, struct parser_state *
// but wait, we could have gotten this one based on GPS coords and could // but wait, we could have gotten this one based on GPS coords and could
// have had two different names for the same site... so let's search the other // have had two different names for the same site... so let's search the other
// way around // way around
struct dive_site *exact_match = get_dive_site_by_gps_and_name(trimmed, &ds->location, *state->log->sites); struct dive_site *exact_match = state->log->sites->get_by_gps_and_name(trimmed, &ds->location);
if (exact_match) { if (exact_match) {
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);
add_dive_to_dive_site(dive, exact_match); add_dive_to_dive_site(dive, exact_match);
} else { } else {
struct dive_site *newds = create_dive_site(trimmed.c_str(), *state->log->sites); struct dive_site *newds = state->log->sites->create(trimmed.c_str());
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);
add_dive_to_dive_site(dive, newds); add_dive_to_dive_site(dive, newds);
if (has_location(&state->cur_location)) { if (has_location(&state->cur_location)) {
@ -504,7 +504,7 @@ void add_dive_site(const char *ds_name, struct dive *dive, struct parser_state *
add_dive_to_dive_site(dive, ds); add_dive_to_dive_site(dive, ds);
} }
} else { } else {
add_dive_to_dive_site(dive, create_dive_site(trimmed, *state->log->sites)); add_dive_to_dive_site(dive, state->log->sites->create(trimmed));
} }
} }
} }

View file

@ -922,7 +922,7 @@ static void save_divesites(git_repository *repo, struct dir *tree)
put_format(&dirname, "01-Divesites"); put_format(&dirname, "01-Divesites");
subdir = new_directory(repo, tree, &dirname); subdir = new_directory(repo, tree, &dirname);
purge_empty_dive_sites(*divelog.sites); divelog.sites->purge_empty();
for (const auto &ds: *divelog.sites) { for (const auto &ds: *divelog.sites) {
membuffer b; membuffer b;
membuffer site_file_name; membuffer site_file_name;

View file

@ -915,7 +915,7 @@ static bool process_raw_buffer(device_data_t *devdata, uint32_t deviceid, std::s
} else if (!is_log && dive && tag == "divespot_id") { } else if (!is_log && dive && tag == "divespot_id") {
int divespot_id; int divespot_id;
if (from_chars(val, divespot_id).ec != std::errc::invalid_argument) { if (from_chars(val, divespot_id).ec != std::errc::invalid_argument) {
struct dive_site *ds = create_dive_site("from Uemis"s, *devdata->log->sites); struct dive_site *ds = devdata->log->sites->create("from Uemis"s);
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);
add_dive_to_dive_site(dive, ds); add_dive_to_dive_site(dive, ds);
uemis_obj.mark_divelocation(dive->dc.diveid, divespot_id, ds); uemis_obj.mark_divelocation(dive->dc.diveid, divespot_id, ds);
@ -1109,7 +1109,7 @@ static void get_uemis_divespot(device_data_t *devdata, const std::string &mountp
* we search all existing divesites if we have one with the same name already. The function * we search all existing divesites if we have one with the same name already. The function
* returns the first found which is luckily not the newly created. * returns the first found which is luckily not the newly created.
*/ */
struct dive_site *ods = get_dive_site_by_name(nds->name, *devdata->log->sites); struct dive_site *ods = devdata->log->sites->get_by_name(nds->name);
if (ods && nds->uuid != ods->uuid) { if (ods && nds->uuid != ods->uuid) {
/* if the uuid's are the same, the new site is a duplicate and can be deleted */ /* if the uuid's are the same, the new site is a duplicate and can be deleted */
unregister_dive_from_dive_site(dive); unregister_dive_from_dive_site(dive);

View file

@ -97,10 +97,10 @@ void DiveSiteListView::diveSiteAdded(struct dive_site *, int idx)
void DiveSiteListView::diveSiteChanged(struct dive_site *ds, int field) void DiveSiteListView::diveSiteChanged(struct dive_site *ds, int field)
{ {
int idx = get_divesite_idx(ds, *divelog.sites); size_t idx = divelog.sites->get_idx(ds);
if (idx < 0) if (idx == std::string::npos)
return; return;
QModelIndex globalIdx = LocationInformationModel::instance()->index(idx, field); QModelIndex globalIdx = LocationInformationModel::instance()->index(static_cast<int>(idx), field);
QModelIndex localIdx = model->mapFromSource(globalIdx); QModelIndex localIdx = model->mapFromSource(globalIdx);
ui.diveSites->view()->scrollTo(localIdx); ui.diveSites->view()->scrollTo(localIdx);
} }

View file

@ -1069,7 +1069,7 @@ bool QMLManager::checkLocation(DiveSiteChange &res, struct dive *d, QString loca
bool changed = false; bool changed = false;
QString oldLocation = QString::fromStdString(get_dive_location(d)); QString oldLocation = QString::fromStdString(get_dive_location(d));
if (oldLocation != location) { if (oldLocation != location) {
ds = get_dive_site_by_name(location.toStdString(), *divelog.sites); ds = divelog.sites->get_by_name(location.toStdString());
if (!ds && !location.isEmpty()) { if (!ds && !location.isEmpty()) {
res.createdDs = std::make_unique<dive_site>(qPrintable(location)); res.createdDs = std::make_unique<dive_site>(qPrintable(location));
res.changed = true; res.changed = true;
@ -1816,7 +1816,7 @@ QString QMLManager::getVersion() const
QString QMLManager::getGpsFromSiteName(const QString &siteName) QString QMLManager::getGpsFromSiteName(const QString &siteName)
{ {
struct dive_site *ds = get_dive_site_by_name(siteName.toStdString(), *divelog.sites); struct dive_site *ds = divelog.sites->get_by_name(siteName.toStdString());
if (!ds) if (!ds)
return QString(); return QString();
return printGPSCoords(&ds->location); return printGPSCoords(&ds->location);

View file

@ -134,8 +134,8 @@ void LocationInformationModel::update()
void LocationInformationModel::diveSiteDiveCountChanged(dive_site *ds) void LocationInformationModel::diveSiteDiveCountChanged(dive_site *ds)
{ {
int idx = get_divesite_idx(ds, *divelog.sites); size_t idx = divelog.sites->get_idx(ds);
if (idx >= 0) if (idx != std::string::npos)
dataChanged(createIndex(idx, NUM_DIVES), createIndex(idx, NUM_DIVES)); dataChanged(createIndex(idx, NUM_DIVES), createIndex(idx, NUM_DIVES));
} }
@ -159,16 +159,16 @@ void LocationInformationModel::diveSiteDeleted(struct dive_site *, int idx)
void LocationInformationModel::diveSiteChanged(struct dive_site *ds, int field) void LocationInformationModel::diveSiteChanged(struct dive_site *ds, int field)
{ {
int idx = get_divesite_idx(ds, *divelog.sites); size_t idx = divelog.sites->get_idx(ds);
if (idx < 0) if (idx == std::string::npos)
return; return;
dataChanged(createIndex(idx, field), createIndex(idx, field)); dataChanged(createIndex(idx, field), createIndex(idx, field));
} }
void LocationInformationModel::diveSiteDivesChanged(struct dive_site *ds) void LocationInformationModel::diveSiteDivesChanged(struct dive_site *ds)
{ {
int idx = get_divesite_idx(ds, *divelog.sites); size_t idx = divelog.sites->get_idx(ds);
if (idx < 0) if (idx == std::string::npos)
return; return;
dataChanged(createIndex(idx, NUM_DIVES), createIndex(idx, NUM_DIVES)); dataChanged(createIndex(idx, NUM_DIVES), createIndex(idx, NUM_DIVES));
} }

View file

@ -67,8 +67,7 @@ QVariant DivesiteImportedModel::data(const QModelIndex &index, int role) const
case NEAREST: { case NEAREST: {
// 40075000 is circumference of the earth in meters // 40075000 is circumference of the earth in meters
struct dive_site *nearest_ds = struct dive_site *nearest_ds =
get_dive_site_by_gps_proximity(&ds->location, divelog.sites->get_by_gps_proximity(&ds->location, 40075000);
40075000, *divelog.sites);
if (nearest_ds) if (nearest_ds)
return QString::fromStdString(nearest_ds->name); return QString::fromStdString(nearest_ds->name);
else else
@ -77,8 +76,7 @@ QVariant DivesiteImportedModel::data(const QModelIndex &index, int role) const
case DISTANCE: { case DISTANCE: {
unsigned int distance = 0; unsigned int distance = 0;
struct dive_site *nearest_ds = struct dive_site *nearest_ds =
get_dive_site_by_gps_proximity(&ds->location, divelog.sites->get_by_gps_proximity(&ds->location, 40075000);
40075000, *divelog.sites);
if (nearest_ds) if (nearest_ds)
distance = get_distance(&ds->location, distance = get_distance(&ds->location,
&nearest_ds->location); &nearest_ds->location);
@ -135,6 +133,6 @@ void DivesiteImportedModel::repopulate(dive_site_table *sites)
lastIndex = (int)importedSitesTable->size() - 1; // Qt: the "last index" is negative for empty lists. Insane. lastIndex = (int)importedSitesTable->size() - 1; // Qt: the "last index" is negative for empty lists. Insane.
checkStates.resize(importedSitesTable->size()); checkStates.resize(importedSitesTable->size());
for (size_t row = 0; row < importedSitesTable->size(); row++) for (size_t row = 0; row < importedSitesTable->size(); row++)
checkStates[row] = !get_dive_site_by_gps(&(*importedSitesTable)[row]->location, *divelog.sites); checkStates[row] = !divelog.sites->get_by_gps(&(*importedSitesTable)[row]->location);
endResetModel(); endResetModel();
} }

View file

@ -423,12 +423,12 @@ static void smtk_build_location(MdbHandle *mdb, char *idx, struct dive_site **lo
concat(str, ", ", table.get_string_view(1)); // Locality concat(str, ", ", table.get_string_view(1)); // Locality
concat(str, ", ", site); concat(str, ", ", site);
ds = get_dive_site_by_name(str, *log->sites); ds = log->sites->get_by_name(str);
if (!ds) { if (!ds) {
if (!has_location(&loc)) if (!has_location(&loc))
ds = create_dive_site(str, *log->sites); ds = log->sites->create(str);
else else
ds = create_dive_site_with_gps(str, &loc, *log->sites); ds = log->sites->create(str, &loc);
} }
*location = ds; *location = ds;

View file

@ -92,7 +92,7 @@ int TestParse::parseCSV(int units, std::string file)
int TestParse::parseDivingLog() int TestParse::parseDivingLog()
{ {
// Parsing of DivingLog import from SQLite database // Parsing of DivingLog import from SQLite database
struct dive_site *ds = alloc_or_get_dive_site(0xdeadbeef, *divelog.sites); struct dive_site *ds = divelog.sites->alloc_or_get(0xdeadbeef);
ds->name = "Suomi - - Hälvälä"; ds->name = "Suomi - - Hälvälä";
int ret = sqlite3_open(SUBSURFACE_TEST_DATA "/dives/TestDivingLog4.1.1.sql", &_sqlite3_handle); int ret = sqlite3_open(SUBSURFACE_TEST_DATA "/dives/TestDivingLog4.1.1.sql", &_sqlite3_handle);