diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-10-06 10:58:12 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-10-07 17:50:51 +0300 |
commit | 97991e2b9fff4254c2b40417bf6d7496ba0d849f (patch) | |
tree | a2b9af681e9ac5e72e4846736df001f75f7cc788 /core/statistics.h | |
parent | 68fdc0b6f49c81cb61387b7d006807509280c9df (diff) | |
download | subsurface-97991e2b9fff4254c2b40417bf6d7496ba0d849f.tar.gz |
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>
Diffstat (limited to 'core/statistics.h')
-rw-r--r-- | core/statistics.h | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/core/statistics.h b/core/statistics.h index 7766d3154..4639ecf3f 100644 --- a/core/statistics.h +++ b/core/statistics.h @@ -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 |