34template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
43 THandle
Allocate(std::shared_ptr<TStruct> structure);
47 std::shared_ptr<TStruct>
Get(THandle handle);
49 std::shared_ptr<TStruct>
Free(THandle handle);
55 template <
typename Functor>
59 std::vector<std::shared_ptr<TStruct>> m_structures;
63template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
65 std::shared_ptr<TStruct> structure) {
66 std::scoped_lock lock(m_handleMutex);
68 for (i = 0; i < m_structures.size(); i++) {
69 if (m_structures[i] ==
nullptr) {
70 m_structures[i] = structure;
71 return static_cast<THandle
>(
createHandle(i, enumValue, m_version));
78 m_structures.push_back(structure);
79 return static_cast<THandle
>(
80 createHandle(
static_cast<int16_t
>(i), enumValue, m_version));
83template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
84std::shared_ptr<TStruct>
86 int16_t index = GetIndex(handle);
87 std::scoped_lock lock(m_handleMutex);
88 if (index < 0 || index >=
static_cast<int16_t
>(m_structures.size())) {
91 return m_structures[index];
94template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
95std::shared_ptr<TStruct>
97 int16_t index = GetIndex(handle);
98 std::scoped_lock lock(m_handleMutex);
99 if (index < 0 || index >=
static_cast<int16_t
>(m_structures.size())) {
102 return std::move(m_structures[index]);
105template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
108 std::scoped_lock lock(m_handleMutex);
109 for (
size_t i = 0; i < m_structures.size(); i++) {
110 m_structures[i].reset();
116template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
117template <
typename Functor>
120 std::scoped_lock lock(m_handleMutex);
122 for (i = 0; i < m_structures.size(); i++) {
123 if (m_structures[i] !=
nullptr) {
124 func(
static_cast<THandle
>(
createHandle(i, enumValue, m_version)),
125 m_structures[i].
get());
constexpr T & get(wpi::array< T, N > &arr) noexcept
Definition: array.h:66
Base for all HAL Handles.
Definition: HandlesInternal.h:28
virtual void ResetHandles()
int16_t m_version
Definition: HandlesInternal.h:38
The UnlimitedHandleResource class is a way to track handles.
Definition: UnlimitedHandleResource.h:35
void ForEach(Functor func)
Definition: UnlimitedHandleResource.h:118
UnlimitedHandleResource & operator=(const UnlimitedHandleResource &)=delete
UnlimitedHandleResource()=default
void ResetHandles() override
Definition: UnlimitedHandleResource.h:106
THandle Allocate(std::shared_ptr< TStruct > structure)
Definition: UnlimitedHandleResource.h:64
int16_t GetIndex(THandle handle)
Definition: UnlimitedHandleResource.h:44
std::shared_ptr< TStruct > Get(THandle handle)
Definition: UnlimitedHandleResource.h:85
friend class UnlimitedHandleResourceTest
Definition: UnlimitedHandleResource.h:36
std::shared_ptr< TStruct > Free(THandle handle)
Definition: UnlimitedHandleResource.h:96
UnlimitedHandleResource(const UnlimitedHandleResource &)=delete
#define HAL_kInvalidHandle
Definition: Types.h:15
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition: ChipObject.h:40
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