Dive sites: prepare for dive site ref-counting

Add a dive site table to each dive site to keep track of dives
that have been added to a dive site. Add two functions to add
dives to / remove dives from dive sites.

Since dive sites now contain a dive table, the order of includes
had to be changed: "divesite.h" now includes "dive.h" and not
vice-versa. This caused some include churn.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-03-04 23:20:29 +01:00 committed by Dirk Hohndel
parent f2cdca7bcc
commit c22fd9f4fd
38 changed files with 75 additions and 29 deletions

View file

@ -15,6 +15,7 @@
#include "units.h"
#include "device.h"
#include "file.h"
#include "divesite.h"
#include "ssrf.h"
static unsigned int two_bytes_to_int(unsigned char x, unsigned char y)

View file

@ -10,6 +10,7 @@
#include "libdivecomputer.h"
#include "device.h"
#include "divelist.h"
#include "divesite.h"
#include "qthelper.h"
#include "metadata.h"
#include "membuffer.h"

View file

@ -12,7 +12,6 @@
#include <zip.h>
#include <string.h>
#include <sys/stat.h>
#include "divesite.h"
#include "units.h"
@ -289,6 +288,8 @@ typedef struct trip_table {
} trip_table_t;
struct picture;
struct dive_site;
struct dive_site_table;
struct dive {
int number;
bool notrip; /* Don't autogroup this dive to a trip */
@ -747,12 +748,12 @@ extern void average_max_depth(struct diveplan *dive, int *avg_depth, int *max_de
* Note: we have to use the typedef "dive_table_t" instead of "struct dive_table",
* because MOC removes the "struct", but dive_table is already the name of a global
* variable, leading to compilation errors. Likewise for "struct trip_table" and
* "struct dive_site_table". */
* "struct dive_site_table" (defined in "divesite.h"). */
#include <QObject>
Q_DECLARE_METATYPE(struct dive *);
Q_DECLARE_METATYPE(struct dive_trip *);
Q_DECLARE_METATYPE(dive_table_t *);
Q_DECLARE_METATYPE(trip_table_t *);
Q_DECLARE_METATYPE(dive_site_table_t *);
#endif

View file

@ -12,8 +12,8 @@
#include <zip.h>
#include <libxslt/transform.h>
#include "dive.h"
#include "subsurface-string.h"
#include "divesite.h"
#include "divelist.h"
#include "display.h"
#include "planner.h"
@ -872,7 +872,7 @@ static MAKE_GET_INSERTION_INDEX(trip_table, struct dive_trip *, trips, trip_less
} \
}
static MAKE_ADD_TO(dive_table, struct dive *, dives)
MAKE_ADD_TO(dive_table, struct dive *, dives)
static MAKE_ADD_TO(trip_table, struct dive_trip *, trips)
#define MAKE_REMOVE_FROM(table_type, array_name) \
@ -916,7 +916,7 @@ static MAKE_GET_IDX(trip_table, struct dive_trip *, trips)
MAKE_SORT(dive_table, struct dive *, dives, comp_dives)
MAKE_SORT(trip_table, struct dive_trip *, trips, comp_trips)
static void remove_dive(struct dive_table *table, const struct dive *dive)
void remove_dive(struct dive_table *table, const struct dive *dive)
{
int idx = get_idx_in_dive_table(table, dive);
if (idx >= 0)

View file

@ -2,6 +2,8 @@
#ifndef DIVELIST_H
#define DIVELIST_H
#include "dive.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -9,8 +11,6 @@ extern "C" {
/* this is used for both git and xml format */
#define DATAFORMAT_VERSION 3
struct dive;
extern void update_cylinder_related_info(struct dive *);
extern void mark_divelist_changed(bool);
extern int unsaved_changes(void);
@ -33,10 +33,12 @@ extern char *get_dive_gas_string(const struct dive *dive);
extern struct dive **grow_dive_table(struct dive_table *table);
extern int dive_table_get_insertion_index(struct dive_table *table, struct dive *dive);
extern void add_to_dive_table(struct dive_table *table, int idx, struct dive *dive);
extern void add_single_dive(int idx, struct dive *dive);
extern void get_dive_gas(const struct dive *dive, int *o2_p, int *he_p, int *o2low_p);
extern int get_divenr(const struct dive *dive);
extern struct dive_trip *unregister_dive_from_trip(struct dive *dive);
extern void remove_dive(struct dive_table *table, const struct dive *dive);
extern void remove_dive_from_trip(struct dive *dive, struct trip_table *trip_table_arg);
extern dive_trip_t *alloc_trip(void);
extern dive_trip_t *create_trip_from_dive(struct dive *dive);

View file

@ -382,6 +382,29 @@ void purge_empty_dive_sites(struct dive_site_table *ds_table)
}
}
void add_dive_to_dive_site(struct dive *d, struct dive_site *ds)
{
int idx;
if (d->dive_site == ds)
return;
if (d->dive_site)
fprintf(stderr, "Warning: adding dive that already belongs to a dive site to a different site\n");
idx = dive_table_get_insertion_index(&ds->dives, d);
add_to_dive_table(&ds->dives, idx, d);
d->dive_site = ds;
}
struct dive_site *unregister_dive_from_dive_site(struct dive *d)
{
struct dive_site *ds = d->dive_site;
if (!ds)
return NULL;
remove_dive(&ds->dives, d);
d->dive_site = NULL;
return ds;
}
/* Assign arbitrary UUIDs to dive sites. This is called by before writing the dive log to XML or git. */
static int compare_sites(const void *_a, const void *_b)
{
const struct dive_site *a = (const struct dive_site *)*(void **)_a;

View file

@ -4,6 +4,7 @@
#include "units.h"
#include "taxonomy.h"
#include "dive.h"
#include <stdlib.h>
#ifdef __cplusplus
@ -18,6 +19,7 @@ struct dive_site
{
uint32_t uuid;
char *name;
struct dive_table dives;
location_t location;
char *description;
char *notes;
@ -54,6 +56,8 @@ struct dive_site *alloc_dive_site();
int nr_of_dives_at_dive_site(struct dive_site *ds, bool select_only);
bool is_dive_site_used(struct dive_site *ds, bool select_only);
void free_dive_site(struct dive_site *ds);
void unregister_dive_site(struct dive_site *ds);
void register_dive_site(struct dive_site *ds);
void delete_dive_site(struct dive_site *ds, struct dive_site_table *ds_table);
struct dive_site *create_dive_site(const char *name, struct dive_site_table *ds_table);
struct dive_site *create_dive_site_with_gps(const char *name, const location_t *, struct dive_site_table *ds_table);
@ -71,6 +75,8 @@ struct dive_site *find_or_create_dive_site_with_name(const char *name, struct di
void merge_dive_sites(struct dive_site *ref, struct dive_site *dive_sites[], int count);
void purge_empty_dive_sites(struct dive_site_table *ds_table);
void clear_dive_site_table(struct dive_site_table *ds_table);
void add_dive_to_dive_site(struct dive *d, struct dive_site *ds);
struct dive_site *unregister_dive_from_dive_site(struct dive *d);
#ifdef __cplusplus
}
@ -78,6 +84,7 @@ QString constructLocationTags(struct taxonomy_data *taxonomy, bool for_maintab);
/* Make pointer-to-dive_site a "Qt metatype" so that we can pass it through QVariants */
Q_DECLARE_METATYPE(dive_site *);
Q_DECLARE_METATYPE(dive_site_table_t *);
#endif

View file

@ -6,7 +6,7 @@
#include <QHash>
#include <QLoggingCategory>
#include "dive.h"
#include "divesite.h"
#include "libdivecomputer.h"
#include "connectionlistmodel.h"
#if BT_SUPPORT

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "core/gpslocation.h"
#include "core/divesite.h"
#include "qt-models/gpslistmodel.h"
#include "core/pref.h"
#include "core/qthelper.h"

View file

@ -5,7 +5,7 @@
#endif
#include "ssrf.h"
#include "dive.h"
#include "divesite.h"
#include "subsurface-string.h"
#include "parse.h"
#include "divelist.h"

View file

@ -5,7 +5,7 @@
#endif
#include "ssrf.h"
#include "dive.h"
#include "divesite.h"
#include "subsurface-string.h"
#include "parse.h"
#include "divelist.h"

View file

@ -14,7 +14,7 @@
#include <fcntl.h>
#include <time.h>
#include "gettext.h"
#include "dive.h"
#include "divesite.h"
#include "subsurface-string.h"
#include "device.h"
#include "divelist.h"

View file

@ -2,7 +2,7 @@
#include <string.h>
#include "ssrf.h"
#include "dive.h"
#include "divesite.h"
#include "divelist.h"
#include "file.h"
#include "strndup.h"

View file

@ -15,7 +15,7 @@
#include "gettext.h"
#include "dive.h"
#include "divesite.h"
#include "subsurface-string.h"
#include "divelist.h"
#include "device.h"

View file

@ -22,7 +22,7 @@
#include "gettext.h"
#include "dive.h"
#include "divesite.h"
#include "subsurface-string.h"
#include "parse.h"
#include "divelist.h"

View file

@ -7,7 +7,7 @@
#include <unistd.h>
#include <libdivecomputer/parser.h>
#include "dive.h"
#include "divesite.h"
#include "subsurface-string.h"
#include "parse.h"
#include "divelist.h"

View file

@ -17,7 +17,7 @@
#include <fcntl.h>
#include <git2.h>
#include "dive.h"
#include "divesite.h"
#include "subsurface-string.h"
#include "divelist.h"
#include "device.h"

View file

@ -7,7 +7,8 @@
#include "save-html.h"
#include "qthelper.h"
#include "gettext.h"
#include "stdio.h"
#include "divesite.h"
#include <stdio.h>
void write_attribute(struct membuffer *b, const char *att_name, const char *value, const char *separator)
{

View file

@ -13,7 +13,7 @@
#include <unistd.h>
#include <fcntl.h>
#include "dive.h"
#include "divesite.h"
#include "subsurface-string.h"
#include "divelist.h"
#include "device.h"

View file

@ -5,6 +5,7 @@
#include <QTextDocument>
#include "core/qthelper.h"
#include "core/divesite.h"
#include "core/subsurface-string.h"
#include "qt-models/tankinfomodel.h"

View file

@ -25,6 +25,7 @@
#include "libdivecomputer.h"
#include "uemis.h"
#include "divelist.h"
#include "divesite.h"
#include "core/subsurface-string.h"
#define ERR_FS_ALMOST_FULL QT_TRANSLATE_NOOP("gettextFromC", "Uemis Zurich: the file system is almost full.\nDisconnect/reconnect the dive computer\nand click \'Retry\'")

View file

@ -13,6 +13,7 @@
#include "gettext.h"
#include "uemis.h"
#include "divesite.h"
#include <libdivecomputer/parser.h>
#include <libdivecomputer/version.h>

View file

@ -10,6 +10,7 @@
#include <stdio.h>
#include "membuffer.h"
#include "divesite.h"
#include "save-html.h"
#include "worldmap-save.h"
#include "worldmap-options.h"

View file

@ -4,7 +4,7 @@
#ifndef COMMAND_BASE_H
#define COMMAND_BASE_H
#include "core/dive.h"
#include "core/divesite.h"
#include <QUndoCommand>
#include <QCoreApplication> // For Q_DECLARE_TR_FUNCTIONS

View file

@ -16,7 +16,7 @@
#include "desktop-widgets/mainwindow.h"
#include "profile-widget/profilewidget2.h"
#include "core/save-profiledata.h"
#include "core/dive.h" // Allows access to helper functions in TeX export.
#include "core/divesite.h"
// Retrieves the current unit settings defined in the Subsurface preferences.
#define GET_UNIT(name, field, f, t) \

View file

@ -11,6 +11,7 @@
#include <QUndoStack>
#include <QPainter>
#include "core/qthelper.h"
#include "core/divesite.h"
#include "core/import-csv.h"
static QString subsurface_mimedata = "subsurface/csvcolumns";

View file

@ -14,6 +14,7 @@
#include "qt-models/divetripmodel.h"
#include "qt-models/divelocationmodel.h"
#include "core/qthelper.h"
#include "core/divesite.h"
#include "desktop-widgets/simplewidgets.h"
#include <QCompleter>

View file

@ -13,6 +13,7 @@
#include <QClipboard>
#include "core/file.h"
#include "core/divesite.h"
#include "desktop-widgets/mainwindow.h"
#include "core/qthelper.h"
#include "libdivecomputer/parser.h"

View file

@ -3,7 +3,7 @@
#include <QAbstractTableModel>
#include <vector>
#include "core/dive.h"
#include "core/divesite.h"
class DiveImportedModel : public QAbstractTableModel
{

View file

@ -2,6 +2,7 @@
#include "core/units.h"
#include "qt-models/divelocationmodel.h"
#include "core/qthelper.h"
#include "core/divesite.h"
#include <QDebug>
#include <QLineEdit>
#include <QIcon>

View file

@ -3,6 +3,7 @@
#include "qt-models/models.h"
#include "core/display.h"
#include "core/qthelper.h"
#include "core/divesite.h"
#include "core/subsurface-string.h"
#include "core/subsurface-qt/DiveListNotifier.h"
#include "qt-models/divetripmodel.h"

View file

@ -2,7 +2,7 @@
#include "testgitstorage.h"
#include "git2.h"
#include "core/dive.h"
#include "core/divesite.h"
#include "core/divelist.h"
#include "core/file.h"
#include "core/qthelper.h"

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "testmerge.h"
#include "core/dive.h"
#include "core/divesite.h"
#include "core/divelist.h"
#include "core/file.h"
#include <QTextStream>

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "testparse.h"
#include "core/dive.h"
#include "core/divesite.h"
#include "core/divelist.h"
#include "core/file.h"
#include "core/import-csv.h"

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "testparseperformance.h"
#include "core/dive.h"
#include "core/divesite.h"
#include "core/divelist.h"
#include "core/file.h"
#include "core/git-access.h"

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "testpicture.h"
#include "core/dive.h"
#include "core/divesite.h"
#include "core/divelist.h"
#include "core/file.h"
#include <QString>

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "testprofile.h"
#include "core/dive.h"
#include "core/divesite.h"
#include "core/file.h"
void TestProfile::testRedCeiling()

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "testrenumber.h"
#include "core/dive.h"
#include "core/divesite.h"
#include "core/divelist.h"
#include "core/file.h"
#include <QTextStream>