From 3df32044ee46caf518b23cc6359f98501f7ca961 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 4 Jun 2019 20:59:08 +0200 Subject: [PATCH] Cleanup: generate clear_*_table() functions by macro In analogy to the other table functions, generate these by a macro as well. Signed-off-by: Berthold Stoeger --- core/divelist.c | 22 +++----------------- core/divelist.h | 2 +- core/divesite.c | 1 + core/downloadfromdcthread.cpp | 2 +- core/table.h | 8 +++++++ core/trip.c | 8 +------ desktop-widgets/downloadfromdivecomputer.cpp | 4 ++-- desktop-widgets/mainwindow.cpp | 2 +- 8 files changed, 18 insertions(+), 31 deletions(-) diff --git a/core/divelist.c b/core/divelist.c index 71391d505..af253fb4e 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -782,6 +782,7 @@ static MAKE_REMOVE_FROM(dive_table, dives) static MAKE_GET_IDX(dive_table, struct dive *, dives) MAKE_SORT(dive_table, struct dive *, dives, comp_dives) MAKE_REMOVE(dive_table, struct dive *, dive) +MAKE_CLEAR_TABLE(dive_table, dives, dive) void insert_dive(struct dive_table *table, struct dive *d) { @@ -1008,23 +1009,6 @@ static void merge_imported_dives(struct dive_table *table) } } -/* - * Clear a dive_table and dive_site_table. Think about generating these with macros. - */ -void clear_table(struct dive_table *table) -{ - for (int i = 0; i < table->nr; i++) - free_dive(table->dives[i]); - table->nr = 0; -} - -void clear_dive_site_table(struct dive_site_table *ds_table) -{ - for (int i = 0; i < ds_table->nr; i++) - free_dive_site(ds_table->dive_sites[i]); - ds_table->nr = 0; -} - /* * Try to merge a new dive into the dive at position idx. Return * true on success. On success, the old dive will be added to the @@ -1292,8 +1276,8 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table * import_trip_table = &local_trip_table; /* Make sure that output parameters don't contain garbage */ - clear_table(dives_to_add); - clear_table(dives_to_remove); + clear_dive_table(dives_to_add); + clear_dive_table(dives_to_remove); clear_trip_table(trips_to_add); clear_dive_site_table(sites_to_add); diff --git a/core/divelist.h b/core/divelist.h index 7b57b41ac..f363cc336 100644 --- a/core/divelist.h +++ b/core/divelist.h @@ -58,7 +58,7 @@ void reset_min_datafile_version(); void report_datafile_version(int version); int get_dive_id_closest_to(timestamp_t when); void clear_dive_file_data(); -void clear_table(struct dive_table *table); +void clear_dive_table(struct dive_table *table); #ifdef DEBUG_TRIP extern void dump_selection(void); diff --git a/core/divesite.c b/core/divesite.c index 45601537c..db5d87cdf 100644 --- a/core/divesite.c +++ b/core/divesite.c @@ -130,6 +130,7 @@ static MAKE_REMOVE_FROM(dive_site_table, dive_sites) static MAKE_GET_IDX(dive_site_table, struct dive_site *, dive_sites) MAKE_SORT(dive_site_table, struct dive_site *, dive_sites, compare_sites) static MAKE_REMOVE(dive_site_table, struct dive_site *, dive_site) +MAKE_CLEAR_TABLE(dive_site_table, dive_sites, dive_site) int add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_table) { diff --git a/core/downloadfromdcthread.cpp b/core/downloadfromdcthread.cpp index 955431880..1112d6027 100644 --- a/core/downloadfromdcthread.cpp +++ b/core/downloadfromdcthread.cpp @@ -82,7 +82,7 @@ void DownloadThread::run() #endif qDebug() << "Starting download from " << (internalData->bluetooth_mode ? "BT" : internalData->devname); qDebug() << "downloading" << (internalData->force_download ? "all" : "only new") << "dives"; - clear_table(&downloadTable); + clear_dive_table(&downloadTable); clear_dive_site_table(&diveSiteTable); Q_ASSERT(internalData->download_table != nullptr); diff --git a/core/table.h b/core/table.h index 1abfa6be3..da0bc96a5 100644 --- a/core/table.h +++ b/core/table.h @@ -91,4 +91,12 @@ return idx; \ } +#define MAKE_CLEAR_TABLE(table_type, array_name, item_name) \ + void clear_##table_type(struct table_type *table) \ + { \ + for (int i = 0; i < table->nr; i++) \ + free_##item_name(table->array_name[i]); \ + table->nr = 0; \ + } + #endif diff --git a/core/trip.c b/core/trip.c index a864e8dfb..446f153eb 100644 --- a/core/trip.c +++ b/core/trip.c @@ -49,6 +49,7 @@ static MAKE_ADD_TO(trip_table, struct dive_trip *, trips) static MAKE_REMOVE_FROM(trip_table, trips) MAKE_SORT(trip_table, struct dive_trip *, trips, comp_trips) MAKE_REMOVE(trip_table, struct dive_trip *, trip) +MAKE_CLEAR_TABLE(trip_table, trips, trip) timestamp_t trip_date(const struct dive_trip *trip) { @@ -294,13 +295,6 @@ dive_trip_t *combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b) return trip; } -void clear_trip_table(struct trip_table *table) -{ - for (int i = 0; i < table->nr; i++) - free_trip(table->trips[i]); - table->nr = 0; -} - /* Trips are compared according to the first dive in the trip. */ int comp_trips(const struct dive_trip *a, const struct dive_trip *b) { diff --git a/desktop-widgets/downloadfromdivecomputer.cpp b/desktop-widgets/downloadfromdivecomputer.cpp index 6d9ba9288..f948097b2 100644 --- a/desktop-widgets/downloadfromdivecomputer.cpp +++ b/desktop-widgets/downloadfromdivecomputer.cpp @@ -342,7 +342,7 @@ void DownloadFromDCWidget::on_downloadCancelRetryButton_clicked() // this means we are retrying - so we better clean out the partial // list of downloaded dives from the last attempt diveImportedModel->clearTable(); - clear_table(thread.table()); + clear_dive_table(thread.table()); } updateState(DOWNLOADING); @@ -493,7 +493,7 @@ void DownloadFromDCWidget::on_cancel_clicked() return; // now discard all the dives - clear_table(thread.table()); + clear_dive_table(thread.table()); done(-1); } diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index b558ded99..2f3db5b2c 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -1769,7 +1769,7 @@ void MainWindow::on_actionImportDiveSites_triggered() sites.dive_sites[i]->dives.nr = 0; // Now we can clear the imported dives and trips. - clear_table(&table); + clear_dive_table(&table); clear_trip_table(&trips); QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");