mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[hal] Resource classes: Use expected, refactor errors (#8768)
Also revamp SetLastError et al; Instead of taking status by pointer, take by value and return new status instead. Rename from SetLast to Make to make this new usage obvious. Also move declarations for the error functions from duplicated in the per-target HALInternal.hpp to a common ErrorHandling.hpp.
This commit is contained in:
@@ -10,9 +10,9 @@
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "HALInitializer.hpp"
|
||||
#include "HALInternal.hpp"
|
||||
#include "PortsInternal.hpp"
|
||||
#include "SmartIo.hpp"
|
||||
#include "wpi/hal/ErrorHandling.hpp"
|
||||
#include "wpi/hal/monotonic_clock.hpp"
|
||||
|
||||
using namespace wpi::hal;
|
||||
@@ -28,28 +28,21 @@ HAL_CounterHandle HAL_InitializeCounter(int channel, HAL_Bool risingEdge,
|
||||
int32_t* status) {
|
||||
wpi::hal::init::CheckInit();
|
||||
if (channel == INVALID_HANDLE_INDEX || channel >= kNumSmartIo) {
|
||||
*status = RESOURCE_OUT_OF_RANGE;
|
||||
wpi::hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Counter",
|
||||
0, kNumSmartIo, channel);
|
||||
*status = MakeErrorIndexOutOfRange(RESOURCE_OUT_OF_RANGE,
|
||||
"Invalid Index for Counter", 0,
|
||||
kNumSmartIo, channel);
|
||||
return HAL_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
HAL_CounterHandle handle;
|
||||
auto resource =
|
||||
smartIoHandles->Allocate(channel, HAL_HandleEnum::COUNTER, "Counter");
|
||||
|
||||
auto port = smartIoHandles->Allocate(channel, HAL_HandleEnum::COUNTER,
|
||||
&handle, status);
|
||||
|
||||
if (*status != 0) {
|
||||
if (port) {
|
||||
wpi::hal::SetLastErrorPreviouslyAllocated(status, "SmartIo", channel,
|
||||
port->previousAllocation);
|
||||
} else {
|
||||
wpi::hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Counter",
|
||||
0, kNumSmartIo, channel);
|
||||
}
|
||||
if (!resource) {
|
||||
*status = resource.error();
|
||||
return HAL_INVALID_HANDLE; // failed to allocate. Pass error back.
|
||||
}
|
||||
|
||||
auto [handle, port] = *resource;
|
||||
port->channel = channel;
|
||||
|
||||
*status =
|
||||
|
||||
Reference in New Issue
Block a user