15 #include "HAL/Errors.h"
16 #include "HAL/Types.h"
17 #include "HAL/cpp/make_unique.h"
18 #include "HAL/cpp/priority_mutex.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);
46 void Free(THandle handle, HAL_HandleEnum enumValue);
49 std::array<std::shared_ptr<TStruct>, size> m_structures;
50 std::array<hal::priority_mutex, size> m_handleMutexes;
53 template <
typename THandle,
typename TStruct,
int16_t size>
55 int16_t index, HAL_HandleEnum enumValue, int32_t* status) {
57 if (index < 0 || index >= size) {
58 *status = RESOURCE_OUT_OF_RANGE;
59 return HAL_kInvalidHandle;
61 std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
63 if (m_structures[index] !=
nullptr) {
64 *status = RESOURCE_IS_ALLOCATED;
65 return HAL_kInvalidHandle;
67 m_structures[index] = std::make_shared<TStruct>();
68 return static_cast<THandle
>(hal::createHandle(index, enumValue));
71 template <
typename THandle,
typename TStruct,
int16_t size>
72 std::shared_ptr<TStruct> DigitalHandleResource<THandle, TStruct, size>::Get(
73 THandle handle, HAL_HandleEnum enumValue) {
75 int16_t index = getHandleTypedIndex(handle, enumValue);
76 if (index < 0 || index >= size) {
79 std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
82 return m_structures[index];
85 template <
typename THandle,
typename TStruct,
int16_t size>
86 void DigitalHandleResource<THandle, TStruct, size>::Free(
87 THandle handle, HAL_HandleEnum enumValue) {
89 int16_t index = getHandleTypedIndex(handle, enumValue);
90 if (index < 0 || index >= size)
return;
92 std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
93 m_structures[index].reset();
The DigitalHandleResource class is a way to track handles.
Definition: DigitalHandleResource.h:36