divelog: turn owning-pointers into unique_ptr<>s

Since everything is C++ now, we can use unique_ptr<>s. This makes
the code significantly shorter, because we can now use the default
move constructor and assignment operators.

This has a semantic change when std::move()-ing the divelog:
now not the contents of the tables are moved, but the pointers.
That is, the moved-from object now has no more tables and
must not be used anymore. This made it necessary to replace
std::move()s by std::swap()s. In that regard, the old code was
in principle broken: it used moved-from objects, which may work
but usually doesn't.

This commit adds a myriad of .get() function calls where the code
expects a C-style pointer. The plan is to remove virtually all of
them, when we move free-standing functions into the class it acts
on. Or, replace C-style pointers by references where we don't support
NULL.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-13 19:34:43 +02:00 committed by bstoeger
parent 6e352d5281
commit d242198c99
22 changed files with 113 additions and 138 deletions

View file

@ -52,7 +52,7 @@ QVariant DiveImportedModel::data(const QModelIndex &index, int role) const
if (index.row() >= log.dives->nr)
return QVariant();
struct dive *d = get_dive_from_table(index.row(), log.dives);
struct dive *d = get_dive_from_table(index.row(), log.dives.get());
if (!d)
return QVariant();
@ -124,8 +124,10 @@ void DiveImportedModel::downloadThreadFinished()
{
beginResetModel();
// Move the table data from thread to model
log = std::move(thread.log);
// Move the table data from thread to model. Replace the downloads thread's log
// with an empty log, because it may reuse it.
log.clear();
std::swap(log, thread.log);
checkStates.resize(log.dives->nr);
std::fill(checkStates.begin(), checkStates.end(), true);
@ -150,8 +152,9 @@ struct divelog DiveImportedModel::consumeTables()
{
beginResetModel();
// Move tables to result
struct divelog res(std::move(log));
// Move tables to result and reset local tables (oldschool pre-C++11 flair).
struct divelog res;
std::swap(res, log);
// Reset indices
checkStates.clear();
@ -176,7 +179,7 @@ void DiveImportedModel::deleteDeselected()
j++;
} else {
beginRemoveRows(QModelIndex(), j, j);
delete_dive_from_table(log.dives, j);
delete_dive_from_table(log.dives.get(), j);
endRemoveRows();
}
}