Files
allwpilib/wpilibc/src/main/native/cpp/event/EventLoop.cpp

47 lines
1.1 KiB
C++
Raw Normal View History

// 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.
2025-11-07 19:56:21 -05:00
#include "wpi/event/EventLoop.hpp"
2024-09-20 17:43:39 -07:00
#include <utility>
2025-11-07 19:56:21 -05:00
#include "wpi/system/Errors.hpp"
2025-11-07 20:00:05 -05:00
using namespace wpi;
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() {}
2025-11-07 20:00:05 -05:00
void EventLoop::Bind(wpi::util::unique_function<void()> action) {
if (m_running) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::Error,
"Cannot bind EventLoop while it is running");
}
m_bindings.emplace_back(std::move(action));
}
void EventLoop::Poll() {
RunningSetter runSetter{m_running};
2025-11-07 20:00:05 -05:00
for (wpi::util::unique_function<void()>& action : m_bindings) {
action();
}
}
void EventLoop::Clear() {
if (m_running) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::Error,
"Cannot clear EventLoop while it is running");
}
m_bindings.clear();
}