2022-06-09 08:16:51 +03:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
#include "frc/event/EventLoop.h"
|
|
|
|
|
|
2023-12-31 22:45:10 -08:00
|
|
|
#include "frc/Errors.h"
|
|
|
|
|
|
2022-06-09 08:16:51 +03:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2023-12-31 22:45:10 -08:00
|
|
|
namespace {
|
|
|
|
|
struct RunningSetter {
|
|
|
|
|
bool& m_running;
|
|
|
|
|
explicit RunningSetter(bool& running) noexcept : m_running{running} {
|
|
|
|
|
m_running = true;
|
|
|
|
|
}
|
|
|
|
|
~RunningSetter() noexcept { m_running = false; }
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2022-06-09 08:16:51 +03:00
|
|
|
EventLoop::EventLoop() {}
|
|
|
|
|
|
2022-11-28 23:48:48 +02:00
|
|
|
void EventLoop::Bind(wpi::unique_function<void()> action) {
|
2023-12-31 22:45:10 -08:00
|
|
|
if (m_running) {
|
|
|
|
|
throw FRC_MakeError(err::Error,
|
|
|
|
|
"Cannot bind EventLoop while it is running");
|
|
|
|
|
}
|
2022-11-28 23:48:48 +02:00
|
|
|
m_bindings.emplace_back(std::move(action));
|
2022-06-09 08:16:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EventLoop::Poll() {
|
2023-12-31 22:45:10 -08:00
|
|
|
RunningSetter runSetter{m_running};
|
2022-11-28 23:48:48 +02:00
|
|
|
for (wpi::unique_function<void()>& action : m_bindings) {
|
|
|
|
|
action();
|
2022-06-09 08:16:51 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EventLoop::Clear() {
|
2023-12-31 22:45:10 -08:00
|
|
|
if (m_running) {
|
|
|
|
|
throw FRC_MakeError(err::Error,
|
|
|
|
|
"Cannot clear EventLoop while it is running");
|
|
|
|
|
}
|
2022-06-09 08:16:51 +03:00
|
|
|
m_bindings.clear();
|
|
|
|
|
}
|