15 #include <wpi/mutex.h>
17 #include "hal/Errors.h"
18 #include "hal/Types.h"
19 #include "hal/handles/HandlesInternal.h"
35 template <
typename THandle,
typename TStruct,
int16_t size>
37 friend class DigitalHandleResourceTest;
44 THandle Allocate(int16_t index,
HAL_HandleEnum enumValue, int32_t* status);
45 std::shared_ptr<TStruct> Get(THandle handle,
HAL_HandleEnum enumValue);
47 void ResetHandles()
override;
50 std::array<std::shared_ptr<TStruct>, size> m_structures;
51 std::array<wpi::mutex, size> m_handleMutexes;
54 template <
typename THandle,
typename TStruct,
int16_t size>
58 if (index < 0 || index >= size) {
59 *status = RESOURCE_OUT_OF_RANGE;
60 return HAL_kInvalidHandle;
62 std::lock_guard<wpi::mutex> lock(m_handleMutexes[index]);
64 if (m_structures[index] !=
nullptr) {
65 *status = RESOURCE_IS_ALLOCATED;
66 return HAL_kInvalidHandle;
68 m_structures[index] = std::make_shared<TStruct>();
72 template <
typename THandle,
typename TStruct,
int16_t size>
73 std::shared_ptr<TStruct> DigitalHandleResource<THandle, TStruct, size>::Get(
76 int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
77 if (index < 0 || index >= size) {
80 std::lock_guard<wpi::mutex> lock(m_handleMutexes[index]);
83 return m_structures[index];
86 template <
typename THandle,
typename TStruct,
int16_t size>
87 void DigitalHandleResource<THandle, TStruct, size>::Free(
90 int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
91 if (index < 0 || index >= size)
return;
93 std::lock_guard<wpi::mutex> lock(m_handleMutexes[index]);
94 m_structures[index].reset();
97 template <
typename THandle,
typename TStruct,
int16_t size>
98 void DigitalHandleResource<THandle, TStruct, size>::ResetHandles() {
99 for (
int i = 0; i <
size; i++) {
100 std::lock_guard<wpi::mutex> lock(m_handleMutexes[i]);
101 m_structures[i].reset();
103 HandleBase::ResetHandles();
HAL_HandleEnum
Enum of HAL handle types.
Definition: HandlesInternal.h:47
Base for all HAL Handles.
Definition: HandlesInternal.h:29
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition: SimDataValue.h:19
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:999
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.
The DigitalHandleResource class is a way to track handles.
Definition: DigitalHandleResource.h:36