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;
43 THandle Allocate(int16_t index, HAL_HandleEnum enumValue, int32_t* status);
44 std::shared_ptr<TStruct> Get(THandle handle, HAL_HandleEnum enumValue);
45 void Free(THandle handle, HAL_HandleEnum enumValue);
48 std::array<std::shared_ptr<TStruct>, size> m_structures;
49 std::array<priority_mutex, size> m_handleMutexes;
52 template <
typename THandle,
typename TStruct,
int16_t size>
54 int16_t index, HAL_HandleEnum enumValue, int32_t* status) {
56 if (index < 0 || index >= size) {
57 *status = RESOURCE_OUT_OF_RANGE;
58 return HAL_kInvalidHandle;
60 std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
62 if (m_structures[index] !=
nullptr) {
63 *status = RESOURCE_IS_ALLOCATED;
64 return HAL_kInvalidHandle;
66 m_structures[index] = std::make_shared<TStruct>();
67 return static_cast<THandle
>(hal::createHandle(index, enumValue));
70 template <
typename THandle,
typename TStruct,
int16_t size>
71 std::shared_ptr<TStruct> DigitalHandleResource<THandle, TStruct, size>::Get(
72 THandle handle, HAL_HandleEnum enumValue) {
74 int16_t index = getHandleTypedIndex(handle, enumValue);
75 if (index < 0 || index >= size) {
78 std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
81 return m_structures[index];
84 template <
typename THandle,
typename TStruct,
int16_t size>
85 void DigitalHandleResource<THandle, TStruct, size>::Free(
86 THandle handle, HAL_HandleEnum enumValue) {
88 int16_t index = getHandleTypedIndex(handle, enumValue);
89 if (index < 0 || index >= size)
return;
91 std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
92 m_structures[index].reset();
The DigitalHandleResource class is a way to track handles.
Definition: DigitalHandleResource.h:36