From d447c7dc3268329de062f18fbcbf1b6c3a9e0f74 Mon Sep 17 00:00:00 2001 From: Starlight220 <53231611+Starlight220@users.noreply.github.com> Date: Sat, 13 Feb 2021 08:17:13 +0200 Subject: [PATCH] [sim] Add SimDeviceSim ctor overloads (#3134) Better parallelism with SimDevice.create(), so teams don't have to mess with concatenating the index/channel themselves. --- .../native/cpp/simulation/SimDeviceSim.cpp | 18 ++++++++++++++++ .../include/frc/simulation/SimDeviceSim.h | 17 +++++++++++++++ .../wpilibj/simulation/SimDeviceSim.java | 21 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp b/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp index 272b4b0fbd..b8b6c69761 100644 --- a/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/SimDeviceSim.cpp @@ -9,6 +9,8 @@ #include #include +#include +#include using namespace frc; using namespace frc::sim; @@ -16,6 +18,22 @@ using namespace frc::sim; SimDeviceSim::SimDeviceSim(const char* name) : m_handle{HALSIM_GetSimDeviceHandle(name)} {} +SimDeviceSim::SimDeviceSim(const char* name, int index) { + wpi::SmallString<128> fullname; + wpi::raw_svector_ostream os(fullname); + os << name << '[' << index << ']'; + + m_handle = HALSIM_GetSimDeviceHandle(fullname.c_str()); +} + +SimDeviceSim::SimDeviceSim(const char* name, int index, int channel) { + wpi::SmallString<128> fullname; + wpi::raw_svector_ostream os(fullname); + os << name << '[' << index << ',' << channel << ']'; + + m_handle = HALSIM_GetSimDeviceHandle(fullname.c_str()); +} + hal::SimValue SimDeviceSim::GetValue(const char* name) const { return HALSIM_GetSimValueHandle(m_handle, name); } diff --git a/wpilibc/src/main/native/include/frc/simulation/SimDeviceSim.h b/wpilibc/src/main/native/include/frc/simulation/SimDeviceSim.h index b1882b56b3..0a4e4d8352 100644 --- a/wpilibc/src/main/native/include/frc/simulation/SimDeviceSim.h +++ b/wpilibc/src/main/native/include/frc/simulation/SimDeviceSim.h @@ -25,6 +25,23 @@ class SimDeviceSim { */ explicit SimDeviceSim(const char* name); + /** + * Constructs a SimDeviceSim. + * + * @param name name of the SimDevice + * @param index device index number to append to name + */ + SimDeviceSim(const char* name, int index); + + /** + * Constructs a SimDeviceSim. + * + * @param name name of the SimDevice + * @param index device index number to append to name + * @param channel device channel number to append to name + */ + SimDeviceSim(const char* name, int index, int channel); + /** * Get the property object with the given name. * 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 ba3d1797f2..4aee29fe42 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 @@ -28,6 +28,27 @@ public class SimDeviceSim { m_handle = SimDeviceDataJNI.getSimDeviceHandle(name); } + /** + * Constructs a SimDeviceSim. + * + * @param name name of the SimDevice + * @param index device index number to append to name + */ + public SimDeviceSim(String name, int index) { + this(name + "[" + index + "]"); + } + + /** + * Constructs a SimDeviceSim. + * + * @param name name of the SimDevice + * @param index device index number to append to name + * @param channel device channel number to append to name + */ + public SimDeviceSim(String name, int index, int channel) { + this(name + "[" + index + "," + channel + "]"); + } + /** * Get the property object with the given name. *