diff --git a/commands/command_divelist.cpp b/commands/command_divelist.cpp index 46d4f0a30..514ca98a0 100644 --- a/commands/command_divelist.cpp +++ b/commands/command_divelist.cpp @@ -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 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")); } diff --git a/commands/command_divelist.h b/commands/command_divelist.h index bdc0f28ec..b161d7aba 100644 --- a/commands/command_divelist.h +++ b/commands/command_divelist.h @@ -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 new_dive, int dc_nr_before, int dc_nr_after); private: void undoit() override; void redoit() override; diff --git a/core/dive.cpp b/core/dive.cpp index 8c769e2c0..694814068 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -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 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(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 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(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(dc_number) >= res->dcs.size()) + return res; + + res->dcs.erase(res->dcs.begin() + dc_number); + + force_fixup_dive(res.get()); return res; } diff --git a/core/dive.h b/core/dive.h index caa516b48..ffd00f5b2 100644 --- a/core/dive.h +++ b/core/dive.h @@ -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 clone_make_first_dc(const struct dive &d, int dc_number); +extern std::unique_ptr clone_delete_divecomputer(const struct dive &d, int dc_number); extern std::array, 2> split_divecomputer(const struct dive &src, int num); /*