From 477748922d304c45df07ff7b4fac2dbe0209b457 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Fri, 3 Feb 2023 18:18:31 -0500 Subject: [PATCH] [wpilib] Throw on nonexistent SimDevice name in SimDeviceSim constructor (#5041) Previously this would just create a object that was otherwise non-functional. --- .../native/cpp/simulation/SimDeviceSim.cpp | 19 ++++++++++++++++++- .../wpilibj/simulation/SimDeviceSim.java | 3 +++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp b/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp index 70e43e72dd2..21a9b58af7c 100644 --- a/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp @@ -11,20 +11,37 @@ #include #include +#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) {} diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java index a64e2f33af3..bd86f16fee1 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SimDeviceSim.java @@ -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 + "'."); + } } /**