8 #ifndef CSCORE_UNLIMITEDHANDLERESOURCE_H_ 9 #define CSCORE_UNLIMITEDHANDLERESOURCE_H_ 15 #include <wpi/ArrayRef.h> 16 #include <wpi/SmallVector.h> 17 #include <wpi/mutex.h> 39 template <
typename THandle,
typename TStruct,
int typeValue,
40 typename TMutex = wpi::mutex>
47 template <
typename... Args>
48 THandle Allocate(Args&&... args);
49 THandle Allocate(std::shared_ptr<THandle> structure);
51 std::shared_ptr<TStruct> Get(THandle handle);
53 void Free(THandle handle);
64 std::pair<THandle, std::shared_ptr<TStruct>> FindIf(F func);
67 THandle MakeHandle(
size_t i) {
68 return THandle{
static_cast<int>(i),
69 static_cast<typename THandle::Type>(typeValue)};
71 std::vector<std::shared_ptr<TStruct>> m_structures;
75 template <
typename THandle,
typename TStruct,
int typeValue,
typename TMutex>
76 template <
typename... Args>
79 std::lock_guard<TMutex> sync(m_handleMutex);
81 for (i = 0; i < m_structures.size(); i++) {
82 if (m_structures[i] ==
nullptr) {
83 m_structures[i] = std::make_shared<TStruct>(std::forward<Args>(args)...);
87 if (i >= THandle::kIndexMax)
return 0;
89 m_structures.emplace_back(
90 std::make_shared<TStruct>(std::forward<Args>(args)...));
94 template <
typename THandle,
typename TStruct,
int typeValue,
typename TMutex>
96 std::shared_ptr<THandle> structure) {
97 std::lock_guard<TMutex> sync(m_handleMutex);
99 for (i = 0; i < m_structures.size(); i++) {
100 if (m_structures[i] ==
nullptr) {
101 m_structures[i] = structure;
102 return MakeHandle(i);
105 if (i >= THandle::kIndexMax)
return 0;
107 m_structures.push_back(structure);
108 return MakeHandle(i);
111 template <
typename THandle,
typename TStruct,
int typeValue,
typename TMutex>
112 inline std::shared_ptr<TStruct>
116 handle.GetTypedIndex(static_cast<typename THandle::Type>(typeValue));
117 if (index < 0)
return nullptr;
118 std::lock_guard<TMutex> sync(m_handleMutex);
119 if (index >= static_cast<int>(m_structures.size()))
return nullptr;
120 return m_structures[index];
123 template <
typename THandle,
typename TStruct,
int typeValue,
typename TMutex>
127 handle.GetTypedIndex(static_cast<typename THandle::Type>(typeValue));
128 if (index < 0)
return;
129 std::lock_guard<TMutex> sync(m_handleMutex);
130 if (index >= static_cast<int>(m_structures.size()))
return;
131 m_structures[index].reset();
134 template <
typename THandle,
typename TStruct,
int typeValue,
typename TMutex>
135 template <
typename T>
139 ForEach([&](THandle handle,
const TStruct& data) { vec.push_back(handle); });
143 template <
typename THandle,
typename TStruct,
int typeValue,
typename TMutex>
144 template <
typename F>
147 std::lock_guard<TMutex> sync(m_handleMutex);
148 for (
size_t i = 0; i < m_structures.size(); i++) {
149 if (m_structures[i] !=
nullptr) func(MakeHandle(i), *(m_structures[i]));
153 template <
typename THandle,
typename TStruct,
int typeValue,
typename TMutex>
154 template <
typename F>
155 inline std::pair<THandle, std::shared_ptr<TStruct>>
157 std::lock_guard<TMutex> sync(m_handleMutex);
158 for (
size_t i = 0; i < m_structures.size(); i++) {
159 auto& structure = m_structures[i];
160 if (structure !=
nullptr && func(*structure))
161 return std::make_pair(MakeHandle(i), structure);
163 return std::make_pair(0,
nullptr);
166 template <
typename THandle,
typename TStruct,
int typeValue,
167 typename TMutex = wpi::mutex>
182 #endif // CSCORE_UNLIMITEDHANDLERESOURCE_H_ Definition: CvSourceImpl.h:19
Definition: UnlimitedHandleResource.h:168
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: WindowsSupport.h:184
Definition: UnlimitedHandleResource.h:41