Statistics: remove global state / calculate only when needed

Statistics were calculated into global variables every time the
current dive was changed.

Calculate statistics only when needed and into a structure
provided by the caller.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-10-06 10:58:12 +02:00 committed by Dirk Hohndel
parent 68fdc0b6f4
commit 97991e2b9f
7 changed files with 141 additions and 107 deletions

View file

@ -12,10 +12,6 @@
#include "core/units.h"
#include "core/dive.h" // For MAX_CYLINDERS
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
int period;
@ -42,13 +38,22 @@ typedef struct
char *location;
} stats_t;
extern stats_t stats_selection;
extern stats_t *stats_yearly;
extern stats_t *stats_monthly;
extern stats_t *stats_by_trip;
extern stats_t *stats_by_type;
struct stats_summary {
stats_t *stats_yearly;
stats_t *stats_monthly;
stats_t *stats_by_trip;
stats_t *stats_by_type;
};
#ifdef __cplusplus
extern "C" {
#endif
extern char *get_minutes(int seconds);
extern void process_all_dives();
extern void init_stats_summary(struct stats_summary *stats);
extern void free_stats_summary(struct stats_summary *stats);
extern void calculate_stats_summary(struct stats_summary *stats);
extern void get_gas_used(struct dive *dive, volume_t gases[MAX_CYLINDERS]);
extern void process_selected_dives(void);
void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot);
@ -57,4 +62,25 @@ void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot);
}
#endif
/*
* For C++ code, provide a convenience version of stats_summary
* that initializes the structure on construction and frees
* resources when it goes out of scope. Apart from that, it
* can be used as a stats_summary replacement.
*/
#ifdef __cplusplus
struct stats_summary_auto_free : public stats_summary {
stats_summary_auto_free();
~stats_summary_auto_free();
};
inline stats_summary_auto_free::stats_summary_auto_free()
{
init_stats_summary(this);
}
inline stats_summary_auto_free::~stats_summary_auto_free()
{
free_stats_summary(this);
}
#endif
#endif // STATISTICS_H