Add a unique id to every dive

This id is just held in memory. It's not supposed to be used for anything
but having a unique handle that represents a dive. Whenever you need to
remember a dive across an operation that might change the dive_table, this
is what you should hold on to, not a dive number, a dive pointer, or
anything like that.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-01-07 09:30:01 +08:00
parent ca391035f3
commit 65e9fecd80
3 changed files with 43 additions and 1 deletions

1
dive.c
View file

@ -892,6 +892,7 @@ struct dive *fixup_dive(struct dive *dive)
weightsystem_t *ws = dive->weightsystem + i; weightsystem_t *ws = dive->weightsystem + i;
add_weightsystem_description(ws); add_weightsystem_description(ws);
} }
dive->id = getUniqID(dive);
return dive; return dive;
} }

19
dive.h
View file

@ -412,9 +412,10 @@ struct dive {
pressure_t surface_pressure; pressure_t surface_pressure;
duration_t duration; duration_t duration;
int salinity; // kg per 10000 l int salinity; // kg per 10000 l
struct tag_entry *tag_list;
struct tag_entry *tag_list;
struct divecomputer dc; struct divecomputer dc;
int id; // unique ID for this dive
}; };
static inline int dive_has_gps_location(struct dive *dive) static inline int dive_has_gps_location(struct dive *dive)
@ -611,6 +612,21 @@ static inline struct dive *get_dive_by_diveid(uint32_t diveid, uint32_t deviceid
} }
return NULL; return NULL;
} }
// this is very different from get_dive_by_diveid() (which is only used
// by the UEMIS downloader) -- this uses the unique diveID to allow us
// to hold an identifier for a dive across operations that might change
// the dive_table
static inline struct dive *getDiveById(int id)
{
int i;
struct dive *dive = NULL;
for_each_dive(i, dive) {
if (dive->id == id)
break;
}
return dive;
}
extern struct dive *find_dive_including(timestamp_t when); extern struct dive *find_dive_including(timestamp_t when);
extern bool dive_within_time_range(struct dive *dive, timestamp_t when, timestamp_t offset); extern bool dive_within_time_range(struct dive *dive, timestamp_t when, timestamp_t offset);
struct dive *find_dive_n_near(timestamp_t when, int n, timestamp_t offset); struct dive *find_dive_n_near(timestamp_t when, int n, timestamp_t offset);
@ -654,6 +670,7 @@ extern void finish_sample(struct divecomputer *dc);
extern void sort_table(struct dive_table *table); extern void sort_table(struct dive_table *table);
extern struct dive *fixup_dive(struct dive *dive); extern struct dive *fixup_dive(struct dive *dive);
extern int getUniqID(struct dive *d);
extern unsigned int dc_airtemp(struct divecomputer *dc); extern unsigned int dc_airtemp(struct divecomputer *dc);
extern unsigned int dc_watertemp(struct divecomputer *dc); extern unsigned int dc_watertemp(struct divecomputer *dc);
extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded); extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded);

View file

@ -217,3 +217,27 @@ QList< int > getDivesInTrip ( dive_trip_t* trip )
} }
return ret; return ret;
} }
// we need this to be uniq, but also make sure
// it doesn't change during the life time of a Subsurface session
// oh, and it has no meaning whatsoever - that's why we have the
// silly initial number and increment by 3 :-)
int getUniqID(struct dive *d)
{
static QSet<int> ids;
static int maxId = 83529;
int id = d->id;
if (id) {
if (!ids.contains(id)) {
qDebug() << "WTF - only I am allowed to create IDs";
ids.insert(id);
}
return id;
}
maxId += 3;
id = maxId;
Q_ASSERT(!ids.contains(id));
ids.insert(id);
return id;
}