core: always keep an empty cylinder at the end of the cylinder array

This will be temporarilly used by the planner to mark consumption of
air at the surface. Do this by creating a new function add_cylinder,
which replaces add_to_cylinder_table() and takes care of always adding
a dummy cylinder at the end of the table. Make the original
add_to_cylinder_table() local, so that it cannot be accessed anymore.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-04-28 14:50:40 +02:00 committed by Robert C. Helling
parent 0c28821d28
commit b949bad026
10 changed files with 28 additions and 16 deletions

View file

@ -1167,7 +1167,7 @@ void RemoveCylinder::undo()
{
for (size_t i = 0; i < dives.size(); ++i) {
std::vector<int> mapping = get_cylinder_map_for_add(dives[i]->cylinders.nr, indexes[i]);
add_to_cylinder_table(&dives[i]->cylinders, indexes[i], clone_cylinder(cyl[i]));
add_cylinder(&dives[i]->cylinders, indexes[i], clone_cylinder(cyl[i]));
emit diveListNotifier.cylinderAdded(dives[i], indexes[i]);
invalidate_dive_cache(dives[i]); // Ensure that dive is written in git_save()
}

View file

@ -682,7 +682,7 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod,
cyl.gasmix.o2.permille = (log[CMD_O2_PERCENT] / 256
+ log[CMD_O2_PERCENT + 1]) * 10;
cyl.gasmix.he.permille = 0;
add_to_cylinder_table(&dive->cylinders, 0, cyl);
add_cylinder(&dive->cylinders, 0, cyl);
} else {
dc->model = "Commander";
dc->deviceid = array_uint32_le(buf + 0x31e); // serial no
@ -692,7 +692,7 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod,
cyl.gasmix.o2.permille = (log[CMD_O2_PERCENT + g * 2] / 256
+ log[CMD_O2_PERCENT + g * 2 + 1]) * 10;
cyl.gasmix.he.permille = 0;
add_to_cylinder_table(&dive->cylinders, g, cyl);
add_cylinder(&dive->cylinders, g, cyl);
}
}
@ -739,7 +739,7 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod,
cyl.gasmix.he.permille =
(log[EMC_HE_PERCENT + g * 2] / 256
+ log[EMC_HE_PERCENT + g * 2 + 1]) * 10;
add_to_cylinder_table(&dive->cylinders, g, cyl);
add_cylinder(&dive->cylinders, g, cyl);
}
tm.tm_year = log[EMC_YEAR];

View file

@ -56,7 +56,7 @@ MAKE_CLEAR_TABLE(weightsystem_table, weightsystems, weightsystem)
//static MAKE_GET_IDX(cylinder_table, cylinder_t, cylinders)
static MAKE_GROW_TABLE(cylinder_table, cylinder_t, cylinders)
//static MAKE_GET_INSERTION_INDEX(cylinder_table, cylinder_t, cylinders, cylinder_less_than)
MAKE_ADD_TO(cylinder_table, cylinder_t, cylinders)
static MAKE_ADD_TO(cylinder_table, cylinder_t, cylinders)
MAKE_REMOVE_FROM(cylinder_table, cylinders)
//MAKE_SORT(cylinder_table, cylinder_t, cylinders, comp_cylinders)
//MAKE_REMOVE(cylinder_table, cylinder_t, cylinder)
@ -144,11 +144,23 @@ cylinder_t clone_cylinder(cylinder_t cyl)
return res;
}
void add_cylinder(struct cylinder_table *t, int idx, cylinder_t cyl)
{
add_to_cylinder_table(t, idx, cyl);
/* FIXME: This is a horrible hack: we make sure that at the end of
* every single cylinder table there is an empty cylinder that can
* be used by the planner as "surface air" cylinder. Fix this.
*/
add_to_cylinder_table(t, t->nr, empty_cylinder);
t->nr--;
t->cylinders[t->nr].cylinder_use = NOT_USED;
}
/* Add a clone of a cylinder to the end of a cylinder table.
* Cloned in means that the description-string is copied. */
void add_cloned_cylinder(struct cylinder_table *t, cylinder_t cyl)
{
add_to_cylinder_table(t, t->nr, clone_cylinder(cyl));
add_cylinder(t, t->nr, clone_cylinder(cyl));
}
bool same_weightsystem(weightsystem_t w1, weightsystem_t w2)
@ -342,7 +354,7 @@ cylinder_t *add_empty_cylinder(struct cylinder_table *t)
{
cylinder_t cyl = empty_cylinder;
cyl.type.description = strdup("");
add_to_cylinder_table(t, t->nr, cyl);
add_cylinder(t, t->nr, cyl);
return &t->cylinders[t->nr - 1];
}

