core: make clone_* functions return a unique_ptr<>

Don't use plain pointers for owning pointers.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-04 07:10:22 +02:00 committed by bstoeger
parent 0ef497c3c9
commit 1e09ec77d7
4 changed files with 22 additions and 31 deletions

View file

@ -871,7 +871,7 @@ SplitDiveComputer::SplitDiveComputer(dive *d, int dc_num) :
setText(Command::Base::tr("split dive computer"));
}
DiveComputerBase::DiveComputerBase(dive *old_dive, dive *new_dive, int dc_nr_before, int dc_nr_after) :
DiveComputerBase::DiveComputerBase(dive *old_dive, std::unique_ptr<dive> new_dive, int dc_nr_before, int dc_nr_after) :
dc_nr_before(dc_nr_before),
dc_nr_after(dc_nr_after)
{
@ -891,7 +891,7 @@ DiveComputerBase::DiveComputerBase(dive *old_dive, dive *new_dive, int dc_nr_bef
new_dive->dive_site = nullptr;
diveToAdd.dives.resize(1);
diveToAdd.dives[0].dive.reset(new_dive);
diveToAdd.dives[0].dive = std::move(new_dive);
diveToAdd.dives[0].trip = old_dive->divetrip;
diveToAdd.dives[0].site = old_dive->dive_site;
}
@ -921,13 +921,13 @@ void DiveComputerBase::undoit()
}
MoveDiveComputerToFront::MoveDiveComputerToFront(dive *d, int dc_num)
: DiveComputerBase(d, make_first_dc(d, dc_num), dc_num, 0)
: DiveComputerBase(d, clone_make_first_dc(*d, dc_num), dc_num, 0)
{
setText(Command::Base::tr("move dive computer to front"));
}
DeleteDiveComputer::DeleteDiveComputer(dive *d, int dc_num)
: DiveComputerBase(d, clone_delete_divecomputer(d, dc_num), dc_num, std::min((int)number_of_computers(d) - 1, dc_num))
: DiveComputerBase(d, clone_delete_divecomputer(*d, dc_num), dc_num, std::min((int)number_of_computers(d) - 1, dc_num))
{
setText(Command::Base::tr("delete dive computer"));
}

View file

@ -237,7 +237,7 @@ class DiveComputerBase : public DiveListBase {
protected:
// old_dive must be a dive known to the core.
// new_dive must be new dive whose ownership is taken.
DiveComputerBase(dive *old_dive, dive *new_dive, int dc_nr_before, int dc_nr_after);
DiveComputerBase(dive *old_dive, std::unique_ptr<dive> new_dive, int dc_nr_before, int dc_nr_after);
private:
void undoit() override;
void redoit() override;

View file

@ -2672,47 +2672,38 @@ void set_git_prefs(const char *prefs)
}
/* clones a dive and moves given dive computer to front */
struct dive *make_first_dc(const struct dive *d, int dc_number)
std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number)
{
/* copy the dive */
dive *res = new dive;
copy_dive(d, res);
auto res = std::make_unique<dive>(d);
/* make a new unique id, since we still can't handle two equal ids */
res->id = dive_getUniqID();
if (dc_number == 0)
return res;
move_in_range(res->dcs, dc_number, dc_number + 1, 0);
if (dc_number != 0)
move_in_range(res->dcs, dc_number, dc_number + 1, 0);
return res;
}
static void delete_divecomputer(struct dive *d, int num)
{
/* Refuse to delete the last dive computer */
if (d->dcs.size() <= 1)
return;
if (num < 0 || num >= (int)d->dcs.size())
return;
d->dcs.erase(d->dcs.begin() + num);
}
/* Clone a dive and delete given dive computer */
struct dive *clone_delete_divecomputer(const struct dive *d, int dc_number)
std::unique_ptr<dive> clone_delete_divecomputer(const struct dive &d, int dc_number)
{
/* copy the dive */
dive *res = new dive;
copy_dive(d, res);
auto res = std::make_unique<dive>(d);
/* make a new unique id, since we still can't handle two equal ids */
res->id = dive_getUniqID();
delete_divecomputer(res, dc_number);
force_fixup_dive(res);
if (res->dcs.size() <= 1)
return res;
if (dc_number < 0 || static_cast<size_t>(dc_number) >= res->dcs.size())
return res;
res->dcs.erase(res->dcs.begin() + dc_number);
force_fixup_dive(res.get());
return res;
}

View file

@ -143,8 +143,8 @@ extern temperature_t dc_watertemp(const struct dive *dive);
extern void set_git_prefs(const char *prefs);
extern struct dive *make_first_dc(const struct dive *d, int dc_number);
extern struct dive *clone_delete_divecomputer(const struct dive *d, int dc_number);
extern std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number);
extern std::unique_ptr<dive> clone_delete_divecomputer(const struct dive &d, int dc_number);
extern std::array<std::unique_ptr<dive>, 2> split_divecomputer(const struct dive &src, int num);
/*