Vector: Call destructors on reassignment and call element copy constructors
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-04-18 20:17:05 +02:00
parent 02f8a50b9d
commit 2ecb1e7c90
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -36,13 +36,10 @@ template <typename T> class Vector
{
if (&other == this) return *this;
if (m_data) free_impl(m_data);
m_data = nullptr;
m_capacity = m_size = 0;
if (m_data) clear();
reserve(other.capacity());
memcpy(m_data, other.data(), other.size());
for (usize i = 0; i < other.size(); i++) { new (&m_data[i]) T(other.m_data[i]); }
m_size = other.size();
return *this;
@ -52,7 +49,7 @@ template <typename T> class Vector
{
if (&other == this) return *this;
if (m_data) free_impl(m_data);
if (m_data) clear();
m_data = other.data();
m_capacity = other.capacity();
@ -66,11 +63,7 @@ template <typename T> class Vector
~Vector()
{
if (m_data)
{
for (const T& item : *this) { item.~T(); }
free_impl(m_data);
}
if (m_data) { clear(); }
}
Result<void> try_reserve(usize capacity)