[Core] Use std type traits to check operations triviality.

(cherry picked from commit 6f02183f8c)
This commit is contained in:
Fabio Alessandrelli
2022-08-04 13:53:03 +02:00
committed by Rémi Verschelde
parent fbc7fc44ae
commit 4e33610fb0
3 changed files with 17 additions and 13 deletions

View File

@ -37,6 +37,8 @@
#include "core/sort_array.h"
#include "core/vector.h"
#include <type_traits>
template <class T, class U = uint32_t, bool force_trivial = false>
class LocalVector {
protected:
@ -64,7 +66,7 @@ public:
CRASH_COND_MSG(!data, "Out of memory");
}
if (!__has_trivial_constructor(T) && !force_trivial) {
if (!std::is_trivially_constructible<T>::value && !force_trivial) {
memnew_placement(&data[count++], T(p_elem));
} else {
data[count++] = p_elem;
@ -77,7 +79,7 @@ public:
for (U i = p_index; i < count; i++) {
data[i] = data[i + 1];
}
if (!__has_trivial_destructor(T) && !force_trivial) {
if (!std::is_trivially_destructible<T>::value && !force_trivial) {
data[count].~T();
}
}
@ -90,7 +92,7 @@ public:
if (count > p_index) {
data[p_index] = data[count];
}
if (!__has_trivial_destructor(T) && !force_trivial) {
if (!std::is_trivially_destructible<T>::value && !force_trivial) {
data[count].~T();
}
}
@ -146,7 +148,7 @@ public:
_FORCE_INLINE_ U size() const { return count; }
void resize(U p_size) {
if (p_size < count) {
if (!__has_trivial_destructor(T) && !force_trivial) {
if (!std::is_trivially_destructible<T>::value && !force_trivial) {
for (U i = p_size; i < count; i++) {
data[i].~T();
}
@ -163,7 +165,7 @@ public:
data = (T *)memrealloc(data, capacity * sizeof(T));
CRASH_COND_MSG(!data, "Out of memory");
}
if (!__has_trivial_constructor(T) && !force_trivial) {
if (!std::is_trivially_constructible<T>::value && !force_trivial) {
for (U i = count; i < p_size; i++) {
memnew_placement(&data[i], T);
}