mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
Add braces to C++ single-line loops and conditionals (NFC) (#2973)
This makes code easier to read and more consistent between C++ and Java. Also update clang-format settings to always add a line break (even if no braces are used).
This commit is contained in:
@@ -41,7 +41,9 @@ void AddressableLEDData::SetData(const HAL_AddressableLEDData* d, int32_t len) {
|
||||
int32_t AddressableLEDData::GetData(HAL_AddressableLEDData* d) {
|
||||
std::scoped_lock lock(m_dataMutex);
|
||||
int32_t len = length;
|
||||
if (d) std::memcpy(d, m_data, len * sizeof(d[0]));
|
||||
if (d) {
|
||||
std::memcpy(d, m_data, len * sizeof(d[0]));
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -50,8 +52,9 @@ extern "C" {
|
||||
int32_t HALSIM_FindAddressableLEDForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumAddressableLEDs; ++i) {
|
||||
if (SimAddressableLEDData[i].initialized &&
|
||||
SimAddressableLEDData[i].outputPort == channel)
|
||||
SimAddressableLEDData[i].outputPort == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -29,8 +29,9 @@ extern "C" {
|
||||
int32_t HALSIM_FindAnalogTriggerForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumAnalogTriggers; ++i) {
|
||||
if (SimAnalogTriggerData[i].initialized &&
|
||||
SimAnalogTriggerData[i].inputPort == channel)
|
||||
SimAnalogTriggerData[i].inputPort == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,9 @@ void CanData::ResetData() {
|
||||
|
||||
extern "C" {
|
||||
|
||||
void HALSIM_ResetCanData(void) { SimCanData->ResetData(); }
|
||||
void HALSIM_ResetCanData(void) {
|
||||
SimCanData->ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMCALLBACKREGISTRY_DEFINE_CAPI_NOINDEX(TYPE, HALSIM, Can##CAPINAME, \
|
||||
|
||||
@@ -27,7 +27,9 @@ void DIOData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetDIOData(int32_t index) { SimDIOData[index].ResetData(); }
|
||||
void HALSIM_ResetDIOData(int32_t index) {
|
||||
SimDIOData[index].ResetData();
|
||||
}
|
||||
|
||||
HAL_SimDeviceHandle HALSIM_GetDIOSimDevice(int32_t index) {
|
||||
return SimDIOData[index].simDevice;
|
||||
|
||||
@@ -26,8 +26,10 @@ void DigitalPWMData::ResetData() {
|
||||
extern "C" {
|
||||
int32_t HALSIM_FindDigitalPWMForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumDigitalPWMOutputs; ++i) {
|
||||
if (SimDigitalPWMData[i].initialized && SimDigitalPWMData[i].pin == channel)
|
||||
if (SimDigitalPWMData[i].initialized &&
|
||||
SimDigitalPWMData[i].pin == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,9 @@ void InitializeDriverStationData() {
|
||||
|
||||
DriverStationData* hal::SimDriverStationData;
|
||||
|
||||
DriverStationData::DriverStationData() { ResetData(); }
|
||||
DriverStationData::DriverStationData() {
|
||||
ResetData();
|
||||
}
|
||||
|
||||
void DriverStationData::ResetData() {
|
||||
enabled.Reset(false);
|
||||
@@ -60,7 +62,8 @@ void DriverStationData::ResetData() {
|
||||
int32_t DriverStationData::RegisterJoystick##name##Callback( \
|
||||
int32_t joystickNum, HAL_Joystick##name##Callback callback, void* param, \
|
||||
HAL_Bool initialNotify) { \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return 0; \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) \
|
||||
return 0; \
|
||||
std::scoped_lock lock(m_joystickDataMutex); \
|
||||
int32_t uid = m_joystick##name##Callbacks.Register(callback, param); \
|
||||
if (initialNotify) { \
|
||||
@@ -79,14 +82,16 @@ void DriverStationData::ResetData() {
|
||||
\
|
||||
void DriverStationData::GetJoystick##name(int32_t joystickNum, \
|
||||
HAL_Joystick##name* d) { \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return; \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) \
|
||||
return; \
|
||||
std::scoped_lock lock(m_joystickDataMutex); \
|
||||
*d = m_joystickData[joystickNum].data##data2; \
|
||||
} \
|
||||
\
|
||||
void DriverStationData::SetJoystick##name(int32_t joystickNum, \
|
||||
const HAL_Joystick##name* d) { \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return; \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) \
|
||||
return; \
|
||||
std::scoped_lock lock(m_joystickDataMutex); \
|
||||
m_joystickData[joystickNum].data##data2 = *d; \
|
||||
m_joystick##name##Callbacks(joystickNum, d); \
|
||||
@@ -100,14 +105,18 @@ DEFINE_CPPAPI_CALLBACKS(Descriptor, descriptor, )
|
||||
|
||||
void DriverStationData::GetJoystickDescriptor(
|
||||
int32_t joystickNum, HAL_JoystickDescriptor* descriptor) {
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
*descriptor = m_joystickData[joystickNum].descriptor;
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickDescriptor(
|
||||
int32_t joystickNum, const HAL_JoystickDescriptor* descriptor) {
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[joystickNum].descriptor = *descriptor;
|
||||
// Always ensure name is null terminated
|
||||
@@ -118,8 +127,9 @@ void DriverStationData::SetJoystickDescriptor(
|
||||
int32_t DriverStationData::RegisterJoystickOutputsCallback(
|
||||
int32_t joystickNum, HAL_JoystickOutputsCallback callback, void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
if (joystickNum < 0 || joystickNum >= DriverStationData::kNumJoysticks)
|
||||
if (joystickNum < 0 || joystickNum >= DriverStationData::kNumJoysticks) {
|
||||
return 0;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
int32_t uid = m_joystickOutputsCallbacks.Register(callback, param);
|
||||
if (initialNotify) {
|
||||
@@ -138,7 +148,9 @@ void DriverStationData::GetJoystickOutputs(int32_t joystickNum,
|
||||
int64_t* outputs,
|
||||
int32_t* leftRumble,
|
||||
int32_t* rightRumble) {
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
*leftRumble = m_joystickData[joystickNum].outputs.leftRumble;
|
||||
*outputs = m_joystickData[joystickNum].outputs.outputs;
|
||||
@@ -148,7 +160,9 @@ void DriverStationData::GetJoystickOutputs(int32_t joystickNum,
|
||||
void DriverStationData::SetJoystickOutputs(int32_t joystickNum, int64_t outputs,
|
||||
int32_t leftRumble,
|
||||
int32_t rightRumble) {
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[joystickNum].outputs.leftRumble = leftRumble;
|
||||
m_joystickData[joystickNum].outputs.outputs = outputs;
|
||||
@@ -204,23 +218,32 @@ void DriverStationData::CallNewDataCallbacks() {
|
||||
m_newDataCallbacks(&empty);
|
||||
}
|
||||
|
||||
void DriverStationData::NotifyNewData() { HAL_ReleaseDSMutex(); }
|
||||
void DriverStationData::NotifyNewData() {
|
||||
HAL_ReleaseDSMutex();
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickButton(int32_t stick, int32_t button,
|
||||
HAL_Bool state) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
if (state)
|
||||
if (state) {
|
||||
m_joystickData[stick].buttons.buttons |= 1 << (button - 1);
|
||||
else
|
||||
} else {
|
||||
m_joystickData[stick].buttons.buttons &= ~(1 << (button - 1));
|
||||
}
|
||||
m_joystickButtonsCallbacks(stick, &m_joystickData[stick].buttons);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickAxis(int32_t stick, int32_t axis,
|
||||
double value) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].axes.axes[axis] = value;
|
||||
m_joystickAxesCallbacks(stick, &m_joystickData[stick].axes);
|
||||
@@ -228,22 +251,30 @@ void DriverStationData::SetJoystickAxis(int32_t stick, int32_t axis,
|
||||
|
||||
void DriverStationData::SetJoystickPOV(int32_t stick, int32_t pov,
|
||||
int32_t value) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (pov < 0 || pov >= HAL_kMaxJoystickPOVs) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
if (pov < 0 || pov >= HAL_kMaxJoystickPOVs) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].povs.povs[pov] = value;
|
||||
m_joystickPOVsCallbacks(stick, &m_joystickData[stick].povs);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickButtons(int32_t stick, uint32_t buttons) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].buttons.buttons = buttons;
|
||||
m_joystickButtonsCallbacks(stick, &m_joystickData[stick].buttons);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickAxisCount(int32_t stick, int32_t count) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].axes.count = count;
|
||||
m_joystickData[stick].descriptor.axisCount = count;
|
||||
@@ -252,7 +283,9 @@ void DriverStationData::SetJoystickAxisCount(int32_t stick, int32_t count) {
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickPOVCount(int32_t stick, int32_t count) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].povs.count = count;
|
||||
m_joystickData[stick].descriptor.povCount = count;
|
||||
@@ -261,7 +294,9 @@ void DriverStationData::SetJoystickPOVCount(int32_t stick, int32_t count) {
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickButtonCount(int32_t stick, int32_t count) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].buttons.count = count;
|
||||
m_joystickData[stick].descriptor.buttonCount = count;
|
||||
@@ -285,21 +320,27 @@ void DriverStationData::GetJoystickCounts(int32_t stick, int32_t* axisCount,
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].descriptor.isXbox = isXbox;
|
||||
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickType(int32_t stick, int32_t type) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].descriptor.type = type;
|
||||
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickName(int32_t stick, const char* name) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
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);
|
||||
@@ -309,8 +350,12 @@ void DriverStationData::SetJoystickName(int32_t stick, const char* name) {
|
||||
|
||||
void DriverStationData::SetJoystickAxisType(int32_t stick, int32_t axis,
|
||||
int32_t type) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].descriptor.axisTypes[axis] = type;
|
||||
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
|
||||
@@ -351,7 +396,9 @@ void DriverStationData::SetReplayNumber(int32_t replayNumber) {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetDriverStationData(void) { SimDriverStationData->ResetData(); }
|
||||
void HALSIM_ResetDriverStationData(void) {
|
||||
SimDriverStationData->ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI_NOINDEX(TYPE, HALSIM, DriverStation##CAPINAME, \
|
||||
|
||||
@@ -30,8 +30,9 @@ extern "C" {
|
||||
int32_t HALSIM_FindDutyCycleForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumDutyCycles; ++i) {
|
||||
if (SimDutyCycleData[i].initialized &&
|
||||
SimDutyCycleData[i].digitalChannel == channel)
|
||||
SimDutyCycleData[i].digitalChannel == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -35,10 +35,13 @@ void EncoderData::ResetData() {
|
||||
extern "C" {
|
||||
int32_t HALSIM_FindEncoderForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumEncoders; ++i) {
|
||||
if (!SimEncoderData[i].initialized) continue;
|
||||
if (!SimEncoderData[i].initialized) {
|
||||
continue;
|
||||
}
|
||||
if (SimEncoderData[i].digitalChannelA == channel ||
|
||||
SimEncoderData[i].digitalChannelB == channel)
|
||||
SimEncoderData[i].digitalChannelB == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -34,7 +34,9 @@ void I2CData::Read(int32_t deviceAddress, uint8_t* buffer, int32_t count) {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetI2CData(int32_t index) { SimI2CData[index].ResetData(); }
|
||||
void HALSIM_ResetI2CData(int32_t index) {
|
||||
SimI2CData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, I2C##CAPINAME, SimI2CData, \
|
||||
|
||||
@@ -31,7 +31,9 @@ void PCMData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetPCMData(int32_t index) { SimPCMData[index].ResetData(); }
|
||||
void HALSIM_ResetPCMData(int32_t index) {
|
||||
SimPCMData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PCM##CAPINAME, SimPCMData, \
|
||||
|
||||
@@ -27,7 +27,9 @@ void PDPData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetPDPData(int32_t index) { SimPDPData[index].ResetData(); }
|
||||
void HALSIM_ResetPDPData(int32_t index) {
|
||||
SimPDPData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PDP##CAPINAME, SimPDPData, \
|
||||
|
||||
@@ -27,7 +27,9 @@ void PWMData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetPWMData(int32_t index) { SimPWMData[index].ResetData(); }
|
||||
void HALSIM_ResetPWMData(int32_t index) {
|
||||
SimPWMData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PWM##CAPINAME, SimPWMData, \
|
||||
|
||||
@@ -25,7 +25,9 @@ void RelayData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetRelayData(int32_t index) { SimRelayData[index].ResetData(); }
|
||||
void HALSIM_ResetRelayData(int32_t index) {
|
||||
SimRelayData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, Relay##CAPINAME, SimRelayData, \
|
||||
|
||||
@@ -36,7 +36,9 @@ void RoboRioData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetRoboRioData(void) { SimRoboRioData->ResetData(); }
|
||||
void HALSIM_ResetRoboRioData(void) {
|
||||
SimRoboRioData->ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI_NOINDEX(TYPE, HALSIM, RoboRio##CAPINAME, \
|
||||
|
||||
@@ -49,7 +49,9 @@ int32_t SPIData::ReadAutoReceivedData(uint32_t* buffer, int32_t numToRead,
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetSPIData(int32_t index) { SimSPIData[index].ResetData(); }
|
||||
void HALSIM_ResetSPIData(int32_t index) {
|
||||
SimSPIData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, SPI##CAPINAME, SimSPIData, \
|
||||
|
||||
@@ -22,26 +22,34 @@ void InitializeSimDeviceData() {
|
||||
SimDeviceData* hal::SimSimDeviceData;
|
||||
|
||||
SimDeviceData::Device* SimDeviceData::LookupDevice(HAL_SimDeviceHandle handle) {
|
||||
if (handle <= 0) return nullptr;
|
||||
--handle;
|
||||
if (static_cast<uint32_t>(handle) >= m_devices.size() || !m_devices[handle])
|
||||
if (handle <= 0) {
|
||||
return nullptr;
|
||||
}
|
||||
--handle;
|
||||
if (static_cast<uint32_t>(handle) >= m_devices.size() || !m_devices[handle]) {
|
||||
return nullptr;
|
||||
}
|
||||
return m_devices[handle].get();
|
||||
}
|
||||
|
||||
SimDeviceData::Value* SimDeviceData::LookupValue(HAL_SimValueHandle handle) {
|
||||
if (handle <= 0) return nullptr;
|
||||
if (handle <= 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// look up device
|
||||
Device* deviceImpl = LookupDevice(handle >> 16);
|
||||
if (!deviceImpl) return nullptr;
|
||||
if (!deviceImpl) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// look up value
|
||||
handle &= 0xffff;
|
||||
--handle;
|
||||
if (static_cast<uint32_t>(handle) >= deviceImpl->values.size() ||
|
||||
!deviceImpl->values[handle])
|
||||
!deviceImpl->values[handle]) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return deviceImpl->values[handle].get();
|
||||
}
|
||||
@@ -65,7 +73,9 @@ void SimDeviceData::SetDeviceEnabled(const char* prefix, bool enabled) {
|
||||
bool SimDeviceData::IsDeviceEnabled(const char* name) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
for (const auto& elem : m_prefixEnabled) {
|
||||
if (wpi::StringRef{name}.startswith(elem.first)) return elem.second;
|
||||
if (wpi::StringRef{name}.startswith(elem.first)) {
|
||||
return elem.second;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -76,17 +86,23 @@ HAL_SimDeviceHandle SimDeviceData::CreateDevice(const char* name) {
|
||||
// don't create if disabled
|
||||
for (const auto& elem : m_prefixEnabled) {
|
||||
if (wpi::StringRef{name}.startswith(elem.first)) {
|
||||
if (elem.second) break; // enabled
|
||||
return 0; // disabled
|
||||
if (elem.second) {
|
||||
break; // enabled
|
||||
}
|
||||
return 0; // disabled
|
||||
}
|
||||
}
|
||||
|
||||
// check for duplicates and don't overwrite them
|
||||
if (m_deviceMap.count(name) > 0) return 0;
|
||||
if (m_deviceMap.count(name) > 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// don't allow more than 4096 devices (limit driven by 12-bit allocation in
|
||||
// value changed callback uid)
|
||||
if (m_devices.size() >= 4095) return 0;
|
||||
if (m_devices.size() >= 4095) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// create and save
|
||||
auto deviceImpl = std::make_shared<Device>(name);
|
||||
@@ -105,9 +121,13 @@ void SimDeviceData::FreeDevice(HAL_SimDeviceHandle handle) {
|
||||
--handle;
|
||||
|
||||
// see if it exists
|
||||
if (handle < 0 || static_cast<uint32_t>(handle) >= m_devices.size()) return;
|
||||
if (handle < 0 || static_cast<uint32_t>(handle) >= m_devices.size()) {
|
||||
return;
|
||||
}
|
||||
auto deviceImpl = std::move(m_devices[handle]);
|
||||
if (!deviceImpl) return;
|
||||
if (!deviceImpl) {
|
||||
return;
|
||||
}
|
||||
|
||||
// remove from map
|
||||
m_deviceMap.erase(deviceImpl->name);
|
||||
@@ -127,15 +147,21 @@ HAL_SimValueHandle SimDeviceData::CreateValue(
|
||||
|
||||
// look up device
|
||||
Device* deviceImpl = LookupDevice(device);
|
||||
if (!deviceImpl) return 0;
|
||||
if (!deviceImpl) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// check for duplicates and don't overwrite them
|
||||
auto it = deviceImpl->valueMap.find(name);
|
||||
if (it != deviceImpl->valueMap.end()) return 0;
|
||||
if (it != deviceImpl->valueMap.end()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// don't allow more than 4096 values per device (limit driven by 12-bit
|
||||
// allocation in value changed callback uid)
|
||||
if (deviceImpl->values.size() >= 4095) return 0;
|
||||
if (deviceImpl->values.size() >= 4095) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// create and save; encode device into handle
|
||||
auto valueImplPtr = std::make_unique<Value>(name, direction, initialValue);
|
||||
@@ -185,7 +211,9 @@ void SimDeviceData::SetValue(HAL_SimValueHandle handle,
|
||||
const HAL_Value& value) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(handle);
|
||||
if (!valueImpl) return;
|
||||
if (!valueImpl) {
|
||||
return;
|
||||
}
|
||||
|
||||
valueImpl->value = value;
|
||||
|
||||
@@ -205,8 +233,9 @@ int32_t SimDeviceData::RegisterDeviceCreatedCallback(
|
||||
// initial notifications
|
||||
if (initialNotify) {
|
||||
for (auto&& device : m_devices) {
|
||||
if (wpi::StringRef{device->name}.startswith(prefix))
|
||||
if (wpi::StringRef{device->name}.startswith(prefix)) {
|
||||
callback(device->name.c_str(), param, device->handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,7 +243,9 @@ int32_t SimDeviceData::RegisterDeviceCreatedCallback(
|
||||
}
|
||||
|
||||
void SimDeviceData::CancelDeviceCreatedCallback(int32_t uid) {
|
||||
if (uid <= 0) return;
|
||||
if (uid <= 0) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_deviceCreated.Cancel(uid);
|
||||
}
|
||||
@@ -226,7 +257,9 @@ int32_t SimDeviceData::RegisterDeviceFreedCallback(
|
||||
}
|
||||
|
||||
void SimDeviceData::CancelDeviceFreedCallback(int32_t uid) {
|
||||
if (uid <= 0) return;
|
||||
if (uid <= 0) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_deviceFreed.Cancel(uid);
|
||||
}
|
||||
@@ -234,11 +267,14 @@ void SimDeviceData::CancelDeviceFreedCallback(int32_t uid) {
|
||||
HAL_SimDeviceHandle SimDeviceData::GetDeviceHandle(const char* name) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
auto it = m_deviceMap.find(name);
|
||||
if (it == m_deviceMap.end()) return 0;
|
||||
if (auto deviceImpl = it->getValue().lock())
|
||||
return deviceImpl->handle;
|
||||
else
|
||||
if (it == m_deviceMap.end()) {
|
||||
return 0;
|
||||
}
|
||||
if (auto deviceImpl = it->getValue().lock()) {
|
||||
return deviceImpl->handle;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
const char* SimDeviceData::GetDeviceName(HAL_SimDeviceHandle handle) {
|
||||
@@ -246,7 +282,9 @@ const char* SimDeviceData::GetDeviceName(HAL_SimDeviceHandle handle) {
|
||||
|
||||
// look up device
|
||||
Device* deviceImpl = LookupDevice(handle);
|
||||
if (!deviceImpl) return nullptr;
|
||||
if (!deviceImpl) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return deviceImpl->name.c_str();
|
||||
}
|
||||
@@ -255,8 +293,9 @@ void SimDeviceData::EnumerateDevices(const char* prefix, void* param,
|
||||
HALSIM_SimDeviceCallback callback) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
for (auto&& device : m_devices) {
|
||||
if (wpi::StringRef{device->name}.startswith(prefix))
|
||||
if (wpi::StringRef{device->name}.startswith(prefix)) {
|
||||
callback(device->name.c_str(), param, device->handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,16 +304,19 @@ int32_t SimDeviceData::RegisterValueCreatedCallback(
|
||||
bool initialNotify) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Device* deviceImpl = LookupDevice(device);
|
||||
if (!deviceImpl) return -1;
|
||||
if (!deviceImpl) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// register callback
|
||||
int32_t index = deviceImpl->valueCreated.Register(callback, param);
|
||||
|
||||
// initial notifications
|
||||
if (initialNotify) {
|
||||
for (auto&& value : deviceImpl->values)
|
||||
for (auto&& value : deviceImpl->values) {
|
||||
callback(value->name.c_str(), param, value->handle, value->direction,
|
||||
&value->value);
|
||||
}
|
||||
}
|
||||
|
||||
// encode device into uid
|
||||
@@ -282,10 +324,14 @@ int32_t SimDeviceData::RegisterValueCreatedCallback(
|
||||
}
|
||||
|
||||
void SimDeviceData::CancelValueCreatedCallback(int32_t uid) {
|
||||
if (uid <= 0) return;
|
||||
if (uid <= 0) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Device* deviceImpl = LookupDevice(uid >> 16);
|
||||
if (!deviceImpl) return;
|
||||
if (!deviceImpl) {
|
||||
return;
|
||||
}
|
||||
deviceImpl->valueCreated.Cancel(uid & 0xffff);
|
||||
}
|
||||
|
||||
@@ -294,15 +340,18 @@ int32_t SimDeviceData::RegisterValueChangedCallback(
|
||||
bool initialNotify) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(handle);
|
||||
if (!valueImpl) return -1;
|
||||
if (!valueImpl) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// register callback
|
||||
int32_t index = valueImpl->changed.Register(callback, param);
|
||||
|
||||
// initial notification
|
||||
if (initialNotify)
|
||||
if (initialNotify) {
|
||||
callback(valueImpl->name.c_str(), param, valueImpl->handle,
|
||||
valueImpl->direction, &valueImpl->value);
|
||||
}
|
||||
|
||||
// encode device and value into uid
|
||||
return (((handle >> 16) & 0xfff) << 19) | ((handle & 0xfff) << 7) |
|
||||
@@ -310,10 +359,14 @@ int32_t SimDeviceData::RegisterValueChangedCallback(
|
||||
}
|
||||
|
||||
void SimDeviceData::CancelValueChangedCallback(int32_t uid) {
|
||||
if (uid <= 0) return;
|
||||
if (uid <= 0) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(((uid >> 19) << 16) | ((uid >> 7) & 0xfff));
|
||||
if (!valueImpl) return;
|
||||
if (!valueImpl) {
|
||||
return;
|
||||
}
|
||||
valueImpl->changed.Cancel(uid & 0x7f);
|
||||
}
|
||||
|
||||
@@ -321,12 +374,18 @@ HAL_SimValueHandle SimDeviceData::GetValueHandle(HAL_SimDeviceHandle device,
|
||||
const char* name) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Device* deviceImpl = LookupDevice(device);
|
||||
if (!deviceImpl) return 0;
|
||||
if (!deviceImpl) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// lookup value
|
||||
auto it = deviceImpl->valueMap.find(name);
|
||||
if (it == deviceImpl->valueMap.end()) return 0;
|
||||
if (!it->getValue()) return 0;
|
||||
if (it == deviceImpl->valueMap.end()) {
|
||||
return 0;
|
||||
}
|
||||
if (!it->getValue()) {
|
||||
return 0;
|
||||
}
|
||||
return it->getValue()->handle;
|
||||
}
|
||||
|
||||
@@ -334,11 +393,14 @@ void SimDeviceData::EnumerateValues(HAL_SimDeviceHandle device, void* param,
|
||||
HALSIM_SimValueCallback callback) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Device* deviceImpl = LookupDevice(device);
|
||||
if (!deviceImpl) return;
|
||||
if (!deviceImpl) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto&& value : deviceImpl->values)
|
||||
for (auto&& value : deviceImpl->values) {
|
||||
callback(value->name.c_str(), param, value->handle, value->direction,
|
||||
&value->value);
|
||||
}
|
||||
}
|
||||
|
||||
const char** SimDeviceData::GetValueEnumOptions(HAL_SimValueHandle handle,
|
||||
@@ -347,7 +409,9 @@ const char** SimDeviceData::GetValueEnumOptions(HAL_SimValueHandle handle,
|
||||
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(handle);
|
||||
if (!valueImpl) return nullptr;
|
||||
if (!valueImpl) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// get list of options (safe to return as they never change)
|
||||
auto& options = valueImpl->cstrEnumOptions;
|
||||
@@ -361,7 +425,9 @@ const double* SimDeviceData::GetValueEnumDoubleValues(HAL_SimValueHandle handle,
|
||||
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(handle);
|
||||
if (!valueImpl) return nullptr;
|
||||
if (!valueImpl) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// get list of option values (safe to return as they never change)
|
||||
auto& optionValues = valueImpl->enumOptionValues;
|
||||
@@ -418,7 +484,9 @@ const char* HALSIM_GetSimDeviceName(HAL_SimDeviceHandle handle) {
|
||||
}
|
||||
|
||||
HAL_SimDeviceHandle HALSIM_GetSimValueDeviceHandle(HAL_SimValueHandle handle) {
|
||||
if (handle <= 0) return 0;
|
||||
if (handle <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return handle >> 16;
|
||||
}
|
||||
|
||||
@@ -471,6 +539,8 @@ const double* HALSIM_GetSimValueEnumDoubleValues(HAL_SimValueHandle handle,
|
||||
return SimSimDeviceData->GetValueEnumDoubleValues(handle, numOptions);
|
||||
}
|
||||
|
||||
void HALSIM_ResetSimDeviceData(void) { SimSimDeviceData->ResetData(); }
|
||||
void HALSIM_ResetSimDeviceData(void) {
|
||||
SimSimDeviceData->ResetData();
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -33,17 +33,25 @@ class SimUnnamedCallbackRegistry {
|
||||
|
||||
public:
|
||||
void Cancel(int32_t uid) {
|
||||
if (m_callbacks) m_callbacks->erase(uid - 1);
|
||||
if (m_callbacks) {
|
||||
m_callbacks->erase(uid - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
if (m_callbacks) m_callbacks->clear();
|
||||
if (m_callbacks) {
|
||||
m_callbacks->clear();
|
||||
}
|
||||
}
|
||||
|
||||
int32_t Register(CallbackFunction callback, void* param) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
if (!m_callbacks) m_callbacks = std::make_unique<CallbackVector>();
|
||||
if (callback == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
if (!m_callbacks) {
|
||||
m_callbacks = std::make_unique<CallbackVector>();
|
||||
}
|
||||
return m_callbacks->emplace_back(param,
|
||||
reinterpret_cast<RawFunctor>(callback)) +
|
||||
1;
|
||||
@@ -84,17 +92,25 @@ class SimPrefixCallbackRegistry {
|
||||
|
||||
public:
|
||||
void Cancel(int32_t uid) {
|
||||
if (m_callbacks) m_callbacks->erase(uid - 1);
|
||||
if (m_callbacks) {
|
||||
m_callbacks->erase(uid - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
if (m_callbacks) m_callbacks->clear();
|
||||
if (m_callbacks) {
|
||||
m_callbacks->clear();
|
||||
}
|
||||
}
|
||||
|
||||
int32_t Register(const char* prefix, void* param, CallbackFunction callback) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
if (!m_callbacks) m_callbacks = std::make_unique<CallbackVector>();
|
||||
if (callback == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
if (!m_callbacks) {
|
||||
m_callbacks = std::make_unique<CallbackVector>();
|
||||
}
|
||||
return m_callbacks->emplace_back(prefix, param, callback) + 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user