32template <
typename THandle,
typename TStruct,
int16_t size>
42 THandle* handle, int32_t* status);
51 std::array<std::shared_ptr<TStruct>, size> m_structures;
52 std::array<wpi::mutex, size> m_handleMutexes;
55template <
typename THandle,
typename TStruct,
int16_t size>
56std::shared_ptr<TStruct>
58 int16_t index,
HAL_HandleEnum enumValue, THandle* handle, int32_t* status) {
60 if (index < 0 || index >= size) {
65 std::scoped_lock lock(m_handleMutexes[index]);
67 if (m_structures[index] !=
nullptr) {
70 return m_structures[index];
72 m_structures[index] = std::make_shared<TStruct>();
76 return m_structures[index];
79template <
typename THandle,
typename TStruct,
int16_t size>
83 int16_t index = GetIndex(handle, enumValue);
84 if (index < 0 || index >= size) {
87 std::scoped_lock lock(m_handleMutexes[index]);
90 return m_structures[index];
93template <
typename THandle,
typename TStruct,
int16_t size>
97 int16_t index = GetIndex(handle, enumValue);
98 if (index < 0 || index >= size) {
102 std::scoped_lock lock(m_handleMutexes[index]);
103 m_structures[index].reset();
106template <
typename THandle,
typename TStruct,
int16_t size>
108 for (
int i = 0; i < size; i++) {
109 std::scoped_lock lock(m_handleMutexes[i]);
110 m_structures[i].reset();
The DigitalHandleResource class is a way to track handles.
Definition: DigitalHandleResource.h:33
DigitalHandleResource(const DigitalHandleResource &)=delete
friend class DigitalHandleResourceTest
Definition: DigitalHandleResource.h:34
std::shared_ptr< TStruct > Get(THandle handle, HAL_HandleEnum enumValue)
Definition: DigitalHandleResource.h:80
DigitalHandleResource()=default
int16_t GetIndex(THandle handle, HAL_HandleEnum enumValue)
Definition: DigitalHandleResource.h:43
std::shared_ptr< TStruct > Allocate(int16_t index, HAL_HandleEnum enumValue, THandle *handle, int32_t *status)
Definition: DigitalHandleResource.h:57
void ResetHandles() override
Definition: DigitalHandleResource.h:107
void Free(THandle handle, HAL_HandleEnum enumValue)
Definition: DigitalHandleResource.h:94
DigitalHandleResource & operator=(const DigitalHandleResource &)=delete
Base for all HAL Handles.
Definition: HandlesInternal.h:28
virtual void ResetHandles()
int16_t m_version
Definition: HandlesInternal.h:38
#define RESOURCE_OUT_OF_RANGE
Definition: Errors.h:80
#define RESOURCE_IS_ALLOCATED
Definition: Errors.h:78
#define HAL_SUCCESS
Definition: Errors.h:44
#define HAL_kInvalidHandle
Definition: Types.h:15
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition: ChipObject.h:40
HAL_HandleEnum
Enum of HAL handle types.
Definition: HandlesInternal.h:46
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.
int16_t getHandleTypedIndex(HAL_Handle handle, HAL_HandleEnum enumType, int16_t version)
Get if the handle is a correct type and version.
Definition: HandlesInternal.h:134