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