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,
36 HAL_HandleEnum enumValue>
38 friend class IndexedHandleResourceTest;
45 THandle Allocate(int16_t index, int32_t* status);
46 std::shared_ptr<TStruct> Get(THandle handle);
47 void Free(THandle handle);
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,
55 HAL_HandleEnum enumValue>
57 int16_t index, int32_t* status) {
59 if (index < 0 || index >= size) {
60 *status = RESOURCE_OUT_OF_RANGE;
61 return HAL_kInvalidHandle;
63 std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
65 if (m_structures[index] !=
nullptr) {
66 *status = RESOURCE_IS_ALLOCATED;
67 return HAL_kInvalidHandle;
69 m_structures[index] = std::make_shared<TStruct>();
70 return static_cast<THandle
>(hal::createHandle(index, enumValue));
73 template <
typename THandle,
typename TStruct, int16_t size,
74 HAL_HandleEnum enumValue>
75 std::shared_ptr<TStruct>
76 IndexedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
78 int16_t index = getHandleTypedIndex(handle, enumValue);
79 if (index < 0 || index >= size) {
82 std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
85 return m_structures[index];
88 template <
typename THandle,
typename TStruct, int16_t size,
89 HAL_HandleEnum enumValue>
90 void IndexedHandleResource<THandle, TStruct, size, enumValue>::Free(
93 int16_t index = getHandleTypedIndex(handle, enumValue);
94 if (index < 0 || index >= size)
return;
96 std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
97 m_structures[index].reset();
The IndexedHandleResource class is a way to track handles.
Definition: IndexedHandleResource.h:37