From fe3021b88a414b6963891a65b738f52bc41e36a8 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 20 Aug 2020 07:31:04 +0200 Subject: cleanup: consistently use get_cylinder() accessor get_cylinder(d, i) is more readable than d->cylinders.cylinders[i]. Moreover, it does bound checking and is more flexible with respect to changing the core data structures. Most places already used this accessor, but some still accessed the cylinders directly. This patch unifies the accesses by consistently switching to get_cylinder(). The affected code is in C++ and accesses the cylinder as reference or object, whereas the get_cylinder() function is C and returns a pointer. This results in funky looking "*get_cylinder(d, i)" expressions. Arguably still better than the original. Signed-off-by: Berthold Stoeger --- commands/command_edit.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'commands') diff --git a/commands/command_edit.cpp b/commands/command_edit.cpp index 248a8e08a..ae97594e4 100644 --- a/commands/command_edit.cpp +++ b/commands/command_edit.cpp @@ -668,7 +668,7 @@ PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dI // 2) For cylinders that do not yet exist in the destination dive, we set the pressures to 0, i.e. unset. // Moreover, for these we set the manually_added flag, because they weren't downloaded from a DC. for (int i = 0; i < d->cylinders.nr && i < cylinders.nr; ++i) { - const cylinder_t &src = d->cylinders.cylinders[i]; + const cylinder_t &src = *get_cylinder(d, i); cylinder_t &dst = cylinders.cylinders[i]; dst.gasmix = src.gasmix; dst.start = src.start; @@ -1112,7 +1112,7 @@ static bool same_cylinder_with_flags(const cylinder_t &cyl1, const cylinder_t &c static int find_cylinder_index(const struct dive *d, const cylinder_t &cyl, int sameCylinderFlags) { for (int idx = 0; idx < d->cylinders.nr; ++idx) { - if (same_cylinder_with_flags(cyl, d->cylinders.cylinders[idx], sameCylinderFlags)) + if (same_cylinder_with_flags(cyl, *get_cylinder(d, idx), sameCylinderFlags)) return idx; } return -1; @@ -1126,7 +1126,7 @@ EditCylinderBase::EditCylinderBase(int index, bool currentDiveOnly, bool nonProt dives.clear(); return; } - const cylinder_t &orig = current->cylinders.cylinders[index]; + const cylinder_t &orig = *get_cylinder(current, index); std::vector divesNew; divesNew.reserve(dives.size()); @@ -1139,7 +1139,7 @@ EditCylinderBase::EditCylinderBase(int index, bool currentDiveOnly, bool nonProt continue; divesNew.push_back(d); indexes.push_back(idx); - cyl.push_back(clone_cylinder(d->cylinders.cylinders[idx])); + cyl.push_back(clone_cylinder(*get_cylinder(d, idx))); } dives = std::move(divesNew); } @@ -1258,7 +1258,7 @@ EditCylinder::EditCylinder(int index, cylinder_t cylIn, EditCylinderType typeIn, void EditCylinder::redo() { for (size_t i = 0; i < dives.size(); ++i) { - std::swap(dives[i]->cylinders.cylinders[indexes[i]], cyl[i]); + std::swap(*get_cylinder(dives[i], indexes[i]), cyl[i]); update_cylinder_related_info(dives[i]); emit diveListNotifier.cylinderEdited(dives[i], indexes[i]); invalidate_dive_cache(dives[i]); // Ensure that dive is written in git_save() -- cgit v1.2.3-70-g09d2