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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-06-04 20:59:08 +02:00 committed by Dirk Hohndel
parent f4ef1c4bc8
commit 3df32044ee
8 changed files with 18 additions and 31 deletions

View file

@ -782,6 +782,7 @@ static MAKE_REMOVE_FROM(dive_table, dives)
static MAKE_GET_IDX(dive_table, struct dive *, dives) static MAKE_GET_IDX(dive_table, struct dive *, dives)
MAKE_SORT(dive_table, struct dive *, dives, comp_dives) MAKE_SORT(dive_table, struct dive *, dives, comp_dives)
MAKE_REMOVE(dive_table, struct dive *, dive) MAKE_REMOVE(dive_table, struct dive *, dive)
MAKE_CLEAR_TABLE(dive_table, dives, dive)
void insert_dive(struct dive_table *table, struct dive *d) 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 * 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 * 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; import_trip_table = &local_trip_table;
/* Make sure that output parameters don't contain garbage */ /* Make sure that output parameters don't contain garbage */
clear_table(dives_to_add); clear_dive_table(dives_to_add);
clear_table(dives_to_remove); clear_dive_table(dives_to_remove);
clear_trip_table(trips_to_add); clear_trip_table(trips_to_add);
clear_dive_site_table(sites_to_add); clear_dive_site_table(sites_to_add);

View file

@ -58,7 +58,7 @@ void reset_min_datafile_version();
void report_datafile_version(int version); void report_datafile_version(int version);
int get_dive_id_closest_to(timestamp_t when); int get_dive_id_closest_to(timestamp_t when);
void clear_dive_file_data(); void clear_dive_file_data();
void clear_table(struct dive_table *table); void clear_dive_table(struct dive_table *table);
#ifdef DEBUG_TRIP #ifdef DEBUG_TRIP
extern void dump_selection(void); extern void dump_selection(void);

View file

@ -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) static MAKE_GET_IDX(dive_site_table, struct dive_site *, dive_sites)
MAKE_SORT(dive_site_table, struct dive_site *, dive_sites, compare_sites) MAKE_SORT(dive_site_table, struct dive_site *, dive_sites, compare_sites)
static MAKE_REMOVE(dive_site_table, struct dive_site *, dive_site) 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) int add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_table)
{ {

View file

@ -82,7 +82,7 @@ void DownloadThread::run()
#endif #endif
qDebug() << "Starting download from " << (internalData->bluetooth_mode ? "BT" : internalData->devname); qDebug() << "Starting download from " << (internalData->bluetooth_mode ? "BT" : internalData->devname);
qDebug() << "downloading" << (internalData->force_download ? "all" : "only new") << "dives"; qDebug() << "downloading" << (internalData->force_download ? "all" : "only new") << "dives";
clear_table(&downloadTable); clear_dive_table(&downloadTable);
clear_dive_site_table(&diveSiteTable); clear_dive_site_table(&diveSiteTable);
Q_ASSERT(internalData->download_table != nullptr); Q_ASSERT(internalData->download_table != nullptr);

View file

@ -91,4 +91,12 @@
return idx; \ 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 #endif

View file

@ -49,6 +49,7 @@ static MAKE_ADD_TO(trip_table, struct dive_trip *, trips)
static MAKE_REMOVE_FROM(trip_table, trips) static MAKE_REMOVE_FROM(trip_table, trips)
MAKE_SORT(trip_table, struct dive_trip *, trips, comp_trips) MAKE_SORT(trip_table, struct dive_trip *, trips, comp_trips)
MAKE_REMOVE(trip_table, struct dive_trip *, trip) MAKE_REMOVE(trip_table, struct dive_trip *, trip)
MAKE_CLEAR_TABLE(trip_table, trips, trip)
timestamp_t trip_date(const struct dive_trip *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; 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. */ /* Trips are compared according to the first dive in the trip. */
int comp_trips(const struct dive_trip *a, const struct dive_trip *b) int comp_trips(const struct dive_trip *a, const struct dive_trip *b)
{ {

View file

@ -342,7 +342,7 @@ void DownloadFromDCWidget::on_downloadCancelRetryButton_clicked()
// this means we are retrying - so we better clean out the partial // this means we are retrying - so we better clean out the partial
// list of downloaded dives from the last attempt // list of downloaded dives from the last attempt
diveImportedModel->clearTable(); diveImportedModel->clearTable();
clear_table(thread.table()); clear_dive_table(thread.table());
} }
updateState(DOWNLOADING); updateState(DOWNLOADING);
@ -493,7 +493,7 @@ void DownloadFromDCWidget::on_cancel_clicked()
return; return;
// now discard all the dives // now discard all the dives
clear_table(thread.table()); clear_dive_table(thread.table());
done(-1); done(-1);
} }

View file

@ -1769,7 +1769,7 @@ void MainWindow::on_actionImportDiveSites_triggered()
sites.dive_sites[i]->dives.nr = 0; sites.dive_sites[i]->dives.nr = 0;
// Now we can clear the imported dives and trips. // Now we can clear the imported dives and trips.
clear_table(&table); clear_dive_table(&table);
clear_trip_table(&trips); clear_trip_table(&trips);
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files"); QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");