[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:
Peter Johnson
2026-04-17 20:19:38 -07:00
committed by GitHub
parent 6cb6903780
commit a97214df43
31 changed files with 309 additions and 406 deletions

View File

@@ -2,6 +2,8 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "wpi/hal/ErrorHandling.hpp"
#include <fmt/format.h>
#include "wpi/hal/Errors.h"
@@ -21,35 +23,36 @@ static LastErrorStorage& GetThreadLastError() {
}
namespace wpi::hal {
void SetLastError(int32_t* status, std::string_view value) {
HAL_Status MakeError(HAL_Status status, std::string_view value) {
LastErrorStorage& lastError = GetThreadLastError();
lastError.message = value;
lastError.status = *status;
*status = HAL_USE_LAST_ERROR;
lastError.status = status;
return HAL_USE_LAST_ERROR;
}
void SetLastErrorIndexOutOfRange(int32_t* status, std::string_view message,
int32_t minimum, int32_t maximum,
int32_t requested) {
SetLastError(
HAL_Status MakeErrorIndexOutOfRange(HAL_Status status, std::string_view message,
int32_t minimum, int32_t maximum,
int32_t requested) {
return MakeError(
status,
fmt::format("{}\n Status: {}\n Minimum: {} Maximum: {} Requested: {}",
message, *status, minimum, maximum, requested));
message, status, minimum, maximum, requested));
}
void SetLastErrorPreviouslyAllocated(int32_t* status, std::string_view message,
int32_t channel,
std::string_view previousAllocation) {
wpi::hal::SetLastError(
status, fmt::format("{} {} previously allocated.\n"
"Location of the previous allocation:\n{}\n"
"Location of the current allocation:",
message, channel, previousAllocation));
HAL_Status MakeErrorPreviouslyAllocated(HAL_Status status,
std::string_view message,
int32_t channel,
std::string_view previousAllocation) {
return MakeError(status,
fmt::format("{} {} previously allocated.\n"
"Location of the previous allocation:\n{}\n"
"Location of the current allocation:",
message, channel, previousAllocation));
}
} // namespace wpi::hal
extern "C" {
const char* HAL_GetLastError(int32_t* status) {
const char* HAL_GetLastError(HAL_Status* status) {
if (*status == HAL_USE_LAST_ERROR) {
LastErrorStorage& lastError = GetThreadLastError();
*status = lastError.status;