5#include <Common/assertion.h>
18template<
typename BaseType>
25 T*
add(std::unique_ptr<T> uniquePtr);
30 std::unique_ptr<BaseType>
remove(std::size_t index);
31 std::unique_ptr<BaseType>
remove(
const BaseType* ptr);
41 void clear(std::size_t index);
45 void clearRange(std::size_t beginIndex, std::size_t endIndex);
47 BaseType*
get(std::size_t index)
const;
48 BaseType*
first()
const;
49 BaseType*
last()
const;
51 std::unique_ptr<BaseType>&
getUniquePtr(std::size_t index);
52 const std::unique_ptr<BaseType>&
getUniquePtr(std::size_t index)
const;
54 std::size_t
size()
const;
55 std::optional<std::size_t>
indexOf(
const BaseType* ptr)
const;
65 typename std::vector<std::unique_ptr<BaseType>>::iterator
begin()
noexcept;
66 typename std::vector<std::unique_ptr<BaseType>>::const_iterator
begin()
const noexcept;
67 typename std::vector<std::unique_ptr<BaseType>>::iterator
end()
noexcept;
68 typename std::vector<std::unique_ptr<BaseType>>::const_iterator
end()
const noexcept;
72 std::vector<std::unique_ptr<BaseType>> m_uniquePtrs;
77template<
typename BaseType>
82 static_assert(std::is_base_of_v<BaseType, T> || std::is_same_v<BaseType, T>,
83 "The type of `unique_ptr` is not compatible to the vector.");
85 T*
const ptr = uniquePtr.get();
86 m_uniquePtrs.push_back(std::move(uniquePtr));
90template<
typename BaseType>
95 static_assert(std::is_base_of_v<BaseType, T> || std::is_same_v<BaseType, T>,
96 "The type of `unique_ptr` is not compatible to the vector.");
100 std::make_move_iterator(uniquePtrs.
begin()),
101 std::make_move_iterator(uniquePtrs.
end()));
106template<
typename BaseType>
109 PH_ASSERT_IN_RANGE(index, 0, m_uniquePtrs.size());
111 auto uniquePtr = std::move(m_uniquePtrs[index]);
112 m_uniquePtrs.erase(m_uniquePtrs.begin() + index);
116template<
typename BaseType>
119 const auto optIndex = indexOf(ptr);
120 return optIndex ? remove(*optIndex) :
nullptr;
123template<
typename BaseType>
126 PH_ASSERT(!isEmpty());
128 auto uniquePtr = std::move(m_uniquePtrs.back());
129 m_uniquePtrs.pop_back();
133template<
typename BaseType>
136 const auto optIndex = indexOf(ptr);
141 swap(m_uniquePtrs[*optIndex], m_uniquePtrs.back());
142 m_uniquePtrs.pop_back();
150template<
typename BaseType>
153 m_uniquePtrs.clear();
156template<
typename BaseType>
159 PH_ASSERT_IN_RANGE(index, 0, m_uniquePtrs.size());
161 m_uniquePtrs[index] =
nullptr;
164template<
typename BaseType>
167 PH_ASSERT_LE(beginIndex, endIndex);
169 for(std::size_t ptrIndex = beginIndex; ptrIndex < endIndex; ++ptrIndex)
175template<
typename BaseType>
178 return index < m_uniquePtrs.size() ? getUniquePtr(index).get() :
nullptr;
181template<
typename BaseType>
184 PH_ASSERT(!isEmpty());
185 return m_uniquePtrs[0].get();
188template<
typename BaseType>
191 PH_ASSERT(!isEmpty());
192 return m_uniquePtrs[size() - 1].get();
195template<
typename BaseType>
198 const auto result = std::find_if(m_uniquePtrs.cbegin(), m_uniquePtrs.cend(),
199 [ptr](
const std::unique_ptr<BaseType>& uniquePtr)
201 return ptr == uniquePtr.get();
204 return result != m_uniquePtrs.cend()
205 ? std::optional<std::size_t>(result - m_uniquePtrs.cbegin())
209template<
typename BaseType>
212 PH_ASSERT_IN_RANGE(index, 0, m_uniquePtrs.size());
213 return m_uniquePtrs[index];
216template<
typename BaseType>
219 PH_ASSERT_IN_RANGE(index, 0, m_uniquePtrs.size());
220 return m_uniquePtrs[index];
223template<
typename BaseType>
226 return m_uniquePtrs.size();
229template<
typename BaseType>
235template<
typename BaseType>
238 return getUniquePtr(index).get();
241template<
typename BaseType>
244 return m_uniquePtrs.begin();
247template<
typename BaseType>
250 return m_uniquePtrs.begin();
253template<
typename BaseType>
256 return m_uniquePtrs.end();
259template<
typename BaseType>
262 return m_uniquePtrs.end();
Marks the derived class as move only.
Definition IMoveOnly.h:23
Definition TUniquePtrVector.h:20
void clear(std::size_t index)
Definition TUniquePtrVector.h:157
std::unique_ptr< BaseType > remove(std::size_t index)
Definition TUniquePtrVector.h:107
BaseType * first() const
Definition TUniquePtrVector.h:182
void removeAll()
Definition TUniquePtrVector.h:151
BaseType * last() const
Definition TUniquePtrVector.h:189
void clearRange(std::size_t beginIndex, std::size_t endIndex)
Set unique_ptrs in the range [beginIndex, endIndex) to nullptr.
Definition TUniquePtrVector.h:165
BaseType * operator[](std::size_t index) const
Definition TUniquePtrVector.h:236
std::unique_ptr< BaseType > removeLast()
Definition TUniquePtrVector.h:124
std::unique_ptr< BaseType > & getUniquePtr(std::size_t index)
Definition TUniquePtrVector.h:210
std::optional< std::size_t > indexOf(const BaseType *ptr) const
Definition TUniquePtrVector.h:196
T * add(std::unique_ptr< T > uniquePtr)
Definition TUniquePtrVector.h:79
void addAll(TUniquePtrVector< T > &uniquePtrs)
Definition TUniquePtrVector.h:92
std::vector< std::unique_ptr< BaseType > >::iterator begin() noexcept
Definition TUniquePtrVector.h:242
std::vector< std::unique_ptr< BaseType > >::iterator end() noexcept
Definition TUniquePtrVector.h:254
std::unique_ptr< BaseType > removeBySwapPop(const BaseType *ptr)
Remove a unique_ptr. Does not preserve the order of remaining unique_ptrs. Complexity: O(N) where N i...
Definition TUniquePtrVector.h:134
bool isEmpty() const
Definition TUniquePtrVector.h:230
std::size_t size() const
Definition TUniquePtrVector.h:224
BaseType * get(std::size_t index) const
Definition TUniquePtrVector.h:176
The root for all renderer implementations.
Definition EEngineProject.h:6