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;
44 THandle Allocate(int16_t index, int32_t* status);
45 std::shared_ptr<TStruct> Get(THandle handle);
46 void Free(THandle handle);
49 std::array<std::shared_ptr<TStruct>, size> m_structures;
50 std::array<priority_mutex, size> m_handleMutexes;
53 template <
typename THandle,
typename TStruct, int16_t size,
54 HAL_HandleEnum enumValue>
56 int16_t index, int32_t* status) {
58 if (index < 0 || index >= size) {
59 *status = RESOURCE_OUT_OF_RANGE;
60 return HAL_kInvalidHandle;
62 std::lock_guard<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));
72 template <
typename THandle,
typename TStruct, int16_t size,
73 HAL_HandleEnum enumValue>
74 std::shared_ptr<TStruct>
75 IndexedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
77 int16_t index = getHandleTypedIndex(handle, enumValue);
78 if (index < 0 || index >= size) {
81 std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
84 return m_structures[index];
87 template <
typename THandle,
typename TStruct, int16_t size,
88 HAL_HandleEnum enumValue>
89 void IndexedHandleResource<THandle, TStruct, size, enumValue>::Free(
92 int16_t index = getHandleTypedIndex(handle, enumValue);
93 if (index < 0 || index >= size)
return;
95 std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
96 m_structures[index].reset();
The IndexedHandleResource class is a way to track handles.
Definition: IndexedHandleResource.h:37