From 777e7f32a5c8b6633e3b1bc87fae0cdc2cf2ba8a Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 30 Jun 2024 22:12:25 +0200 Subject: [PATCH] core: replace same_weightsystem() by operator==() The important point is that this now takes a reference that avoid string copying. The old code used C-strings and therefore copy-semantics were OK. Signed-off-by: Berthold Stoeger --- commands/command_edit.cpp | 4 ++-- core/dive.cpp | 2 +- core/equipment.cpp | 6 +++--- core/equipment.h | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/commands/command_edit.cpp b/commands/command_edit.cpp index 5c6b17ace..97a33c571 100644 --- a/commands/command_edit.cpp +++ b/commands/command_edit.cpp @@ -951,7 +951,7 @@ void AddWeight::redo() static int find_weightsystem_index(const struct dive *d, const weightsystem_t &ws) { - return index_of_if(d->weightsystems, [&ws](auto &ws2) { return same_weightsystem(ws2, ws); }); + return index_of_if(d->weightsystems, [&ws](auto &ws2) { return ws == ws2; }); } EditWeightBase::EditWeightBase(int index, bool currentDiveOnly) : @@ -1047,7 +1047,7 @@ EditWeight::EditWeight(int index, weightsystem_t wsIn, bool currentDiveOnly) : new_ws.description = it->name; // If that doesn't change anything, do nothing - if (same_weightsystem(ws, new_ws)) { + if (ws == new_ws) { dives.clear(); return; } diff --git a/core/dive.cpp b/core/dive.cpp index 9740b75fc..c54a8bd0a 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -1720,7 +1720,7 @@ static void merge_cylinders(struct dive &res, const struct dive &a, const struct /* Check whether a weightsystem table contains a given weightsystem */ static bool has_weightsystem(const weightsystem_table &t, const weightsystem_t &w) { - return any_of(t.begin(), t.end(), [&w] (auto &w2) { return same_weightsystem(w, w2); }); + return any_of(t.begin(), t.end(), [&w] (auto &w2) { return w == w2; }); } static void merge_equipment(struct dive &res, const struct dive &a, const struct dive &b) diff --git a/core/equipment.cpp b/core/equipment.cpp index 0abe4ec5a..2f3f9def9 100644 --- a/core/equipment.cpp +++ b/core/equipment.cpp @@ -172,10 +172,10 @@ void add_cylinder(struct cylinder_table *t, int idx, cylinder_t cyl) t->insert(t->begin() + idx, std::move(cyl)); } -bool same_weightsystem(weightsystem_t w1, weightsystem_t w2) +bool weightsystem_t::operator==(const weightsystem_t &w2) const { - return w1.weight.grams == w2.weight.grams && - w1.description == w2.description; + return std::tie(weight.grams, description) == + std::tie(w2.weight.grams, w2.description); } void get_gas_string(struct gasmix gasmix, char *text, int len) diff --git a/core/equipment.h b/core/equipment.h index 08143a4d3..06dbae6dd 100644 --- a/core/equipment.h +++ b/core/equipment.h @@ -60,6 +60,8 @@ struct weightsystem_t weightsystem_t(); weightsystem_t(weight_t w, std::string desc, bool auto_filled); ~weightsystem_t(); + + bool operator==(const weightsystem_t &w2) const; }; /* Table of weightsystems. Attention: this stores weightsystems, @@ -72,7 +74,6 @@ extern enum cylinderuse cylinderuse_from_text(const char *text); extern void copy_cylinder_types(const struct dive *s, struct dive *d); extern cylinder_t *add_empty_cylinder(struct cylinder_table *t); extern cylinder_t *get_or_create_cylinder(struct dive *d, int idx); -extern bool same_weightsystem(weightsystem_t w1, weightsystem_t w2); extern void remove_cylinder(struct dive *dive, int idx); extern void remove_weightsystem(struct dive *dive, int idx); extern void set_weightsystem(struct dive *dive, int idx, weightsystem_t ws);