From c0e834cffc7d9733e87e4ea6a33acaf2a41122c0 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 25 May 2024 20:12:10 +0200 Subject: [PATCH] core: fold event-related functions into event class Not strictly necessary, but more idiomatic C++ and less polution of the global namespace. This one is so trivial that there seems to be no reason not to do it. Signed-off-by: Berthold Stoeger --- core/dive.cpp | 10 +++++----- core/event.cpp | 13 ++++++------- core/event.h | 8 ++++---- core/eventtype.cpp | 4 ++-- core/libdivecomputer.cpp | 2 +- core/load-git.cpp | 2 +- core/parse.cpp | 2 +- core/save-git.cpp | 2 +- core/save-xml.cpp | 2 +- profile-widget/diveeventitem.cpp | 6 +++--- profile-widget/profilescene.cpp | 4 ++-- profile-widget/profilewidget2.cpp | 2 +- 12 files changed, 28 insertions(+), 29 deletions(-) diff --git a/core/dive.cpp b/core/dive.cpp index b9c51c1cf..4b45dcb63 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -130,7 +130,7 @@ void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int second struct gasmix get_gasmix_from_event(const struct dive *dive, const struct event &ev) { - if (event_is_gaschange(ev)) { + if (ev.is_gaschange()) { int index = ev.gas.index; // FIXME: The planner uses one past cylinder-count to signify "surface air". Remove in due course. if (index == dive->cylinders.nr) @@ -311,7 +311,7 @@ void copy_events_until(const struct dive *sd, struct dive *dd, int dcNr, int tim for (const auto &ev: s->events) { // Don't add events the planner knows about - if (ev.time.seconds < time && !event_is_gaschange(ev) && !event_is_divemodechange(ev)) + if (ev.time.seconds < time && !ev.is_gaschange() && !ev.is_divemodechange()) add_event(d, ev.time.seconds, ev.type, ev.flags, ev.value, ev.name); } } @@ -1051,7 +1051,7 @@ static bool validate_gaschange(struct dive *dive, struct event &event) /* Clean up event, return true if event is ok, false if it should be dropped as bogus */ static bool validate_event(struct dive *dive, struct event &event) { - if (event_is_gaschange(event)) + if (event.is_gaschange()) return validate_gaschange(dive, event); return true; } @@ -1483,7 +1483,7 @@ pick_b: * If that's a gas-change that matches the previous * gas change, we'll just skip it */ - if (event_is_gaschange(*pick)) { + if (pick->is_gaschange()) { if (last_gas && same_gas(pick, last_gas)) continue; last_gas = pick; @@ -1567,7 +1567,7 @@ static void renumber_last_sample(struct divecomputer *dc, const int mapping[]) static void event_renumber(struct event &ev, const int mapping[]) { - if (!event_is_gaschange(ev)) + if (!ev.is_gaschange()) return; if (ev.gas.index < 0) return; diff --git a/core/event.cpp b/core/event.cpp index 73546efcf..08774823f 100644 --- a/core/event.cpp +++ b/core/event.cpp @@ -49,15 +49,14 @@ event::~event() { } -bool event_is_gaschange(const struct event &ev) +bool event::is_gaschange() const { - return ev.type == SAMPLE_EVENT_GASCHANGE || - ev.type == SAMPLE_EVENT_GASCHANGE2; + return type == SAMPLE_EVENT_GASCHANGE || type == SAMPLE_EVENT_GASCHANGE2; } -bool event_is_divemodechange(const struct event &ev) +bool event::is_divemodechange() const { - return ev.name == "modechange"; + return name == "modechange"; } bool event::operator==(const event &b) const @@ -66,9 +65,9 @@ bool event::operator==(const event &b) const std::tie(b.time.seconds, b.type, b.flags, b.value, b.name); } -extern enum event_severity get_event_severity(const struct event &ev) +enum event_severity event::get_severity() const { - switch (ev.flags & SAMPLE_FLAGS_SEVERITY_MASK) { + switch (flags & SAMPLE_FLAGS_SEVERITY_MASK) { case SAMPLE_FLAGS_SEVERITY_INFO: return EVENT_SEVERITY_INFO; case SAMPLE_FLAGS_SEVERITY_WARN: diff --git a/core/event.h b/core/event.h index 585a5f3bd..6da0c5020 100644 --- a/core/event.h +++ b/core/event.h @@ -48,6 +48,10 @@ struct event { ~event(); bool operator==(const event &b2) const; + + bool is_gaschange() const; + bool is_divemodechange() const; + event_severity get_severity() const; }; class event_loop @@ -83,10 +87,6 @@ public: divemode_t next(int time); }; -extern bool event_is_gaschange(const struct event &ev); -extern bool event_is_divemodechange(const struct event &ev); -extern enum event_severity get_event_severity(const struct event &ev); - extern const struct event *get_first_event(const struct divecomputer &dc, const std::string &name); extern struct event *get_first_event(struct divecomputer &dc, const std::string &name); diff --git a/core/eventtype.cpp b/core/eventtype.cpp index fe0cc2a5b..4352707af 100644 --- a/core/eventtype.cpp +++ b/core/eventtype.cpp @@ -14,7 +14,7 @@ struct event_type { bool plot; event_type(const struct event *ev) : name(ev->name), - severity(get_event_severity(*ev)), + severity(ev->get_severity()), plot(true) { } @@ -108,7 +108,7 @@ QString event_type_name(const event &ev) return QString(); QString name = QString::fromStdString(ev.name); - return event_type_name(std::move(name), get_event_severity(ev)); + return event_type_name(std::move(name), ev.get_severity()); } QString event_type_name(int idx) diff --git a/core/libdivecomputer.cpp b/core/libdivecomputer.cpp index 7ab229c21..992e37e74 100644 --- a/core/libdivecomputer.cpp +++ b/core/libdivecomputer.cpp @@ -363,7 +363,7 @@ static void handle_event(struct divecomputer *dc, const struct sample &sample, d time += sample.time.seconds; ev = add_event(dc, time, type, value.event.flags, value.event.value, name); - if (event_is_gaschange(*ev) && ev->gas.index >= 0) + if (ev->is_gaschange() && ev->gas.index >= 0) current_gas_index = ev->gas.index; } diff --git a/core/load-git.cpp b/core/load-git.cpp index 9a1db99bd..fa92aff10 100644 --- a/core/load-git.cpp +++ b/core/load-git.cpp @@ -864,7 +864,7 @@ static void parse_dc_event(char *line, struct git_parser_state *state) if (p.ev.time.seconds == 0 && p.ev.type == SAMPLE_EVENT_PO2 && p.ev.value && state->active_dc->divemode==OC) state->active_dc->divemode = CCR; - if (event_is_gaschange(*ev)) { + if (ev->is_gaschange()) { /* * We subtract one here because "0" is "no index", * and the parsing will add one for actual cylinder diff --git a/core/parse.cpp b/core/parse.cpp index b25e7541d..b6d2c9461 100644 --- a/core/parse.cpp +++ b/core/parse.cpp @@ -91,7 +91,7 @@ void event_end(struct parser_state *state) if (ev.time.seconds == 0 && ev.type == SAMPLE_EVENT_PO2 && ev.value && dc->divemode==OC) dc->divemode = CCR; - if (event_is_gaschange(ev)) { + if (ev.is_gaschange()) { /* See try_to_fill_event() on why the filled-in index is one too big */ ev.gas.index = state->cur_event.gas.index-1; if (state->cur_event.gas.mix.o2.permille || state->cur_event.gas.mix.he.permille) diff --git a/core/save-git.cpp b/core/save-git.cpp index 3235e3622..c19319d33 100644 --- a/core/save-git.cpp +++ b/core/save-git.cpp @@ -394,7 +394,7 @@ static void save_one_event(struct membuffer *b, struct dive *dive, const struct else show_index(b, ev.value, "value=", ""); show_utf8(b, " name=", ev.name.c_str(), ""); - if (event_is_gaschange(ev)) { + if (ev.is_gaschange()) { struct gasmix mix = get_gasmix_from_event(dive, ev); if (ev.gas.index >= 0) show_integer(b, ev.gas.index, "cylinder=", ""); diff --git a/core/save-xml.cpp b/core/save-xml.cpp index 3cbc15238..4da3ed744 100644 --- a/core/save-xml.cpp +++ b/core/save-xml.cpp @@ -363,7 +363,7 @@ static void save_one_event(struct membuffer *b, struct dive *dive, const struct else show_index(b, ev.value, "value='", "'"); show_utf8(b, ev.name.c_str(), " name='", "'", 1); - if (event_is_gaschange(ev)) { + if (ev.is_gaschange()) { struct gasmix mix = get_gasmix_from_event(dive, ev); if (ev.gas.index >= 0) show_integer(b, ev.gas.index, "cylinder='", "'"); diff --git a/profile-widget/diveeventitem.cpp b/profile-widget/diveeventitem.cpp index 1ddf54ef6..cc1b49f73 100644 --- a/profile-widget/diveeventitem.cpp +++ b/profile-widget/diveeventitem.cpp @@ -39,7 +39,7 @@ DiveEventItem::~DiveEventItem() void DiveEventItem::setupPixmap(struct gasmix lastgasmix, const DivePixmaps &pixmaps) { - event_severity severity = get_event_severity(ev); + event_severity severity = ev.get_severity(); if (ev.name.empty()) { setPixmap(pixmaps.warning); } else if (same_string_caseinsensitive(ev.name.c_str(), "modechange")) { @@ -50,7 +50,7 @@ void DiveEventItem::setupPixmap(struct gasmix lastgasmix, const DivePixmaps &pix } else if (ev.type == SAMPLE_EVENT_BOOKMARK) { setPixmap(pixmaps.bookmark); setOffset(QPointF(0.0, -pixmap().height())); - } else if (event_is_gaschange(ev)) { + } else if (ev.is_gaschange()) { struct gasmix mix = get_gasmix_from_event(dive, ev); struct icd_data icd_data; bool icd = isobaric_counterdiffusion(lastgasmix, mix, &icd_data); @@ -121,7 +121,7 @@ void DiveEventItem::setupToolTipString(struct gasmix lastgasmix) int value = ev.value; int type = ev.type; - if (event_is_gaschange(ev)) { + if (ev.is_gaschange()) { struct icd_data icd_data; struct gasmix mix = get_gasmix_from_event(dive, ev); name += ": "; diff --git a/profile-widget/profilescene.cpp b/profile-widget/profilescene.cpp index 5f0037825..566a11286 100644 --- a/profile-widget/profilescene.cpp +++ b/profile-widget/profilescene.cpp @@ -559,7 +559,7 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM if (event.name.empty() || !(event.name == "heading" || (event.name == "SP change" && event.time.seconds == 0) || - event_is_gaschange(event) || + event.is_gaschange() || event.type == SAMPLE_EVENT_BOOKMARK)) continue; } @@ -570,7 +570,7 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM addItem(item); eventItems.push_back(item); } - if (event_is_gaschange(event)) + if (event.is_gaschange()) lastgasmix = get_gasmix_from_event(d, event); } diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp index fdc7b746f..aef0f578f 100644 --- a/profile-widget/profilewidget2.cpp +++ b/profile-widget/profilewidget2.cpp @@ -559,7 +559,7 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event) DiveEventItem *item = dynamic_cast(sceneItem); // Add or edit Gas Change - if (d && item && event_is_gaschange(item->ev)) { + if (d && item && item->ev.is_gaschange()) { int eventTime = item->ev.time.seconds; QMenu *gasChange = m.addMenu(tr("Edit Gas Change")); for (int i = 0; i < d->cylinders.nr; i++) {