29template <
typename THandle,
typename TStruct,
int16_t size,
43 std::shared_ptr<TStruct>
Get(THandle handle);
44 void Free(THandle handle);
48 std::array<std::shared_ptr<TStruct>,
size> m_structures;
49 std::array<wpi::mutex, size> m_handleMutexes;
53template <
typename THandle,
typename TStruct,
int16_t size,
57 std::scoped_lock lock(m_allocateMutex);
59 if (m_structures[i] ==
nullptr) {
62 std::scoped_lock lock(m_handleMutexes[i]);
63 m_structures[i] = std::make_shared<TStruct>();
64 return static_cast<THandle
>(
createHandle(i, enumValue, m_version));
70template <
typename THandle,
typename TStruct,
int16_t size,
72std::shared_ptr<TStruct>
76 if (index < 0 || index >=
size) {
79 std::scoped_lock lock(m_handleMutexes[
index]);
82 return m_structures[
index];
85template <
typename THandle,
typename TStruct,
int16_t size,
91 if (index < 0 || index >=
size) {
95 std::scoped_lock allocateLock(m_allocateMutex);
96 std::scoped_lock handleLock(m_handleMutexes[
index]);
97 m_structures[
index].reset();
100template <
typename THandle,
typename TStruct,
int16_t size,
104 std::scoped_lock allocateLock(m_allocateMutex);
105 for (
int i = 0; i <
size; i++) {
106 std::scoped_lock handleLock(m_handleMutexes[i]);
107 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 LimitedHandleResource class is a way to track handles.
Definition: LimitedHandleResource.h:31
LimitedHandleResource()=default
LimitedHandleResource & operator=(const LimitedHandleResource &)=delete
void ResetHandles() override
Definition: LimitedHandleResource.h:102
int16_t GetIndex(THandle handle)
Definition: LimitedHandleResource.h:40
THandle Allocate()
Definition: LimitedHandleResource.h:55
LimitedHandleResource(const LimitedHandleResource &)=delete
void Free(THandle handle)
Definition: LimitedHandleResource.h:87
friend class LimitedHandleResourceTest
Definition: LimitedHandleResource.h:32
std::shared_ptr< TStruct > Get(THandle handle)
Definition: LimitedHandleResource.h:73
#define HAL_kInvalidHandle
Definition: Types.h:15
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
::int16_t int16_t
Definition: Meta.h:55
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