16 #include <wpi/mutex.h>
18 #include "hal/Types.h"
19 #include "hal/handles/HandlesInternal.h"
37 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
39 friend class UnlimitedHandleResourceTest;
46 THandle Allocate(std::shared_ptr<TStruct> structure);
47 std::shared_ptr<TStruct> Get(THandle handle);
49 std::shared_ptr<TStruct> Free(THandle handle);
50 void ResetHandles()
override;
55 template <
typename Functor>
56 void ForEach(Functor func);
59 std::vector<std::shared_ptr<TStruct>> m_structures;
60 wpi::mutex m_handleMutex;
63 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
65 std::shared_ptr<TStruct> structure) {
66 std::lock_guard<wpi::mutex> 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));
74 if (i >= INT16_MAX)
return HAL_kInvalidHandle;
76 m_structures.push_back(structure);
77 return static_cast<THandle
>(
78 createHandle(static_cast<int16_t>(i), enumValue, m_version));
81 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
82 std::shared_ptr<TStruct>
83 UnlimitedHandleResource<THandle, TStruct, enumValue>::Get(THandle handle) {
84 int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
85 std::lock_guard<wpi::mutex> lock(m_handleMutex);
86 if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
88 return m_structures[index];
91 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
92 std::shared_ptr<TStruct>
93 UnlimitedHandleResource<THandle, TStruct, enumValue>::Free(THandle handle) {
94 int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
95 std::lock_guard<wpi::mutex> lock(m_handleMutex);
96 if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
98 return std::move(m_structures[index]);
101 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
102 void UnlimitedHandleResource<THandle, TStruct, enumValue>::ResetHandles() {
104 std::lock_guard<wpi::mutex> lock(m_handleMutex);
105 for (
size_t i = 0; i < m_structures.size(); i++) {
106 m_structures[i].reset();
109 HandleBase::ResetHandles();
112 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
113 template <
typename Functor>
114 void UnlimitedHandleResource<THandle, TStruct, enumValue>::ForEach(
116 std::lock_guard<wpi::mutex> lock(m_handleMutex);
118 for (i = 0; i < m_structures.size(); i++) {
119 if (m_structures[i] !=
nullptr) {
120 func(static_cast<THandle>(
createHandle(i, enumValue, m_version)),
121 m_structures[i].
get());
The UnlimitedHandleResource class is a way to track handles.
Definition: UnlimitedHandleResource.h:38
Base for all HAL Handles.
Definition: HandlesInternal.h:29
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition: SimDataValue.h:19
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.