[wpiutil,hal] Move C++ Handle wrapper to wpiutil (#8935)

Also move WPI_Handle typedef to its own header (util/Handle.h).
This commit is contained in:
Peter Johnson
2026-06-01 13:57:25 -07:00
committed by GitHub
parent 3d982f81dd
commit 3f0d7bc2c4
24 changed files with 126 additions and 127 deletions

View File

@@ -6,15 +6,17 @@
#include <stdint.h>
#include "wpi/util/Handle.h"
/**
* @defgroup hal_types Type Definitions
* @ingroup hal_capi
* @{
*/
#define HAL_INVALID_HANDLE 0
#define HAL_INVALID_HANDLE WPI_INVALID_HANDLE
typedef int32_t HAL_Handle;
typedef WPI_Handle HAL_Handle;
typedef HAL_Handle HAL_AlertHandle;

View File

@@ -1,77 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// 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.
#pragma once
#include "wpi/hal/Types.h"
namespace wpi::hal {
/**
* A move-only C++ wrapper around a HAL handle.
* Will free the handle if FreeFunction is provided
*/
template <typename CType, void (*FreeFunction)(CType) = nullptr,
int32_t CInvalid = HAL_INVALID_HANDLE>
class Handle {
public:
Handle() = default;
/*implicit*/ Handle(CType val) : m_handle(val) {} // NOLINT
Handle(const Handle&) = delete;
Handle& operator=(const Handle&) = delete;
Handle(Handle&& rhs) : m_handle(rhs.m_handle) { rhs.m_handle = CInvalid; }
Handle& operator=(Handle&& rhs) {
if (this != &rhs) {
// FIXME: GCC gives the false positive "the address of <GetDefault> will never
// be NULL" because it doesn't realize the default template parameter can make
// GetDefault nullptr. Fixed in GCC 13.
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105885
#if __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waddress"
#endif // __GNUC__
if constexpr (FreeFunction != nullptr) {
#if __GNUC__
#pragma GCC diagnostic pop
#endif // __GNUC__
if (m_handle != CInvalid) {
FreeFunction(m_handle);
}
}
}
m_handle = rhs.m_handle;
rhs.m_handle = CInvalid;
return *this;
}
~Handle() {
// FIXME: GCC gives the false positive "the address of <GetDefault> will never
// be NULL" because it doesn't realize the default template parameter can make
// GetDefault nullptr. Fixed in GCC 13.
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105885
#if __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waddress"
#endif // __GNUC__
if constexpr (FreeFunction != nullptr) {
#if __GNUC__
#pragma GCC diagnostic pop
#endif // __GNUC__
if (m_handle != CInvalid) {
FreeFunction(m_handle);
}
}
}
operator CType() const { return m_handle; } // NOLINT
private:
CType m_handle = CInvalid;
};
} // namespace wpi::hal