32template <
typename THandle,
typename TStruct, int16_t size,
42 std::shared_ptr<TStruct>
Allocate(int16_t index, THandle* handle,
47 std::shared_ptr<TStruct>
Get(THandle handle);
48 void Free(THandle handle);
52 std::array<std::shared_ptr<TStruct>, size> m_structures;
53 std::array<wpi::mutex, size> m_handleMutexes;
56template <
typename THandle,
typename TStruct, int16_t size,
58std::shared_ptr<TStruct>
60 int16_t index, THandle* handle, int32_t* status) {
62 if (index < 0 || index >= size) {
67 std::scoped_lock lock(m_handleMutexes[index]);
69 if (m_structures[index] !=
nullptr) {
72 return m_structures[index];
74 m_structures[index] = std::make_shared<TStruct>();
78 return m_structures[index];
81template <
typename THandle,
typename TStruct, int16_t size,
83std::shared_ptr<TStruct>
86 int16_t index = GetIndex(handle);
87 if (index < 0 || index >= size) {
90 std::scoped_lock lock(m_handleMutexes[index]);
93 return m_structures[index];
96template <
typename THandle,
typename TStruct, int16_t size,
101 int16_t index = GetIndex(handle);
102 if (index < 0 || index >= size) {
106 std::scoped_lock lock(m_handleMutexes[index]);
107 m_structures[index].reset();
110template <
typename THandle,
typename TStruct, int16_t size,
113 for (
int i = 0; i < size; i++) {
114 std::scoped_lock lock(m_handleMutexes[i]);
115 m_structures[i].reset();
Base for all HAL Handles.
Definition: HandlesInternal.h:28
virtual void ResetHandles()
int16_t m_version
Definition: HandlesInternal.h:38
The IndexedHandleResource class is a way to track handles.
Definition: IndexedHandleResource.h:34
void ResetHandles() override
Definition: IndexedHandleResource.h:112
friend class IndexedHandleResourceTest
Definition: IndexedHandleResource.h:35
IndexedHandleResource()=default
int16_t GetIndex(THandle handle)
Definition: IndexedHandleResource.h:44
std::shared_ptr< TStruct > Get(THandle handle)
Definition: IndexedHandleResource.h:84
void Free(THandle handle)
Definition: IndexedHandleResource.h:98
IndexedHandleResource & operator=(const IndexedHandleResource &)=delete
std::shared_ptr< TStruct > Allocate(int16_t index, THandle *handle, int32_t *status)
Definition: IndexedHandleResource.h:59
IndexedHandleResource(const IndexedHandleResource &)=delete
#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