Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw on nonexistent/invalid SimDevice name in SimDeviceSim constructor #5041

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,37 @@
#include <hal/SimDevice.h>
#include <hal/simulation/SimDeviceData.h>

#include "frc/Errors.h"

using namespace frc;
using namespace frc::sim;

SimDeviceSim::SimDeviceSim(const char* name)
: m_handle{HALSIM_GetSimDeviceHandle(name)} {}
: m_handle{HALSIM_GetSimDeviceHandle(name)} {
if (m_handle == 0) {
throw FRC_MakeError(err::InvalidParameter,
"No sim device exists with name '{}'.", name);
}
}

SimDeviceSim::SimDeviceSim(const char* name, int index) {
m_handle =
HALSIM_GetSimDeviceHandle(fmt::format("{}[{}]", name, index).c_str());
if (m_handle == 0) {
throw FRC_MakeError(err::InvalidParameter,
"No sim device exists with name '{}[{}]'.", name,
index);
}
}

SimDeviceSim::SimDeviceSim(const char* name, int index, int channel) {
m_handle = HALSIM_GetSimDeviceHandle(
fmt::format("{}[{},{}]", name, index, channel).c_str());
if (m_handle == 0) {
throw FRC_MakeError(err::InvalidParameter,
"No sim device exists with name '{}[{},{}]'.", name,
index, channel);
}
}

SimDeviceSim::SimDeviceSim(HAL_SimDeviceHandle handle) : m_handle(handle) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class SimDeviceSim {
*/
public SimDeviceSim(String name) {
this(SimDeviceDataJNI.getSimDeviceHandle(name));
if (m_handle == 0) {
throw new IllegalArgumentException("No sim device exists with name '" + name + "'.");
}
}

/**
Expand Down