15 #include "HAL/Types.h"
16 #include "HAL/cpp/priority_mutex.h"
17 #include "HAL/handles/HandlesInternal.h"
35 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
37 friend class UnlimitedHandleResourceTest;
44 THandle Allocate(std::shared_ptr<TStruct> structure);
45 std::shared_ptr<TStruct> Get(THandle handle);
46 void Free(THandle handle);
49 std::vector<std::shared_ptr<TStruct>> m_structures;
53 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
55 std::shared_ptr<TStruct> structure) {
56 std::lock_guard<hal::priority_mutex> sync(m_handleMutex);
58 for (i = 0; i < m_structures.size(); i++) {
59 if (m_structures[i] ==
nullptr) {
60 m_structures[i] = structure;
61 return static_cast<THandle
>(createHandle(i, enumValue));
64 if (i >= INT16_MAX)
return HAL_kInvalidHandle;
66 m_structures.push_back(structure);
67 return static_cast<THandle
>(createHandle(static_cast<int16_t>(i), enumValue));
70 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
71 std::shared_ptr<TStruct>
72 UnlimitedHandleResource<THandle, TStruct, enumValue>::Get(THandle handle) {
73 int16_t index = getHandleTypedIndex(handle, enumValue);
74 std::lock_guard<hal::priority_mutex> sync(m_handleMutex);
75 if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
77 return m_structures[index];
80 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
81 void UnlimitedHandleResource<THandle, TStruct, enumValue>::Free(
83 int16_t index = getHandleTypedIndex(handle, enumValue);
84 std::lock_guard<hal::priority_mutex> sync(m_handleMutex);
85 if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
return;
86 m_structures[index].reset();
The UnlimitedHandleResource class is a way to track handles.
Definition: UnlimitedHandleResource.h:36
Definition: priority_mutex.h:57