[hal] Replace const char* with std::string_view in Driver Station sim functions (#4532)

This commit is contained in:
Ryan Blue
2022-12-09 16:10:23 -05:00
committed by GitHub
parent 5a52b51443
commit bde383f763
11 changed files with 60 additions and 44 deletions

View File

@@ -38,7 +38,7 @@ class FMSModel : public Model {
virtual void SetEnabled(bool val) = 0;
virtual void SetTest(bool val) = 0;
virtual void SetAutonomous(bool val) = 0;
virtual void SetGameSpecificMessage(const char* val) = 0;
virtual void SetGameSpecificMessage(std::string_view val) = 0;
};
/**

View File

@@ -47,7 +47,7 @@ class NTFMSModel : public FMSModel {
void SetEnabled(bool val) override {}
void SetTest(bool val) override {}
void SetAutonomous(bool val) override {}
void SetGameSpecificMessage(const char* val) override {}
void SetGameSpecificMessage(std::string_view val) override {}
void Update() override;
bool Exists() override;

View File

@@ -103,13 +103,13 @@ void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {}
void HALSIM_SetJoystickType(int32_t stick, int32_t type) {}
void HALSIM_SetJoystickName(int32_t stick, const char* name) {}
void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size) {}
void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type) {}
void HALSIM_SetGameSpecificMessage(const char* message) {}
void HALSIM_SetGameSpecificMessage(const char* message, size_t size) {}
void HALSIM_SetEventName(const char* name) {}
void HALSIM_SetEventName(const char* name, size_t size) {}
void HALSIM_SetMatchType(HAL_MatchType type) {}

View File

@@ -732,7 +732,8 @@ JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickName
(JNIEnv* env, jclass, jint stick, jstring name)
{
HALSIM_SetJoystickName(stick, JStringRef{env, name}.c_str());
JStringRef nameJString{env, name};
HALSIM_SetJoystickName(stick, nameJString.c_str(), nameJString.size());
}
/*
@@ -756,7 +757,8 @@ JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setGameSpecificMessage
(JNIEnv* env, jclass, jstring message)
{
HALSIM_SetGameSpecificMessage(JStringRef{env, message}.c_str());
JStringRef messageJString{env, message};
HALSIM_SetGameSpecificMessage(messageJString.c_str(), messageJString.size());
}
/*
@@ -768,7 +770,8 @@ JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setEventName
(JNIEnv* env, jclass, jstring name)
{
HALSIM_SetEventName(JStringRef{env, name}.c_str());
JStringRef nameJString{env, name};
HALSIM_SetEventName(nameJString.c_str(), nameJString.size());
}
/*

View File

@@ -4,6 +4,8 @@
#pragma once
#include <cstddef>
#include "hal/DriverStationTypes.h"
#include "hal/Types.h"
#include "hal/simulation/NotifyListener.h"
@@ -145,11 +147,11 @@ void HALSIM_GetJoystickCounts(int32_t stick, int32_t* axisCount,
void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox);
void HALSIM_SetJoystickType(int32_t stick, int32_t type);
void HALSIM_SetJoystickName(int32_t stick, const char* name);
void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size);
void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type);
void HALSIM_SetGameSpecificMessage(const char* message);
void HALSIM_SetEventName(const char* name);
void HALSIM_SetGameSpecificMessage(const char* message, size_t size);
void HALSIM_SetEventName(const char* name, size_t size);
void HALSIM_SetMatchType(HAL_MatchType type);
void HALSIM_SetMatchNumber(int32_t matchNumber);
void HALSIM_SetReplayNumber(int32_t replayNumber);

View File

@@ -339,14 +339,17 @@ void DriverStationData::SetJoystickType(int32_t stick, int32_t type) {
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
void DriverStationData::SetJoystickName(int32_t stick, const char* name) {
void DriverStationData::SetJoystickName(int32_t stick, const char* name,
size_t size) {
if (stick < 0 || stick >= kNumJoysticks) {
return;
}
std::scoped_lock lock(m_joystickDataMutex);
std::strncpy(m_joystickData[stick].descriptor.name, name,
sizeof(m_joystickData[stick].descriptor.name) - 1);
*(std::end(m_joystickData[stick].descriptor.name) - 1) = '\0';
if (size > sizeof(m_joystickData[stick].descriptor.name) - 1) {
size = sizeof(m_joystickData[stick].descriptor.name) - 1;
}
std::strncpy(m_joystickData[stick].descriptor.name, name, size);
m_joystickData[stick].descriptor.name[size] = '\0';
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
@@ -363,19 +366,27 @@ void DriverStationData::SetJoystickAxisType(int32_t stick, int32_t axis,
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
void DriverStationData::SetGameSpecificMessage(const char* message) {
void DriverStationData::SetGameSpecificMessage(const char* message,
size_t size) {
std::scoped_lock lock(m_matchInfoMutex);
if (size > sizeof(m_matchInfo.gameSpecificMessage) - 1) {
size = sizeof(m_matchInfo.gameSpecificMessage) - 1;
}
std::strncpy(reinterpret_cast<char*>(m_matchInfo.gameSpecificMessage),
message, sizeof(m_matchInfo.gameSpecificMessage) - 1);
*(std::end(m_matchInfo.gameSpecificMessage) - 1) = '\0';
m_matchInfo.gameSpecificMessageSize = std::strlen(message);
message, size);
m_matchInfo.gameSpecificMessage[size] = '\0';
m_matchInfo.gameSpecificMessageSize =
std::strlen(reinterpret_cast<char*>(m_matchInfo.gameSpecificMessage));
m_matchInfoCallbacks(&m_matchInfo);
}
void DriverStationData::SetEventName(const char* name) {
void DriverStationData::SetEventName(const char* name, size_t size) {
std::scoped_lock lock(m_matchInfoMutex);
std::strncpy(m_matchInfo.eventName, name, sizeof(m_matchInfo.eventName) - 1);
*(std::end(m_matchInfo.eventName) - 1) = '\0';
if (size > sizeof(m_matchInfo.eventName) - 1) {
size = sizeof(m_matchInfo.eventName) - 1;
}
std::strncpy(m_matchInfo.eventName, name, size);
m_matchInfo.eventName[size] = '\0';
m_matchInfoCallbacks(&m_matchInfo);
}
@@ -540,20 +551,20 @@ void HALSIM_SetJoystickType(int32_t stick, int32_t type) {
SimDriverStationData->SetJoystickType(stick, type);
}
void HALSIM_SetJoystickName(int32_t stick, const char* name) {
SimDriverStationData->SetJoystickName(stick, name);
void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size) {
SimDriverStationData->SetJoystickName(stick, name, size);
}
void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type) {
SimDriverStationData->SetJoystickAxisType(stick, axis, type);
}
void HALSIM_SetGameSpecificMessage(const char* message) {
SimDriverStationData->SetGameSpecificMessage(message);
void HALSIM_SetGameSpecificMessage(const char* message, size_t size) {
SimDriverStationData->SetGameSpecificMessage(message, size);
}
void HALSIM_SetEventName(const char* name) {
SimDriverStationData->SetEventName(name);
void HALSIM_SetEventName(const char* name, size_t size) {
SimDriverStationData->SetEventName(name, size);
}
void HALSIM_SetMatchType(HAL_MatchType type) {

View File

@@ -107,11 +107,11 @@ class DriverStationData {
void SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox);
void SetJoystickType(int32_t stick, int32_t type);
void SetJoystickName(int32_t stick, const char* name);
void SetJoystickName(int32_t stick, const char* name, size_t size);
void SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type);
void SetGameSpecificMessage(const char* message);
void SetEventName(const char* name);
void SetGameSpecificMessage(const char* message, size_t size);
void SetEventName(const char* name, size_t size);
void SetMatchType(HAL_MatchType type);
void SetMatchNumber(int32_t matchNumber);
void SetReplayNumber(int32_t replayNumber);

View File

@@ -254,8 +254,8 @@ class FMSSimModel : public glass::FMSModel {
void SetAutonomous(bool val) override {
HALSIM_SetDriverStationAutonomous(val);
}
void SetGameSpecificMessage(const char* val) override {
HALSIM_SetGameSpecificMessage(val);
void SetGameSpecificMessage(std::string_view val) override {
HALSIM_SetGameSpecificMessage(val.data(), val.size());
}
void Update() override;

View File

@@ -166,8 +166,8 @@ void HALSimWSProviderDriverStation::OnNetValueChanged(const wpi::json& json) {
HALSIM_SetDriverStationMatchTime(it.value());
}
if ((it = json.find(">game_data")) != json.end()) {
HALSIM_SetGameSpecificMessage(
it.value().get_ref<const std::string&>().c_str());
std::string message = it.value().get_ref<const std::string&>();
HALSIM_SetGameSpecificMessage(message.c_str(), message.length());
}
// Only notify usercode if we get the new data message

View File

@@ -234,20 +234,20 @@ void DriverStationSim::SetJoystickType(int stick, int type) {
HALSIM_SetJoystickType(stick, type);
}
void DriverStationSim::SetJoystickName(int stick, const char* name) {
HALSIM_SetJoystickName(stick, name);
void DriverStationSim::SetJoystickName(int stick, std::string_view name) {
HALSIM_SetJoystickName(stick, name.data(), name.size());
}
void DriverStationSim::SetJoystickAxisType(int stick, int axis, int type) {
HALSIM_SetJoystickAxisType(stick, axis, type);
}
void DriverStationSim::SetGameSpecificMessage(const char* message) {
HALSIM_SetGameSpecificMessage(message);
void DriverStationSim::SetGameSpecificMessage(std::string_view message) {
HALSIM_SetGameSpecificMessage(message.data(), message.size());
}
void DriverStationSim::SetEventName(const char* name) {
HALSIM_SetEventName(name);
void DriverStationSim::SetEventName(std::string_view name) {
HALSIM_SetEventName(name.data(), name.size());
}
void DriverStationSim::SetMatchType(DriverStation::MatchType type) {

View File

@@ -337,7 +337,7 @@ class DriverStationSim {
* @param stick The joystick number
* @param name The value of name
*/
static void SetJoystickName(int stick, const char* name);
static void SetJoystickName(int stick, std::string_view name);
/**
* Sets the types of Axes for a joystick.
@@ -353,14 +353,14 @@ class DriverStationSim {
*
* @param message the game specific message
*/
static void SetGameSpecificMessage(const char* message);
static void SetGameSpecificMessage(std::string_view message);
/**
* Sets the event name.
*
* @param name the event name
*/
static void SetEventName(const char* name);
static void SetEventName(std::string_view name);
/**
* Sets the match type.