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;
43 THandle Allocate(std::shared_ptr<TStruct> structure);
44 std::shared_ptr<TStruct> Get(THandle handle);
45 void Free(THandle handle);
48 std::vector<std::shared_ptr<TStruct>> m_structures;
52 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
54 std::shared_ptr<TStruct> structure) {
55 std::lock_guard<priority_mutex> sync(m_handleMutex);
57 for (i = 0; i < m_structures.size(); i++) {
58 if (m_structures[i] ==
nullptr) {
59 m_structures[i] = structure;
60 return static_cast<THandle
>(createHandle(i, enumValue));
63 if (i >= INT16_MAX)
return HAL_kInvalidHandle;
65 m_structures.push_back(structure);
66 return static_cast<THandle
>(createHandle(static_cast<int16_t>(i), enumValue));
69 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
70 std::shared_ptr<TStruct>
71 UnlimitedHandleResource<THandle, TStruct, enumValue>::Get(THandle handle) {
72 int16_t index = getHandleTypedIndex(handle, enumValue);
73 std::lock_guard<priority_mutex> sync(m_handleMutex);
74 if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
76 return m_structures[index];
79 template <
typename THandle,
typename TStruct, HAL_HandleEnum enumValue>
80 void UnlimitedHandleResource<THandle, TStruct, enumValue>::Free(
82 int16_t index = getHandleTypedIndex(handle, enumValue);
83 std::lock_guard<priority_mutex> sync(m_handleMutex);
84 if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
return;
85 m_structures[index].reset();
The UnlimitedHandleResource class is a way to track handles.
Definition: UnlimitedHandleResource.h:36
Definition: priority_mutex.h:53