Add trivial destructor condition to reserved_vector

This commit is contained in:
Jack Andersen 2017-09-17 17:31:50 -10:00
parent 41032d0a7c
commit 285669db05
1 changed files with 20 additions and 12 deletions

View File

@ -3,6 +3,7 @@
#include <vector>
#include <algorithm>
#include <type_traits>
#include <stdlib.h>
#include "optional.hpp"
#include <logvisor/logvisor.hpp>
@ -182,8 +183,9 @@ public:
reserved_vector() : base(0) {}
~reserved_vector()
{
for (size_t i=0 ; i<base::x0_size ; ++i)
base::_value(i).~T();
if (std::is_destructible<T>::value && !std::is_trivially_destructible<T>::value)
for (size_t i=0 ; i<base::x0_size ; ++i)
base::_value(i).~T();
}
void push_back(const T& d)
@ -224,7 +226,8 @@ public:
Log.report(logvisor::Fatal, "pop_back() called on empty rstl::reserved_vector.");
#endif
--base::x0_size;
base::_value(base::x0_size).~T();
if (std::is_destructible<T>::value && !std::is_trivially_destructible<T>::value)
base::_value(base::x0_size).~T();
}
iterator insert(const_iterator pos, const T& value)
@ -287,8 +290,9 @@ public:
}
else if (size < base::x0_size)
{
for (size_t i = size; i < base::x0_size; ++i)
base::_value(i).~T();
if (std::is_destructible<T>::value && !std::is_trivially_destructible<T>::value)
for (size_t i = size; i < base::x0_size; ++i)
base::_value(i).~T();
base::x0_size = size;
}
}
@ -307,8 +311,9 @@ public:
}
else if (size < base::x0_size)
{
for (size_t i = size; i < base::x0_size; ++i)
base::_value(i).~T();
if (std::is_destructible<T>::value && !std::is_trivially_destructible<T>::value)
for (size_t i = size; i < base::x0_size; ++i)
base::_value(i).~T();
base::x0_size = size;
}
}
@ -322,14 +327,16 @@ public:
for (auto it = base::_const_cast_iterator(pos) + 1; it != base::end(); ++it)
*(it - 1) = std::forward<T>(*it);
--base::x0_size;
base::_value(base::x0_size).~T();
if (std::is_destructible<T>::value && !std::is_trivially_destructible<T>::value)
base::_value(base::x0_size).~T();
return base::_const_cast_iterator(pos);
}
void clear()
{
for (auto it = base::begin(); it != base::end(); ++it)
it->~T();
if (std::is_destructible<T>::value && !std::is_trivially_destructible<T>::value)
for (auto it = base::begin(); it != base::end(); ++it)
it->~T();
base::x0_size = 0;
}
};
@ -347,8 +354,9 @@ class prereserved_vector : public _reserved_vector_base<T, N>
}
void _deinit()
{
for (auto& i : base::x4_data)
reinterpret_cast<T*>(std::addressof(i))->~T();
if (std::is_destructible<T>::value && !std::is_trivially_destructible<T>::value)
for (auto& i : base::x4_data)
reinterpret_cast<T*>(std::addressof(i))->~T();
}
public:
using base = _reserved_vector_base<T, N>;