diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-08-04 18:44:57 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-11-09 19:19:04 +0100 |
commit | 7c9f46acd202121e67557bb634961ef17a9f6c1f (patch) | |
tree | f21b6ff42a930cc97c31db9f20c03f1613e70291 /desktop-widgets/command_edit.cpp | |
parent | cd4f66014f7b5269dff9e088c9d7d38241c03156 (diff) | |
download | subsurface-7c9f46acd202121e67557bb634961ef17a9f6c1f.tar.gz |
Core: remove MAX_CYLINDERS restriction
Instead of using fixed size arrays, use a new cylinder_table structure.
The code copies the weightsystem code, but is significantly more complex
because cylinders are such an integral part of the core.
Two functions to access the cylinders were added:
get_cylinder() and get_or_create_cylinder()
The former does a simple array access and supposes that the cylinder
exists. The latter is used by the parser(s) and if a cylinder with
the given id does not exist, cylinders up to that id are generated.
One point will make C programmers cringe: the cylinder structure is
passed by value. This is due to the way the table-macros work. A
refactoring of the table macros is planned. It has to be noted that
the size of a cylinder_t is 64 bytes, i.e. 8 long words on a 64-bit
architecture, so passing on the stack is probably not even significantly
slower than passing as reference.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/command_edit.cpp')
-rw-r--r-- | desktop-widgets/command_edit.cpp | 45 |
1 files changed, 14 insertions, 31 deletions
diff --git a/desktop-widgets/command_edit.cpp b/desktop-widgets/command_edit.cpp index 95665722d..97fc05c95 100644 --- a/desktop-widgets/command_edit.cpp +++ b/desktop-widgets/command_edit.cpp @@ -666,18 +666,6 @@ DiveField EditDiveMaster::fieldId() const return DiveField::DIVEMASTER; } -// Helper function to copy cylinders. This supposes that the destination -// cylinder is uninitialized. I.e. the old description is not freed! -static void copy_cylinder(const cylinder_t &s, cylinder_t &d) -{ - d.type.description = copy_string(s.type.description); - d.type.size = s.type.size; - d.type.workingpressure = s.type.workingpressure; - d.gasmix = s.gasmix; - d.cylinder_use = s.cylinder_use; - d.depth = s.depth; -} - static void swapCandQString(QString &q, char *&c) { QString tmp(c); @@ -687,10 +675,9 @@ static void swapCandQString(QString &q, char *&c) } PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dIn), - tags(nullptr), - cylinders(MAX_CYLINDERS) + tags(nullptr) { - memset(&cylinders[0], 0, sizeof(cylinders)); + memset(&cylinders, 0, sizeof(cylinders)); memset(&weightsystems, 0, sizeof(weightsystems)); if (what.notes) notes = data->notes; @@ -708,10 +695,8 @@ PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dI divesite = data->dive_site; if (what.tags) tags = taglist_copy(data->tag_list); - if (what.cylinders) { - for (int i = 0; i < MAX_CYLINDERS; ++i) - copy_cylinder(data->cylinder[i], cylinders[i]); - } + if (what.cylinders) + copy_cylinders(&data->cylinders, &cylinders); if (what.weights) copy_weights(&data->weightsystems, &weightsystems); } @@ -719,8 +704,7 @@ PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dI PasteState::~PasteState() { taglist_free(tags); - for (cylinder_t &c: cylinders) - free((void *)c.type.description); + clear_cylinder_table(&cylinders); clear_weightsystem_table(&weightsystems); free(weightsystems.weightsystems); } @@ -743,10 +727,8 @@ void PasteState::swap(dive_components what) std::swap(divesite, d->dive_site); if (what.tags) std::swap(tags, d->tag_list); - if (what.cylinders) { - for (int i = 0; i < MAX_CYLINDERS; ++i) - std::swap(cylinders[i], d->cylinder[i]); - } + if (what.cylinders) + std::swap(cylinders, d->cylinders); if (what.weights) std::swap(weightsystems, d->weightsystems); } @@ -836,7 +818,7 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive), dc({ 0 }), notes(nullptr) { - memset(&cylinders[0], 0, sizeof(cylinders)); + memset(&cylinders, 0, sizeof(cylinders)); if (!d) return; @@ -849,7 +831,7 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive), surface_pressure = source->surface_pressure; // This resets the dive computers and cylinders of the source dive, avoiding deep copies. - std::swap(source->cylinder, cylinders); + std::swap(source->cylinders, cylinders); std::swap(source->dc, dc); setText(tr("Replan dive")); @@ -857,8 +839,7 @@ ReplanDive::ReplanDive(dive *source) : d(current_dive), ReplanDive::~ReplanDive() { - for (cylinder_t &c: cylinders) - free((void *)c.type.description); + clear_cylinder_table(&cylinders); free_dive_dcs(&dc); free(notes); } @@ -873,7 +854,7 @@ void ReplanDive::undo() std::swap(d->when, when); std::swap(d->maxdepth, maxdepth); std::swap(d->meandepth, meandepth); - std::swap(d->cylinder, cylinders); + std::swap(d->cylinders, cylinders); std::swap(d->dc, dc); std::swap(d->notes, notes); std::swap(d->surface_pressure, surface_pressure); @@ -882,9 +863,11 @@ void ReplanDive::undo() fixup_dive(d); QVector<dive *> divesToNotify = { d }; + // Note that we have to emit cylindersReset before divesChanged, because the divesChanged + // updates the DivePlotDataModel, which is out-of-sync and gets confused. + emit diveListNotifier.cylindersReset(divesToNotify); emit diveListNotifier.divesChanged(divesToNotify, DiveField::DATETIME | DiveField::DURATION | DiveField::DEPTH | DiveField::MODE | DiveField::NOTES | DiveField::SALINITY | DiveField::ATM_PRESS); - emit diveListNotifier.cylindersReset(divesToNotify); } // Redo and undo do the same |