30template <
typename THandle,
typename TStruct, int16_t size,
41 THandle
Allocate(std::shared_ptr<TStruct> toSet);
45 std::shared_ptr<TStruct>
Get(THandle handle);
46 void Free(THandle handle);
50 std::array<std::shared_ptr<TStruct>, size> m_structures;
51 std::array<wpi::mutex, size> m_handleMutexes;
55template <
typename THandle,
typename TStruct, int16_t size,
59 std::shared_ptr<TStruct> toSet) {
61 std::scoped_lock lock(m_allocateMutex);
62 for (int16_t i = 0; i < size; i++) {
63 if (m_structures[i] ==
nullptr) {
66 std::scoped_lock lock(m_handleMutexes[i]);
67 m_structures[i] = toSet;
68 return static_cast<THandle
>(
createHandle(i, enumValue, m_version));
74template <
typename THandle,
typename TStruct, int16_t size,
76std::shared_ptr<TStruct>
80 int16_t index = GetIndex(handle);
81 if (index < 0 || index >= size) {
84 std::scoped_lock lock(m_handleMutexes[index]);
87 return m_structures[index];
90template <
typename THandle,
typename TStruct, int16_t size,
95 int16_t index = GetIndex(handle);
96 if (index < 0 || index >= size) {
100 std::scoped_lock allocateLock(m_allocateMutex);
101 std::scoped_lock handleLock(m_handleMutexes[index]);
102 m_structures[index].reset();
105template <
typename THandle,
typename TStruct, int16_t size,
108 enumValue>::ResetHandles() {
110 std::scoped_lock allocateLock(m_allocateMutex);
111 for (
int i = 0; i < size; i++) {
112 std::scoped_lock handleLock(m_handleMutexes[i]);
113 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 LimitedClassedHandleResource class is a way to track handles.
Definition: LimitedClassedHandleResource.h:32
void ResetHandles() override
Definition: LimitedClassedHandleResource.h:108
THandle Allocate(std::shared_ptr< TStruct > toSet)
Definition: LimitedClassedHandleResource.h:58
int16_t GetIndex(THandle handle)
Definition: LimitedClassedHandleResource.h:42
LimitedClassedHandleResource(const LimitedClassedHandleResource &)=delete
void Free(THandle handle)
Definition: LimitedClassedHandleResource.h:92
LimitedClassedHandleResource()=default
std::shared_ptr< TStruct > Get(THandle handle)
Definition: LimitedClassedHandleResource.h:77
friend class LimitedClassedHandleResourceTest
Definition: LimitedClassedHandleResource.h:33
LimitedClassedHandleResource & operator=(const LimitedClassedHandleResource &)=delete
#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
::std::mutex mutex
Definition: mutex.h:17