View file

@ -39,7 +39,7 @@ static const cylinder_t empty_cylinder = { { { 0 }, { 0 }, (const char *)0}, { {
* *not* pointers to cylinders. This has two crucial consequences:
* 1) Pointers to cylinders are not stable. They may be
* invalidated if the table is reallocated.
* 2) add_to_cylinder_table(), etc. take ownership of the
* 2) add_cylinder(), etc. take ownership of the
* cylinder. Notably of the description string. */
struct cylinder_table {
int nr, allocated;
@ -102,7 +102,7 @@ extern void add_to_weightsystem_table(struct weightsystem_table *, int idx, weig
/* Cylinder table functions */
extern void clear_cylinder_table(struct cylinder_table *);
extern void add_to_cylinder_table(struct cylinder_table *, int idx, cylinder_t cyl);
extern void add_cylinder(struct cylinder_table *, int idx, cylinder_t cyl);
void get_gas_string(struct gasmix gasmix, char *text, int len);
const char *gasname(struct gasmix gasmix);

View file

@ -228,7 +228,7 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t
if (empty_string(cyl.type.description))
cyl.type.description = strdup(translate("gettextFromC", "unknown"));
add_to_cylinder_table(&dive->cylinders, dive->cylinders.nr, cyl);
add_cylinder(&dive->cylinders, dive->cylinders.nr, cyl);
}
return DC_STATUS_SUCCESS;
}

View file

@ -151,7 +151,7 @@ static void parse_dives(int log_version, const unsigned char *buf, unsigned int
for (i = 0; i < 1; i++) {
cylinder_t cyl = empty_cylinder;
fill_default_cylinder(dive, &cyl);
add_to_cylinder_table(&dive->cylinders, i, cyl);
add_cylinder(&dive->cylinders, i, cyl);
}
// Model 0=Xen, 1,2=Xeo, 4=Lynx, other=Liquivision

View file

@ -455,7 +455,7 @@ static void parse_dive_cylinder(char *line, struct membuffer *str, struct git_pa
if (cylinder.cylinder_use == OXYGEN)
state->o2pressure_sensor = state->active_dive->cylinders.nr;
add_to_cylinder_table(&state->active_dive->cylinders, state->active_dive->cylinders.nr, cylinder);
add_cylinder(&state->active_dive->cylinders, state->active_dive->cylinders.nr, cylinder);
}
static void parse_weightsystem_keyvalue(void *_ws, const char *key, const char *value)

View file

@ -1074,7 +1074,7 @@ bool plan(struct deco_state *ds, struct diveplan *diveplan, struct dive *dive, i
// If no empty cylinder is found, keep using last deco gas
cylinder_t cyl = empty_cylinder;
cyl.cylinder_use = NOT_USED;
add_to_cylinder_table(&dive->cylinders, dive->cylinders.nr, cyl);
add_cylinder(&dive->cylinders, dive->cylinders.nr, cyl);
current_cylinder = dive->cylinders.nr - 1;
plan_add_segment(diveplan, prefs.surface_segment, 0, current_cylinder, 0, false, OC);
}

View file

@ -490,7 +490,7 @@ void CylindersModel::add()
int row = d->cylinders.nr;
cylinder_t cyl = create_new_cylinder(d);
beginInsertRows(QModelIndex(), row, row);
add_to_cylinder_table(&d->cylinders, row, cyl);
add_cylinder(&d->cylinders, row, cyl);
endInsertRows();
emit dataChanged(createIndex(row, 0), createIndex(row, COLUMNS - 1));
}

View file

@ -174,14 +174,14 @@ void DivePlannerPointsModel::setupCylinders()
cylinder_t cyl = empty_cylinder;
fill_default_cylinder(&displayed_dive, &cyl);
cyl.start = cyl.type.workingpressure;
add_to_cylinder_table(&displayed_dive.cylinders, 0, cyl);
add_cylinder(&displayed_dive.cylinders, 0, cyl);
} else {
cylinder_t cyl = empty_cylinder;
// roughly an AL80
cyl.type.description = copy_qstring(tr("unknown"));
cyl.type.size.mliter = 11100;
cyl.type.workingpressure.mbar = 207000;
add_to_cylinder_table(&displayed_dive.cylinders, 0, cyl);
add_cylinder(&displayed_dive.cylinders, 0, cyl);
}
reset_cylinders(&displayed_dive, false);
cylinders.updateDive(&displayed_dive);