mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpilib] Throw early when EventLoop is modified while running (#6115)
This commit is contained in:
@@ -4,20 +4,41 @@
|
||||
|
||||
#include "frc/event/EventLoop.h"
|
||||
|
||||
#include "frc/Errors.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
namespace {
|
||||
struct RunningSetter {
|
||||
bool& m_running;
|
||||
explicit RunningSetter(bool& running) noexcept : m_running{running} {
|
||||
m_running = true;
|
||||
}
|
||||
~RunningSetter() noexcept { m_running = false; }
|
||||
};
|
||||
} // namespace
|
||||
|
||||
EventLoop::EventLoop() {}
|
||||
|
||||
void EventLoop::Bind(wpi::unique_function<void()> action) {
|
||||
if (m_running) {
|
||||
throw FRC_MakeError(err::Error,
|
||||
"Cannot bind EventLoop while it is running");
|
||||
}
|
||||
m_bindings.emplace_back(std::move(action));
|
||||
}
|
||||
|
||||
void EventLoop::Poll() {
|
||||
RunningSetter runSetter{m_running};
|
||||
for (wpi::unique_function<void()>& action : m_bindings) {
|
||||
action();
|
||||
}
|
||||
}
|
||||
|
||||
void EventLoop::Clear() {
|
||||
if (m_running) {
|
||||
throw FRC_MakeError(err::Error,
|
||||
"Cannot clear EventLoop while it is running");
|
||||
}
|
||||
m_bindings.clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user