From 7c9f46acd202121e67557bb634961ef17a9f6c1f Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 4 Aug 2019 18:44:57 +0200 Subject: 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 --- core/subsurface-qt/DiveObjectHelper.cpp | 38 +++++++++++++++------------------ 1 file changed, 17 insertions(+), 21 deletions(-) (limited to 'core/subsurface-qt/DiveObjectHelper.cpp') diff --git a/core/subsurface-qt/DiveObjectHelper.cpp b/core/subsurface-qt/DiveObjectHelper.cpp index 5146a6ea1..1deffdd4e 100644 --- a/core/subsurface-qt/DiveObjectHelper.cpp +++ b/core/subsurface-qt/DiveObjectHelper.cpp @@ -32,7 +32,7 @@ static QString getFormattedWeight(const struct dive *dive, unsigned int idx) static QString getFormattedCylinder(const struct dive *dive, unsigned int idx) { - const cylinder_t *cyl = &dive->cylinder[idx]; + const cylinder_t *cyl = &dive->cylinders.cylinders[idx]; const char *desc = cyl->type.description; if (!desc && idx > 0) return QString(); @@ -46,7 +46,7 @@ static QString getFormattedCylinder(const struct dive *dive, unsigned int idx) static QString getPressures(const struct dive *dive, int i, enum returnPressureSelector ret) { - const cylinder_t *cyl = &dive->cylinder[i]; + const cylinder_t *cyl = &dive->cylinders.cylinders[i]; QString fmt; if (ret == START_PRESSURE) { if (cyl->start.mbar) @@ -101,13 +101,13 @@ static QString formatGas(const dive *d) * from the get_gas_string function or this is correct? */ QString gas, gases; - for (int i = 0; i < MAX_CYLINDERS; i++) { + for (int i = 0; i < d->cylinders.nr; i++) { if (!is_cylinder_used(d, i)) continue; - gas = d->cylinder[i].type.description; + gas = d->cylinders.cylinders[i].type.description; if (!gas.isEmpty()) gas += QChar(' '); - gas += gasname(d->cylinder[i].gasmix); + gas += gasname(d->cylinders.cylinders[i].gasmix); // if has a description and if such gas is not already present if (!gas.isEmpty() && gases.indexOf(gas) == -1) { if (!gases.isEmpty()) @@ -155,10 +155,8 @@ static QStringList formatWeights(const dive *d) QStringList formatCylinders(const dive *d) { QStringList cylinders; - for (int i = 0; i < MAX_CYLINDERS; i++) { + for (int i = 0; i < d->cylinders.nr; i++) { QString cyl = getFormattedCylinder(d, i); - if (cyl.isEmpty()) - continue; cylinders << cyl; } return cylinders; @@ -167,10 +165,10 @@ QStringList formatCylinders(const dive *d) static QVector makeCylinderObjects(const dive *d) { QVector res; - for (int i = 0; i < MAX_CYLINDERS; i++) { + for (int i = 0; i < d->cylinders.nr; i++) { //Don't add blank cylinders, only those that have been defined. - if (d->cylinder[i].type.description) - res.append(CylinderObjectHelper(&d->cylinder[i])); // no emplace for QVector. :( + if (d->cylinders.cylinders[i].type.description) + res.append(CylinderObjectHelper(&d->cylinders.cylinders[i])); // no emplace for QVector. :( } return res; } @@ -178,9 +176,9 @@ static QVector makeCylinderObjects(const dive *d) QStringList formatGetCylinder(const dive *d) { QStringList getCylinder; - for (int i = 0; i < MAX_CYLINDERS; i++) { + for (int i = 0; i < d->cylinders.nr; i++) { if (is_cylinder_used(d, i)) - getCylinder << d->cylinder[i].type.description; + getCylinder << d->cylinders.cylinders[i].type.description; } return getCylinder; } @@ -188,7 +186,7 @@ QStringList formatGetCylinder(const dive *d) QStringList getStartPressure(const dive *d) { QStringList startPressure; - for (int i = 0; i < MAX_CYLINDERS; i++) { + for (int i = 0; i < d->cylinders.nr; i++) { if (is_cylinder_used(d, i)) startPressure << getPressures(d, i, START_PRESSURE); } @@ -198,7 +196,7 @@ QStringList getStartPressure(const dive *d) QStringList getEndPressure(const dive *d) { QStringList endPressure; - for (int i = 0; i < MAX_CYLINDERS; i++) { + for (int i = 0; i < d->cylinders.nr; i++) { if (is_cylinder_used(d, i)) endPressure << getPressures(d, i, END_PRESSURE); } @@ -208,9 +206,9 @@ QStringList getEndPressure(const dive *d) QStringList getFirstGas(const dive *d) { QStringList gas; - for (int i = 0; i < MAX_CYLINDERS; i++) { + for (int i = 0; i < d->cylinders.nr; i++) { if (is_cylinder_used(d, i)) - gas << get_gas_string(d->cylinder[i].gasmix); + gas << get_gas_string(d->cylinders.cylinders[i].gasmix); } return gas; } @@ -221,10 +219,8 @@ QStringList getFullCylinderList() int i = 0; struct dive *d; for_each_dive (i, d) { - for (int j = 0; j < MAX_CYLINDERS; j++) { - QString cyl = d->cylinder[j].type.description; - if (cyl.isEmpty()) - continue; + for (int j = 0; j < d->cylinders.nr; j++) { + QString cyl = d->cylinders.cylinders[j].type.description; cylinders << cyl; } } -- cgit v1.2.3-70-g09d2