core: convert sample.c to C++ and add default constructor

This changes default behavior when creating a sample struct
in C++ code: it is now initialized to default values. If this
ever turns out to be a performance problem, we can either add
additional constructors or use special functions that do
not initialize memory, such as make_unique_for_overwrite.

This removes non-standard (respectively >C++20) constructs,
namely designated initializers.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-02-27 16:31:44 +01:00 committed by Michael Keller
parent c27e093ebd
commit 148775f418
5 changed files with 30 additions and 6 deletions

View file

@ -66,7 +66,7 @@ SOURCES += subsurface-mobile-main.cpp \
core/parse.c \ core/parse.c \
core/picture.c \ core/picture.c \
core/pictureobj.cpp \ core/pictureobj.cpp \
core/sample.c \ core/sample.cpp \
core/import-suunto.c \ core/import-suunto.c \
core/import-shearwater.c \ core/import-shearwater.c \
core/import-seac.c \ core/import-seac.c \

View file

@ -153,7 +153,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
qthelper.cpp qthelper.cpp
qthelper.h qthelper.h
range.h range.h
sample.c sample.cpp
sample.h sample.h
save-git.c save-git.c
save-html.c save-html.c

View file

@ -1337,7 +1337,7 @@ static void merge_one_sample(const struct sample *sample, int time, struct divec
* a minute apart, and shallower than 5m * a minute apart, and shallower than 5m
*/ */
if (time > last_time + 60 && last_depth < 5000) { if (time > last_time + 60 && last_depth < 5000) {
struct sample surface = { 0 }; struct sample surface;
/* Init a few values from prev sample to avoid useless info in XML */ /* Init a few values from prev sample to avoid useless info in XML */
surface.bearing.degrees = prev->bearing.degrees; surface.bearing.degrees = prev->bearing.degrees;
@ -1388,7 +1388,7 @@ static void merge_samples(struct divecomputer *res,
for (;;) { for (;;) {
int j; int j;
int at, bt; int at, bt;
struct sample sample = { .ndl{ -1 } , .bearing{ -1 } }; struct sample sample;
if (!res) if (!res)
return; return;

View file

@ -2,6 +2,28 @@
#include "sample.h" #include "sample.h"
sample::sample() :
time({ 0 }),
stoptime({ 0 }),
ndl({ -1 }),
tts({ 0 }),
rbt({ 0 }),
depth({ 0 }),
stopdepth({ 0 }),
temperature({ 0 }),
pressure { { 0 }, { 0 } },
setpoint({ 0 }),
o2sensor { { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } },
bearing({ -1 }),
sensor { 0, 0 },
cns(0),
heartbeat(0),
sac({ 0 }),
in_deco(false),
manually_entered(false)
{
}
/* /*
* Adding a cylinder pressure sample field is not quite as trivial as it * Adding a cylinder pressure sample field is not quite as trivial as it
* perhaps should be. * perhaps should be.
@ -15,7 +37,7 @@
* from the previous sample, so the indices are pre-populated (but the * from the previous sample, so the indices are pre-populated (but the
* pressures obviously are not) * pressures obviously are not)
*/ */
void add_sample_pressure(struct sample *sample, int sensor, int mbar) extern "C" void add_sample_pressure(struct sample *sample, int sensor, int mbar)
{ {
int idx; int idx;
@ -42,4 +64,3 @@ void add_sample_pressure(struct sample *sample, int sensor, int mbar)
/* We do not have enough slots for the pressure samples. */ /* We do not have enough slots for the pressure samples. */
/* Should we warn the user about dropping pressure data? */ /* Should we warn the user about dropping pressure data? */
} }

View file

@ -33,6 +33,9 @@ struct sample // BASE TYPE BYTES UNITS RANGE
bool in_deco; // bool 1 y/n y/n this sample is part of deco bool in_deco; // bool 1 y/n y/n this sample is part of deco
bool manually_entered; // bool 1 y/n y/n this sample was entered by the user, bool manually_entered; // bool 1 y/n y/n this sample was entered by the user,
// not calculated when planning a dive // not calculated when planning a dive
#ifdef __cplusplus
sample(); // Default constructor
#endif
}; // Total size of structure: 63 bytes, excluding padding at end }; // Total size of structure: 63 bytes, excluding padding at end
extern void add_sample_pressure(struct sample *sample, int sensor, int mbar); extern void add_sample_pressure(struct sample *sample, int sensor, int mbar);