Refactoring: Improve event_loop.

Improve the event loop architecture by making it set the divecomputer in
the constructor - using the same loop for multiple dive computers is not
intended to work.
Also change `next()` in `divemode_loop` to `at()` to make the name more
aligned with its function.

Signed-off-by: Michael Keller <github@ike.ch>
This commit is contained in:
Michael Keller 2024-09-03 19:48:49 +12:00 committed by bstoeger
parent 33bb39f1ca
commit ee25e8a1db
11 changed files with 28 additions and 33 deletions

View file

@ -81,27 +81,22 @@ enum event_severity event::get_severity() const
}
}
event_loop::event_loop(const char *name) : name(name), idx(0)
event_loop::event_loop(const char *name, const struct divecomputer &dc) : name(name), idx(0), dc(dc)
{
}
struct event *event_loop::next(struct divecomputer &dc)
const struct event *event_loop::next()
{
if (name.empty())
return nullptr;
while (idx < dc.events.size()) {
struct event &ev = dc.events[idx++];
const struct event &ev = dc.events[idx++];
if (ev.name == name)
return &ev;
}
return nullptr;
}
const struct event *event_loop::next(const struct divecomputer &dc)
{
return next(const_cast<divecomputer &>(dc));
}
struct event *get_first_event(struct divecomputer &dc, const std::string &name)
{
auto it = std::find_if(dc.events.begin(), dc.events.end(), [name](auto &ev) { return ev.name == name; });