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);
47 void ResetHandles()
override;
50 std::array<std::shared_ptr<TStruct>, size> m_structures;
51 std::array<hal::priority_mutex, size> m_handleMutexes;
54 template <
typename THandle,
typename TStruct,
int16_t size>
56 int16_t index, HAL_HandleEnum enumValue, int32_t* status) {
58 if (index < 0 || index >= size) {
59 *status = RESOURCE_OUT_OF_RANGE;
60 return HAL_kInvalidHandle;
62 std::lock_guard<hal::priority_mutex> sync(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>();
69 return static_cast<THandle
>(hal::createHandle(index, enumValue, m_version));
72 template <
typename THandle,
typename TStruct,
int16_t size>
73 std::shared_ptr<TStruct> DigitalHandleResource<THandle, TStruct, size>::Get(
74 THandle handle, HAL_HandleEnum enumValue) {
76 int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
77 if (index < 0 || index >= size) {
80 std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
83 return m_structures[index];
86 template <
typename THandle,
typename TStruct,
int16_t size>
87 void DigitalHandleResource<THandle, TStruct, size>::Free(
88 THandle handle, HAL_HandleEnum enumValue) {
90 int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
91 if (index < 0 || index >= size)
return;
93 std::lock_guard<hal::priority_mutex> sync(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<hal::priority_mutex> sync(m_handleMutexes[i]);
101 m_structures[i].reset();
103 HandleBase::ResetHandles();
Definition: HandlesInternal.h:26
The DigitalHandleResource class is a way to track handles.
Definition: DigitalHandleResource.h:36